Skip to content
loading...
markdown guide
 

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 !

 

I am a fan of Robert Martin's Clean Code.

Clean code also means "not having (too) clever code" by reducing multiple simple lines with one long complicated line. Rather the opposite: break up a long complicated line into multiple short, simple, understandable lines.

It means coding for obviousness, maintainability, simplicity.

And since it is Robert Martin, he also champions code that has abundant unit tests.

Depending on your programming language, some languages are very verbose. Lots of ceremony and annotation.

Other languages are very succinct. Perhaps to the point of line-noise incomprehensibility... I'm looking at you, Perl!

If you cannot change your language for your project, you have what you have. And have to make do to make it appropriate Clean Code as best you can. Assuming the developer has embraced Clean Code.

 

Robert Martin is very correct. By me saying write less code i don't mean "make it a cleaver one liner". I have given an example above of what i mean.

Classic DEV Post from Apr 5

Are newer developers pushed too exclusively towards web development?

I get the impression that web development is the overwhelming path of choice for bootcamps, etc. But...

Sadick profile image
An experienced software engineer who enjoys working on interesting and challenging projects.Open to new opportunities.