What is elegant coding?

Asked

Viewed 787 times

19

I always see some answers to questions suggesting that if you do something x is more elegant than the y form. I started thinking what would be elegant in our context.

9 pages of 50 records with the term: /search?q=elegante

Dictionary:

  1. which is characterized by harmony, lightness or naturalness. "costume and." 2. Frequented by elegant people (it is said of place or environment); select. 3. exquisite in the choice of words; refined, fine. "syntax and." 4. morally or intellectually correct; honorable, noble. "dispute and." 5. relating to elegant persons or their actions. "dinner and." 6. two-gender adjective and noun that or who reveals good taste in the choice of their garments and the manner of wearing them. "man and." 7. two-gender adjective and noun that or who reveals correctness and fineness in attitudes; distinct, delicate.

When did this term start to be used in programming? In programming what can be defined as elegant?

  • 2

    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.

  • 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 ?

2 answers

16


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.

  • It became much clearer. When I asked I thought maybe it would be difficult to explain, but I think I understood the "essence".

3

Excellent question.

While this can bring several interpretations based on each developer’s professional experience, the final destination of an elegant code must meet some requirements that ultimately lead toa code that will meet quality requirements for someone with several years of language experience.

I have listed some points below where some of them can be understood as natural consequence of others, but it is important to list them all:

  • The code is straightforward and goal in your purpose, doing nothing but your responsibility.
  • The code avoids "super engineering" unnecessary to the size of the problem you are willing to solve, based on the information available at the time.
  • Classes, methods and variables have well-defined names, doing exactly what is expected of them when reading the code.
  • It does not violate aspects of good practice of language and does not contain vulnerabilities obvious.
  • The solution approaches the minimum possible code used, not being so short as to be indecipherable and not so long as to divert attention from understanding.
  • The code is easily testable by automated testing.
  • Does not violate other important non-functional points, as safety and performance, meeting the first and being (at least) acceptable in the second.
  • Comments to describe what the code does are missing, as the code is already clear enough. Commentary are found only to clarify business questions.
  • If the code is disputed, the author can easily defend it, because it was he is based on different sources to clarify its quality, as: good books, authors, Internet (Stack Overflow, for example) and tools of the area. This is important, as understanding elegant code can differ greatly between developers and a "proof" may be required for consensus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.