To ask a programmer what a beautiful code is and whether it should be like this is the same as asking an architect what a beautiful house is and whether it should be like that. What is a beautiful house is unlikely the architect will explain to you, and if you ask him whether he should be, he will, of course, say that he should.
The code, in my opinion, should be beautiful. It is very good if it is beautiful, but sometimes you have to do without it. In fact, each of us in our hearts dreams that someday he will build his own opera house in Sydney or something like that. In the meantime, you have to build communal.
Maybe this is a bit pessimistic, but if you put yourself in the place of a person who in the 50s moved from a communal apartment to his own house - even if a small five-story one, which we now dismissively call “Khrushchev” - this resettlement gave him much more happiness and joy in life than any beauty that he could be hung from above.
The beauty and clarity of the code is not exactly the same. The clearest code is code that is not there. The smaller the code, the better. The code of a hundred lines is clearer than the code of 10,000 lines, no matter how beautifully and magically the second one was written. This is the first. Secondly, it seems to me that clarity and general understanding of the code is like sex. In the sense that it is better not to engage alone.
Whether the code should be beautiful depends, of course, on the context. Probably, the washing machine code may not be very beautiful, because not many people write it. And when a lot of people are involved in the project, the code should at least not be very nasty. What is a dirty room in which cigarette butts creak under their feet, this is probably understandable. The code may also be the one you want to run away from, yes. And where a person has a feeling of rejection, I do not want to work.
Probably, the code should be to some extent beautiful, and to some degree decent. In beautiful letters, you can write some utter nonsense. And it happens ... That is, it is not only indents, of course.
Recently, I took a course in neuroscience at Curser. In it, people were put in an MRI, showed pictures, and asked to say which ones are beautiful and which ones are not. Then the researchers looked at which areas of the brain are activated by the participants. In short, at the sight of a beautiful picture, that area of ​​the brain, which is responsible for what you can eat, is activated, and at the sight of a bad picture, the area that is responsible for escaping is activated. Beautiful code is a code that is suitable for food, and from which you do not want to run as far as possible.
Everyone wants beautiful code. More seriously, in practice, in large systems, the perfect beautiful code is impossible. The code should not be disgusting, because the disgusting code cannot be used. And you will need to spend time to bring it back to normal. But ideally beautiful code is also some time spent. Teams do not have time to lick it.
Beautiful code is a minimalistic code. This means that it should not do anything extra in addition to the task that we are actually trying to solve with this code. To avoid spreading noodles, but simply a clear concise description of the thought that exists. That's what a beautiful code is.
The code should be readable, but that does not make it beautiful. I have seen a very readable code, which, from my point of view, is not beautiful.
Oddly enough, the code probably does not have to be beautiful. Beauty is a good quality of his, but completely optional. The code, first of all, must be well-functioning, be understandable to people who read it, not be excessively complex for the problem to be solved. Will he be beautiful? It would be nice if he was beautiful at the same time, but in principle it is not necessary.
I do not know what a beautiful code is. Beautiful is some kind of aesthetic category. Does it fit on the screen, does the image resemble. It is difficult for me to operate with these concepts. They do not really interest me, to be honest.
The best code is one that does not exist. So if you already had to write the code, then something went wrong. But if you write it, write so that it is easy to read. Because if a person is unpleasant to read your code, then you tried in vain. In general, writing code takes 10% of the time, and the remaining 90% is reading what has already been written. Therefore, I do not know if a beautiful code is needed, but a well-readable code is important.
The unreadable code is usually written by new developers. And, of course, we have to deal with this when we recruit new people or when we download something from the Internet. Despite the fact that it is useful, and we could use it, someone wrote everything so that it is impossible to read, the variable names consist of one letter, and the efforts that need to be spent on reading are comparable to writing everything. again. Therefore, this code, of course, ugly. Beautiful is a clear code, and especially beautiful code that is not.
That is, if you managed to simplify the problem and solve it by a method that is not much worse or even better, then this is undoubtedly a gain. If you have a system and you managed to erase half of it, this is a great celebration to celebrate. If you can avoid the invention of a complex method, then so be it. If simple linear search works fast enough, then fine.
For me, the main feature of a beautiful code is flexibility. What is his development potential? When you do a task that is complicated and changes right under your feet, you want to avoid redrawing the half-project every time. You make changes in several places - everything is ready. The code must be provided in advance for all growth points. I believe that the programmer should turn the feeling of beauty precisely in this direction.
It can be trained. Show the programmer a beautiful code, teach him to admire him. And then a trained sense of beauty will help make the right architectural decisions. They will look beautiful to you.
And I think that it’s right to arrange spaces, which variable names to give, for which files to expand your project and into which daddies, I think is vanity. You have to be above this: as you agree with your colleagues, so do. Let others argue about this.
It seems to me that the term “beautiful code” means two different things. People who have recently programmed one beautiful code, but for those who have been doing this for a long time and stumbled upon a lot, another. The first is an aesthetic that falls on some abstract representations of beauty in my head, and if this happens, it seems that the code is perfect. And the other is when the code performs its role well and will be understood, for example, by one hundred people who will read this code after me. And this often turns out to be very different codes.
Whether the code should be beautiful depends on a particular goal. In general, it seems to me that yes. People should strive for what they themselves like. And beautiful code is something they like. If people write something, in their opinion, unpleasant and terrible, they will not like what they do. Then, for example, and Yandex will be bad. So everyone should strive to write beautiful code, but with some corrections that occur over the years in their heads.
Beautiful code is for me two completely different categories. An algorithmic solution can be beautiful - and this is one story. And maybe, for example, some beautiful trick. For example, in ancient times, in order to quickly clear a piece of memory, there was really a quick-fast pushing by just an assembler command to push, because the push worked faster than filling it through registers. And it was just a beautiful trick. Beautiful tricks in industrial programming - in the place where we are talking about serious loads, some big serious decisions - most often there is no place. But there is always a place for beautiful algorithms. And, of course, the code should be beautiful and everyone should know that the code is power.
The main task of the code is to solve the task that has been set for it. The most important thing the code has to do is work. Can be broken down into stages. First, the code should be compiled (if it is code in compiled languages), then it should pass tests, then it should work. And finally, if this is a code addressed to the user, he should please him. That is, the code performs a huge number of different tasks. Should the code please the programmer himself? Well, of course, should. Otherwise, why should he actually work as a programmer? Not for the money, we all work here, right?
Source: https://habr.com/ru/post/210638/
All Articles