DEV Community

Discussion on: Keeping your code clean by sweeping out "if" statements

Collapse
 
brycebba profile image
brycebba

I have to say this being "cleaner" isn't necessarily true to me. I think it is more clever for sure but as someone who works professionally on a bug fixing team I would MUCH rather see the ifs in my codebase than someone trying to be clever but then again, people trying to be "clever" or write less lines is why I have the job fixing bugs that I have which I love. I'm not trying to insult your code, I'm simply saying the basics are something everyone understands and I have seen countless times where a refactoring to produce less lines or "simplify" something is the cause of a bug or difficult debugging to find a bug. Ultimately is all personal preference until performance is analyzed but when you work for an enterprise level company with tons of devs cranking out code it is better not to be "clever" and to be consistent and predictable.

Collapse
 
tomazfernandes profile image
Tomaz Lemos

Hi brycebba, thanks for replying!

I hear the complaints about it being
“clever” code. Normally clever code is when someone sacrifices readability or comprehension to create an one liner or to use a design pattern just for using it.

First of all, I don’t see where is the complexity of these solutions. They are just different from what we are used to, but it has one very simple line of code (map.get). One can change the requireNonNull to an “if” if he or she is not used to it.

What I don’t see people talking here is about how easy or hard it is understanding the business rules behind the logic, which is my main point.

I think code should be read like a business document, because in the end of the day that’s what we do: we code business rules.

So I really don’t see these patterns as “clever” code, because in my opinion they enhance the business rules readability.

As for the bug fixing, in my experience it’s really easy to debug such patterns because there’s really one place that can have a bug, which is the map, and it’s very easy to notice when something is wrong. Can’t say the same about those lots of ifs.

Cheers!