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

I have recently heard about verbose code reduction (along with the term boiler plate code), and also when studying ES6 when falling into arrow-functions. Would you like a clearer definition of what verbose code would be? And why is it interesting to reduce it?

Author: Vinicius Brasil, 2016-12-23

3 answers

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

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

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

There is another chain that says that the code is more readable if it is shorter, if you can put more understandable code in the same field of view. I have no way to locate, but there are studies that indicate this, and it goes for anything, not just programming.

It should be obvious that if you have distractions throughout the code, if you have too much information, or if the code gets too mnemonic (encrypted) by trying to shorten as much as it gives, it doesn't get readable. Have that having the right commitment that gives the most adequate expressiveness to whatever you do.

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

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

Example of 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 to another function. It gets confusing to have a function inside an argument, it gets too long to read, and there's no gain.

We use conventions to more compactly and possibly concisely indicate the same thing. Thus we eliminate: the word function which in this context is not necessary; and the return because the idea is that this form is very simple, that only has one line and is already a return always, and that by having only one line, does not need the ;, and also the keys are not needed for the same reason. But we add => to make it unambiguous and show that the intention is that.

Shortcode

Do not confuse concise (non-verbose) code with the shortest possible code. It will not abuse one-letter variables, join everything in one line, cut spaces, etc. just to look shorter, this is cut the meat and not the fat from the code. The ideal is to write smooth, that is, clean code. If you pile it all up, it doesn't get 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, that goes straight to the point, it is easier to read. Of course, first you have to learn that notation, but that's the easy part of learning to program.

What does not give right is to force the programmer to have to learn a new notation every codebase he goes to mess with. In each language gives. Even most copy the others, so it is rare for someone to invent something very new, and in general brings what is already used in mathematics.

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

Example to see how it is easier to read:

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

Of course that those who do not know the notation of the former may not understand, but it is a temporary disability of her , she learned, it becomes easier to read the terse code.

Verbose languages

Some languages are more verbose than others. They prefer words than symbols. They prefer to be more explicit than implicit(and it happens to be even 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 is modernizing to be even less. CoffeeScript is a language that runs on top of JS that has as its main characteristic to be very little verbose. Some think that in an exaggerated way. When exaggerating begins give room for errors or illegibility. There is something that seems exaggerated, but it has a function to be there.

 18
Author: Maniero, 2020-06-11 14:45:34

The expression "verbose code" refers to code that is longer than would be expected, either because the logic is too complicated or because the 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 it is to understand the code (although it is not rule).

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

 10
Author: Filipe Costa, 2017-04-13 12:45:56

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 amount of lines and the difficulty of understanding the expected behavior.

In JS, more specifically ES6 arrow functions can reduce the number of lines and keep the 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 da to have idea of the improvement of readability and even with line reduction

 0
Author: Elizeu Santos, 2018-04-13 13:46:22