How do I avoid having a chain of nested if and else if?
I'm new to coding and I'm working on a project and found myself falling into
If Code If Code Elseif Code
There has to be something better that I'm not aware of right ?
I just don't want to be like that yandere dev
Jackson Stewart
case
Blake Rivera
/thread also, most times you can reduce the level of nesting ifs just coding in another way.
Brandon Martinez
Does not work in all languages. Python does not have switch statements (worst thing about the language), but there you can use a dictionary of functions.
Daniel Fisher
yo estoy trabajando en lua tambien
Jack Brooks
>How do I avoid having a chain of nested if and else if? You don't. Why would you even want that?
Aiden Roberts
functions, hundreds of them
each one simple enough to debug, but with a specific task assigned to it.
then code like this:
try: foo1() except e: foo2() finally: foo3()
in foo1() you can do an if-else, or try if else except , etc.
break everything into simple functions that call each other and which you can check/debug easily.
Xavier Nguyen
You could replace: if x < y: z = True else: z = False
With: z = False
if x < y: z = True
But the first option is generally considered better practice and neater imo.
If you're dealing with lots of elseif blocks, you'll just want to use a switch statement.
Aiden Phillips
if (condition&&condition) { do; }
Henry Cruz
Why are a bunch of ifs even bad? It's like goto where if someone sees one instance of it ever they have a heart attack
Daniel Stewart
Could you set z equal to the result of x < y directly?
Wyatt Collins
Yep. z = x < y
You could also do something like this, which is called a ternary operation. z = "a" if x < y else "b"
Zachary Thomas
Can and should.
Jaxson Thompson
>implying that isn't the same sort of thing
Switch statements are worse actually.
Luis Sanchez
depends on the problem really. sometimes it can't be avoided due to the nature of the problem.
other times you may not understand the problem thoroughly enough to reduce the conditionals to an absolute minimum.
and yet other times restructuring the order of your code can change the logic used.
Brandon Allen
Write a truth table of the conditions. You'll often find that it's easier to check the negative case vs all the positive ones. Sometimes you'll even realize some cases are redundant. Also nested if's with no else statements can be reduced to a single conditional with &.
Even still, try to keep the conditional nesting as flat as possible. If it's more than 3 levels deep, you're (probably) doing something wrong.
Nathaniel Rogers
So Not nested. But just a lot of ifs. Is ok?
I mean if you have to check for different things and NEED a different response for each thing and aren't using a switch
If Code ElseIf Code Elseif Code Else Code
Ayden Perez
>Not nested. But just a lot of ifs. Is ok? Sure. Why wouldn't it be?
Landon Scott
It looks ugly to me
Elijah Lopez
>NEED a different response for each thing if you just want to return some message, use a hashmap.
Oliver Parker
Not to me. It expresses the pattern you want to express in the clearest and least ambiguous way.
Tyler Evans
If your language supports it, then depending on what conditions you check a switch/case statement can sometimes look nicer on the page.
Jordan Ramirez
I find that switch statements are almost never more readable than if/else chains. In my code, I never ever use them, and the code is better for it.