What is elegant coding?

18

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

9 pages of 50 records with the term: link

Dictionary:

  
  • which is characterized by harmony, lightness or naturalness.   "suit and."   2.   frequented by elegant people (it is said of place or environment); select.   3.   exquisite in the choice of words; sharp, fine.   "syntax and."   4.   moral or intellectually correct; honored, noble.   "dispute and."   5.   elegant persons or their acts.   "Dinner e."   6.   adjective and noun of two genera   who or who shows good taste in the choice of their robes and in the way of using them.   "man and."   7.   adjective and noun of two genera   who or who reveals correctness and finesse in attitudes; distinct, delicate.
  •   

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

        
    asked by anonymous 23.12.2016 / 14:18

    2 answers

    16

    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.

        
    23.12.2016 / 15:04
    3

    Excellent question.

    While this may bring multiple interpretations based on each developer's work experience, the ultimate fate of an elegant code must meet some requirements that ultimately leads us to a code that will meet quality requirements for someone with several years of experience in language.

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

    • The code is direct and objective in its purpose, doing nothing beyond its responsibility.
    • The code avoids "super engineering" unnecessary for 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.
    • Does not violate aspects of good practices and does not contain obvious vulnerabilities.
    • The solution comes close to 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 , such as security and performance, fulfilling the first and being (at least) acceptable in the second.
    • The comments to describe what the code does are missing, because the code is already clear enough. Comments are only found to clarify business doubts .
    • If the code is contested, the author can easily defend it, as it was based on different sources to clarify its quality, such as: good books, authors , Internet (Stack Overflow, for example), and area tools. This is important because understanding elegant code may differ greatly between developers and a "check" may be necessary for consensus.
    27.12.2016 / 13:53