First understand that the evaluation will always be subjective, although there may be objective criteria.
The dictionary’s definition is good. Of course it does not help to understand the points to consider, but I also believe that even in a book you can not define in a concrete way. And there are several who try to do that.
Obviously all the elegant code is correct, I put it as hors concours. It’s 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 characteristic is the code being readable by qualified professionals. Note the differentiation. Being readable to laymen, students, amateurs the pseudo-professionals is not a requirement to be elegant. The code should be easily understood, even if it may require comment that makes sense.
Another very strong feature is that it should be simple, stupid. The problem may be complex, the solution must be simple, which is the opposite of complicated.
Be a code clean is another feature. There must be nothing you don’t need. You should not have more than one responsibility. But you should not shorten it to the point that it is not easy to understand what is happening there. No use creating a lot of layers to achieve this goal and make another worse.
The code shall be clear-cut and express well what you want. It should be an obvious solution. It should be as concise as possible without compromising readability. For this it must have the right abstractions, and the proper abstract level.
Usually he should do the way he already is known by all and do not try to invent a weird way unnecessarily. You must choose the appropriate algorithms. Do not reinvent the wheel in the good way.
Must follow conventions of coding established in general terms, by language and by the team. Thus it is easier to follow what is written. Elegant code is well formatted, not too white and not too much (vertical or horizontal). No unnecessary abbreviations, uses significant names.
There must be a way facilitate maintenance and avoid bugs. You can trust the code. You can change without fright?
Probably him shouldn’t premature optimization. But it should be as fast as possible without compromising other characteristics. And of course it should have the desired performance if possible. But more than being fast, it needs to be efficient.
Shouldn’t Clever. This is a word usually used to indicate that the programmer tried to make a very clever, performatic, short code, which seems to do something curious, which takes advantage of some characteristic of language or mathematics to get the result, but which is not easily understandable. Clever code may seem elegant, but it’s the opposite.
You only get this if you master computing (in the mathematical and technical sense), know how the computer, language and other resources work.
You can only make 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 only write elegant code always, who has experience, and is not stubborn :) Of course copying elegant code from others does that elegant excerpt, but to produce elegant innovative code needs "floor".
Elegance is doing what is best suited for that case. And following blindly prepared rules doesn’t help much. This is why good practices do not directly help to make elegant code. They are separate things.
The elegant code is possibly the one that someone shows you how you could have done and you say "why didn’t I think of it before?" :).
A code WTF isn’t 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 doesn’t show up on paper. As the name implies, it involves (good) taste.
Related: What defines a clean code? - Obs: since explained that it is not something standardized or technical or anything like that, the question seems to me valid if the answer(s) answer(s) are well explained I think, the problem are the bad answers that usually attract, but even so should not fail to ask.
– Guilherme Nascimento
Although it’s an excellent question, I think it asks for answers based on achism. Which, if I’m not mistaken, is against the stackoverflow guidelines. Correct ?
– Gato de Schrödinger