Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding if statements

I was thinking about object oriented design today, and I was wondering if you should avoid if statements. My thought is that in any case where you require an if statement you can simply create two objects that implement the same method. The two method implementations would simply be the two possible branches of the original if statement.

I realize that this seems extreme, but it seems as though you could try and argue it to some extent. Any thoughts on this?

EDIT

Wow that didn't take long. I suppose this is way too extreme. Is it possible to say though, that under OOP you should expect way less if statements?

SECOND EDIT

What about this: An object that determines its method implementation based on its attributes. That is to say you can implement someMethod() in two ways and specify some restrictions. At any point an object will route to the correct method implementation based on its properties. So in the case of if(x > 5) just have two methods that rely on the x attribute

like image 544
Ori Avatar asked Aug 26 '09 21:08

Ori


People also ask

How do you avoid multiple if conditions?

You could maintain a mapping of strings to enums elsewhere in the program, pull out the enum associated with the returned string from the map (with a default NO_MATCH in case the string isn't in the map) and write a switch statement on the enums.

How do you avoid multiple nested if statements?

Nested IFs are powerful, but they become complicated quickly as you add more levels. One way to avoid more levels is to use IF in combination with the AND and OR functions. These functions return a simple TRUE/FALSE result that works perfectly inside IF, so you can use them to extend the logic of a single IF.

What is the replacement for if/then else statement?

' or ' '. An alternative to IF-THEN-ELSE I use often is the use of logical expressions. A logical expression is specified within parentheses '()' and are evaluated as being true or false.

Why should we avoid if-else?

In my experience, using if-else statements are not a good practice. It makes the code less readable and it makes developers lazy in thinking of better ways of making their code more readable.


2 Answers

I can tell you one thing. No matter what people say, thinking about simplifying and eliminating unnecessary branching is a sign of you maturing as a software developer. There are many reasons why branching is bad, testing, maintenance, higher rate of bugs and so on. This is one of the things I look for when interviewing people and is an excellent indicator how mature they are as a developer. I would encourage you to keep experimenting, simplifying your code and design by using less conditions. When I did this switch I found much less time debugging my code, it simply worked, then when I had to change something, changes were super easy to make since most of the code was sequential. Again I would encourage you 100% to keep doing what you are doing no matter what other people say. Keep in mind most developers are working and thinking at much lower level and just follow the rules. So good job bringing this up.

like image 82
vance Avatar answered Sep 29 '22 13:09

vance


Explain how to implement the following without an if statement or ternary logic:

if ( x < 5 ) {    x = 0 } else {    print x; } 
like image 44
Stefan Kendall Avatar answered Sep 29 '22 14:09

Stefan Kendall