Does syntax affect readability?

Sometime ago I had a quick discussion with a colleague about this line of code:

errors.length == 0 && submit();

In his view, this wasn’t readable – being readable code that which doesn’t need translation, or as little as possible.
He argued that the code would be much better expressed like this:

if (errors.length == 0) {
  submit();
}

Thinking about this discussion, my understanding of his point of view was that the JavaScript’s shortened syntax sacrificed readability, making the reader translate the first version into the second in order to understand the meaning of the code – which is, in this case, “submit if there are no errors”.

I agree with him that the code’s intention should be made clearer. We could achieve that by assigning a new name:

var isFormValid = errors.length == 0;

 
And perhaps changing the other one:

submitLoginForm();

 
But I disagree completely that syntax alone may decrease code quality. My understanding is that such statement could be used to say that using the English contractions (do not – don’t), or even the spoken French liaisons also diminishes the understanding of the receiver.

Syntax is a matter of habit and understanding – and it also may provide shortcuts.
Names, on the other hand, can define meaning to operations.

I would go for this code:

var formValid = errors.length == 0;

formValid && submitLoginForm();

 

And you?

Comments are closed.