DEV Community

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

Collapse
iulianbojinca profile image
iulian-bojinca

I can't believe the comments I'm reading. The 'if' discussion is part of a bigger subject: declarative vs imperative. And there are plenty of writings about this. The general consent is that you use imperative solutions as far away from your domain logic as possible. When you keep abstracting functions, you will end up will some mathematical and computational functions that underneath are maybe using ifs, switches or fors like map does. These functions are then easily packed in a lib of some sort and you just keep using the declarative style in the rest of your code. It's a longer discussion overall, but to see so many people defending the imperative style...makes me sad. I have seen so many bugs and hit so many problems because of the imperative approach used on the wrong levels and I still have to fight for this because people are unwilling to learn and move past the God damn 'if' chapter and approach my code as 'clever'. It is not clever, it is programming and this approach WILL clean your app for 90% of your bugs and WILL make you move faster when changes are required.

Collapse
binarydelight profile image
Binary Delight • Edited on

Well said, these comments also make me sad. It means so many programmers still don’t get it.

Reducing Cyclomatic Complexity is vital to reduce bugs. Avoiding the use of a hash and rather using ifs is not clever. It’s short sighted.

Collapse
alwynschoeman profile image
Alwyn Schoeman

The problem is that people do not work with significantly large and/or complex programs with multiple developers.

The examples here are simple value mappings, but what happens when those if blocks perform logic? Different types of logic blatantly ignoring the single responsibility principle. The more complicated they get the bigger the chance that there are unmatched use cases.

But that's a code smell? Yes, but if statements are like candy to children who just want to plug some code in somewhere to finish a ticket. Code they don't understand and that gets more complicated with each if statement.

Apart from functional declarative solutions there is also the proper use of OOP which is really what design patterns are all about.

All of these require reasoning about code, which apparently is too much too ask.

Thread Thread
binarydelight profile image
Binary Delight

Agreed. This is going to sound snobbish. Anyone can code; but not everyone can apply coding principles.

Collapse
tomazfernandes profile image
Tomaz Lemos Author

Hi Roger, thanks for your feedback! I've written a blog post about another pattern, if you'd like to take a look I'd really appreciate your feedback on that as well.

Thanks again and happy new year!

Collapse
laughingraven profile image
Laughing Raven

Bold statements, but dubious at best. Don't be clever. Use the if statement. Think about all the overhead you introduce and how much more difficult your code is to debug.

Collapse
binarydelight profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Binary Delight

Clearly you don’t get the difference between declarative programming and imperative and would rather stay “not clever”. It’s fine, I hire clever declarative programmers over imperative programmers . Clever programmers who write smart code, less code, which leads to less bugs. The reason you prefer using the if statement is because you find it easier to debug, which obviously is very important to you because that’s what you do a lot of... because of your spaghetti if code. Now that’s dubious .

Thread Thread
lagerenas profile image
lagerenas

Hmm. Reading this article and reflecting on the times I have encountered the map pattern I initially did not like it. It separates the logic into two different places. That makes it harder to trace through the code and find the right place to modify. You are however right that I have to do it less often using this pattern.

Thread Thread
laughingraven profile image
Laughing Raven • Edited on

As an electrical engineer, coding is only a small part of what I do. The rest of my day is spent in planning, documentation, testing for regulatory compliance and debugging. Then again, I write firmware for medical equipment. It must be correct or people die.

Thread Thread
skyl profile image
Skylar Saveland

Imperative style with lots of ifs takes a lot more unit tests to prove it works compared to declarative style. One shouldn’t be debugging, one should be testing and isolating complexity out of the main program. This thread is scary, frankly.

Don’t worry about cyclomatic complexity, just do a lot of debugging to make sure people don’t die? Hrm ...

If it has to work or people die, then you really should scrutinize every if statement carefully and see if you can get rid of it. Many ifs are bugs waiting to be discovered.