Skip to content

re: Write less code VIEW POST


I'm not so sure about that.
If you put 5 lines of code in 1 with trickier code you would make the code just way harder to read.
I would rather advice to be flexible, sometimes writing more code and sometimes less.
But that's just my opinion.


I completely agree, but lets consider this.

if (expression) {
  return true
} else  {
  return false

Which can be reduced to

return expression

And would still be clear


I agree your example is typical of useless verbosity. I find that the problem with the above 5-lines code is not so much that the one-line code is equivalent, it is more that the 5-line code is redundent, it repeats itself somehow.

Note that in some languages, it might not be equivalent (in JavaScript for instance, the latter returns an expression that could be of any type, while the former returns only true or false, the two possible boolean values.

return Boolean(expression) or return !!expression would be the JavaScript equivalent of the former expression.

However, if you have a nice tricky formula that covers several cases, and you use your clever formula instead of a clear if - else if - else statement, you might give a hard time to your future colleagues that will have to maintain and augment your code. This other person could be you some months or years later !

code of conduct - report abuse