What is the definition of verbose code? And why is it interesting to reduce it?

Asked

Viewed 4,038 times

17

I have recently heard about verbose code reduction (next to the term Boiler Plate code), and also by studying ES6 by falling into Arrow-functions. Would you like a clearer definition of what verbose code would be? And why is it interesting to narrow it down?

3 answers

17


Verbose code is one that needs more words, or longer words, than necessary to properly express the intent of the code.

In verbose codes there are many very long symbols or symbols.

There is a current that says that if the code is as close to natural language as possible to human it will be readable. If this were true the languages today would tend to this more and more and COBOL would still be very successful for new projects.

There’s another current that says the code is more readable if it’s shorter, if you can put more understandable code in the same field of view. I can’t locate it, but there are studies that indicate it, and it goes for something, not just programming.

It should be obvious that if you have distractions along the code, if you have too much information, or if the code gets mnemonic (encrypted) too much by trying to shorten it as much as it gives, it is not readable. You have to have the right commitment that gives the most appropriate expression to whatever you do.

In some cases writing too much can even pass the initial impression of ambiguity. There are cases that encourage writing a code one way when there would fit another style.

Programming languages should always encourage the writing of the shortest possible code that is easily understandable, especially in situations where concepts end up merging.

Example of the question

Why write function() { return 42; } when you can only write () => 42?

This is especially useful in cases where the function is passed as an argument from another function. It gets confusing to have a function within an argument, it gets too long to read and there is no gain.

We use conventions to indicate more compact and possibly concise the same thing. So delete: the word function which is not necessary in this context; and return because the idea is that this form is very simple, that it has only one line and is already a return always, and that because it has only one line, it does not need the ;, and also keys are not required for the same reason. But we added => to make unambiguous and show that the intention is this.

Shortcode

Do not confuse concise (non-verbose) code with the shortest possible code. It will not abuse variables of a letter, to join everything in one line, cut spaces, etc. just to seem shorter, that is to cut the meat and not the fat of the code. The ideal is to write Internet code, that is, clean. If you stack everything up, it is not clean.

It is understood that a mathematical notation is as expressive as a natural language notation if it is to express a concept that will be widely used, and because it is shorter, succinct, which goes straight to the point, it is easier to read. Of course first one has to learn that notation, but that’s the easy part of learning to program.

What does not work is to force the programmer to learn a new notation for each code base he will move. In each language it does. Even the majority copies the others, so it is rare someone invents something very new, and in general brings what is already used in mathematics.

Less verbose code should always be clearer and readable, not the other way around.

Example to see how easy it is to read:

var array = [1, 2, 3];
var quadrados = array.map(x => x * x);
var quadrados2 = array.map(function(x) { return x * x });

I put in the Github for future reference.

Clear-cut that those who do not know the notation of the former may not understand, but it is a temporary deficiency of it, learnt, it is easier to read the code.

Verbose languages

Some languages are more verbose than others. They prefer words to symbols. They prefer to be more explicit than implicit (and there is a case that is better). They prefer commonly used names that are long because it sounds more natural. Some adopt a style or paradigm that preaches more ceremony and requires things that are of little use in most cases to benefit the rare cases.

Javascript was never very verbose, but it was a bit, now it’s modernizing to be even less. Coffeescript is a language that runs on top of JS that has as its main feature to be rather verbose. Some find it exaggerated. When exaggeration begins to give room for mistakes or illegibility. There is something that seems exaggerated, but has a function to be there.

  • COBOL is still very successful for new projects. I’m not sure what your point is. Do you think that with the arrival of the PC, with 95% + professional programmers writing in COBOL, the reason why COBOL is not important for modern languages is ... some ill-defined "verbose"? Nothing to do with a COBOL compiler at the moment being about the same cost as the PC? Seriously?

  • I don’t know what you’re talking about. There are practically only new projects in COBOL that are associated with old projects in COBOL or in very specific scenarios. COBOL was my second language, but 95% of today’s programmers have never heard of COBOL. I do not know if you tried to dispute that COBOL is not verbose, if that was it, I do not know what to say, because I think it is the consensus that is the most verbose language there is.

  • The issue is Javascript and ES6. It mentions the word "verbose", so you mention COBOL ... You then turn on "verbose" for why there are so few people (relative) who know COBOL for non-cobol ... for all COBOL programmers I know who bought Pcs from the mid-80s to the mid-90s, the cost of a compiler was why they did not write in COBOL.

  • COBOL has gone from being the predominant language of business to not. In addition to expensive compilers, breaking the principles of "computer science" according to those who invented computer science probably contributed to less teaching. There are probably multi-billion X86 machines and less than 10,000 "COBOL-domain" machines, but the weight of transactions between large organizations is the other way.

  • I deny COBOL is "verbose" in terms of the question? Of course.

10

The expression "verbose code" refers to code that is longer than one would expect, either because logic is too complicated or because language itself requires it.

For example, in this case the two codes do exactly the same, but the second example is much less "verbose" and easy to understand:

Java:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Ruby:

print "Hello World!"

Typically the less verbose, the easier/faster the code is to understand (although it is not a rule).

More information about "verbose languages": https://softwareengineering.stackexchange.com/questions/141175/why-is-verbosity-bad-for-a-programming-language

0

The verbose code expression is related to a code that uses many lines to do something that can be done in less without compromising readability.

There needs to be a harmony between the number of lines and the difficulty of understanding the expected behavior.

In JS, more specifically ES6 functions can reduce the number of lines and keep code readable.

// sem arrow function
let relatorio = {
  gerarPdf: function() {
    // código aqui...
  },
  gerarRelatorio: function(){
    document.addEventListener('click', function(e) {
      this.gerarPdf();
    }.bind(this));
  }
}

// com arrow function
let relatorio = {
  gerarPdf: function() {
    // codigo aqui ...
  },
  gerarRelatorio: function() {
    document.addEventListener('click', (e) => 
    this.gerarPdf());
  }
}

The example was minimal, but gives to have idea of the improvement of readability and even with reduction of lines

Browser other questions tagged

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