First understand that evaluation will always be subjective, even though there may be objective criteria.
The dictionary definition is good. Of course it does not help to understand the points to consider, but I also believe that even in a book can not define in a concrete way. And there are several who try to do this.
-
Obviously all smart code is correct, this I put as hors concours . It is not enough to "work", it has to be correct . He does what he should do in any planned situation and deals well with unplanned situations.
-
I think the main feature is the code is readable by qualified professionals a>. Note the differentiation. Being readable to lay people, students, amateur pseudo-professionals is not a requirement to be elegant. The code should be easily noticed, even though you may need to comment that make sense .
-
Another very strong feature is that it should be simple, stupid . The problem may be complex , the solution should be simple , which is the opposite of complicated.
-
Being a clean code is another feature. There should be nothing you do not need . You should not have more than one responsibility. But it also should not shorten to the point that it is not easy to understand what is happening there. It's no use creating a lot of layers to achieve this goal and make it worse.
-
The code should be clear and express well what you want. It must be an obvious solution. It should be as concise as possible without compromising legibility. For this you must have the right abstractions, and the appropriate abstraction level.
-
Usually it should do the way it is already known by everyone and not try to invent a weird shape needlessly. You should choose the right algorithms. Do not reinvent the wheel, in a good way.
-
You should follow coding conventions in general terms, by language and by team. This makes it easier to follow what is written. Elegant code is well formatted, has not too many whites and not less (vertical or horizontal). It does not have unnecessary abbreviations, it uses meaningful names.
-
Must have a form that facilitates maintenance and avoids bugs . You can trust the code. You can change without fright.
-
It probably should not premature optimization . But it should be as fast as possible without compromising other features. And of course you should have the desired performance if possible. But more than being fast, it needs to be efficient.
-
Should not be clever . This is a word usually used to indicate that the programmer has tried to make a very intelligent, short, performative code that seems to do something curious, that takes advantage of some characteristic of language or mathematics to obtain the result, but which is not easily understood . Clever code might look elegant, but it's the opposite.
You only get this if you master computing (in the mathematical and technical sense), you know how the computer, language, and other features work. You can only do elegant code if you understand the problem you are solving and have made a prior planning of what you need, what can go wrong, of all needs. So you can say that you only write elegant code every time, someone who has experience, and is not stubborn :) Of course, copying elegant code from others does that elegant stretch, but to produce innovative, stylish code, you need "ground".
Elegance is doing what is best suited for that case. And following blind rules does not help much. That's why best practices do not directly help make elegant code. They are different things.
Elegant code is possibly the one that someone shows you how you could have done it and you say "why did not I think of it before?" :).
A WTF code is not elegant.
It has languages that encourage elegant code, others not so much.
Is it clearer? Maybe not, but it's the kind of thing that does not show up on paper.