0
Is it practical?
This question is not related to a specific programming language. Is it fine to use multiple if else-if statements? It might not be the best solution, but can it be useful in some programmes?
17 Answers
+ 5
Frankly speaking, I haven't ever seen cascade of else-ifs that couldn't be rewritten with switch, map or a couple of polymorphic classes; and oh boy have I seen spaghetti of else-ifs.
Do you have an example in mind, or is it more of general question?
+ 4
A chain of else ifs is very inefficient as each must be evalulated in series. If possible, it is better to nest ifs to limit the expressions evalulated. However, there are times you have no choice. In that case, order them so those used most often are first.
+ 4
I agree with BlazingMagpie... For every 20 or 30 code reviews I'll do that involves many if-else-if statements, maybe one will not require being refactored.
This is typically a code smell that indicates room for improvement which many junior developers eventually overcome as they become familiar with new design patterns, SOLID Principles, and various coding practices they've not yet been exposed to.
It's not a problem for those in the learning stages. Learners should be more focused on just getting things to work and thinking imperatively about implementing solutions for their code.
This coding pattern with many if else's chained together becomes less and less present among intermediate and senior developers working on larger code bases - especially those with high code coverage in unit tests.
I highly recommend reading more about how, when, and why this and other code smells should be refactored.
https://refactoring.guru/refactoring/techniques/simplifying-conditional-expressions
Hope this was helpful.
+ 3
To quote Tim Peter's
"... practicality beats purity."
Basically, functioning code... code that works, but is not pretty, is better than pretty, non-working code. I hope this helps 👍👍
https://www.python.org/dev/peps/pep-0020/
+ 2
What I do when I have to use long if-else-if or switch statement is I seperate it to a function. The function only contains my switch statement. Is that not how we write reducers in Redux?
+ 2
Usually, long if-else-if ladders are a sign that the function does more than one thing. It is best to split it.
+ 1
Steven M Yeah, but sometimes, there can be a cleaner code with shorter lines and more readability, isn't that considered more "Pythonic"?
I am wondering if I have to look for such a cleaner way all the time, especially when I am dealing with a language I am less familiar with.
+ 1
Code optimization is part of the Continuous Improvement Continuous Delivery Software Development Lifecycle, there will always be updates, there will always be something to improve, the most important part of development is to deliver a Minimum Viable Product and if your MVP has several if-else statements and works, that is okay. However, if your code looks good, uses the latest and greatest optimizations, but doesn't work, then that is not okay because now the program has to be re-written
+ 1
better learn ternary operator for c,c++,javascript etc.it might be best alternate for if-else.And for python use if-else because there is no ternary operator .
0
Can I ask for help David Carroll John Wells ?
0
BlazingMagpie After writing one of my very first scripts, I later found out that those 150 lines of code could just be simplified into 30 lines (and maybe even shorter), back then I had that feeling that all those ifs and else ifs are repeating almost the same thing. Ever since then I tried to avoid them as much as possible, but now I am using a language I am less familiar with and found my self using multiple conditional statements, however, I don't really feel that I am repeating the code. Perhaps, I can spend time trying to implement the same ideas (since the two languages provide many similar features). But I am focusing more on the functionality.
0
Thanks for the great answers!
I truly appreciate your help!
0
Ali Abdelhady Please do share a sample of the code you're referring to. It would provide more context about this scenario which might be beneficial to others sharing the same experience.
0
David Carroll sure!
Let me introduce an "attributes" simple console game: version 1
Looking back at it, the code is really repeating itself!
https://code.sololearn.com/cCl8M5fwU5wg/?ref=app
And here is version 2
(Almost the same, but just making more use of some common data types and methods)
https://code.sololearn.com/cgSU59tI3vHa/?ref=app
0
Krishnaprasanth D V Thanks for the advice.
Python actually has a ternary operator as well.
b = 8
a = b + 4 if b == 8 else 5
0
ali abdelhady i know this bro.It is big headache in python.It is not like ternary it is just if else in a single line 😂😂😂😂