I've had a colleague that told me he once worked for a company that had as a policy to never have conditionals ("if" and "switch" statements) in the code and that they let all the decisions in the code be done using polymorphism and (I'm guessing) some other OO principles.
I sort of understand the reasoning behind this, of having code that is more DRY and easier to update, but I'm looking for a more in-depth explanation of this concept. Or maybe it's part of a more general design approach.
If anyone has any resources for this or would be willing to explain or even have some more terms related to this I can use to find more answers I'd be much obliged.
I found one question on SO that was kind of related but I'm unfamiliar with C++ so I don't understand too much of the answers there.
(I'm no OO guru btw but I can manage)
I'm most proficient in PHP, and after that Python so I'd prefer info that uses those languages.
Update: I'll ask my colleague for more info on what he meant exactly.
Update 2015: after some more years of experience in programming I see now that the aim of this policy was probably to prevent programmers from adding functionality in a haphazard way by just adding conditionals (if statements) in certain places. A better way to extend software is to use the "Open/Closed principle" where software is extended by using inheritance and polymorphism. I strongly doubt whether the policy was super strict on all conditionals as it's kinda hard to go completely without them.
What will you do? 2. Conditionals without "if" are also possible using inversion. We can omit "if" and use "should" (Type 1 Conditionals), "were" (Type 2 Conditionals) and "had" (Type 3 Conditionals) before the subject.
If statements are logical blocks used within programming. They're conditional statements that tell a computer what to do with certain information. In other words, they let a program make 'decisions' while it's running. They're comprised of a minimum of two parts, 'if' and 'then'.
Imperative If statements are evil because they force you to create side-effects when you don't need to. For an If statement to be meaningful, you have to produce different "effects" depending on the condition expression.
There are some resources on the Anti-IF Campaign site, such as this article.
I believe it's a matter of degree. Conditionals aren't always bad, but they can be (and frequently are) abused.
Additional thoughts (one day later)
Refactoring: Improving the Design of Existing Code is a good reference on this subject (and many others). It covers Replace Conditional with Polymorphism. There's also a new one, Replace Conditional with Visitor, on the web site.
I value simplicity and single responsibility over removing all if
statements. These three goals often coincide. Static analysis tools that support the cyclomatic complexity metric can quickly point out code with nested or serial conditionals. The if
statements may remain post-refactoring, but could be broken out into smaller methods and/or multiple classes.
Update: Michael Feathers wrote an article on Unconditional Programming.
This is a popular topic: Phil Haack on Death to the IF statement!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With