DEV Community

Discussion on: 2 Simple Tips To Clean Nested IF/ELSE Conditions.

Collapse
 
darkain profile image
Vincent Milum Jr

If you are using early-out return statements entirely, then the "else" statements are never needed.

Another method I've done in the past for very large lists (this only works in a few languages)

switch (true) {
case a: stuff1; break;
case b: stuff2; break;
case c: stuff3; break;
case d: stuff4; break;
}

That's right! Some languages will allow switch/case statements to compare a static value to a set of expressions, instead of the other way around ;)

Collapse
 
rhuzaifa profile image
Huzaifa Rasheed

In the context of this specific example, I am using early out return for the purpose of demonstrating the Guard Clause. Else statement can be avoided, but not covered since the article only focuses to reduce if/else nesting.

However, there are some cases in where one has to use else conditions.