DEV Community

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

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!