DEV Community


Posted on


Write less code

More code means there is more to read and more to understand — it makes our programs harder to comprehend.The more code you have, the more places there are for bugs to hide.

Top comments (5)

imrc21 profile image

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.

sadick profile image
Sadick • Edited

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

lioobayoyo profile image

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 !

eljayadobe profile image
Eljay-Adobe • Edited

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.

sadick profile image

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.

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.