7

That moment where you see code of someone who riddles their code with nested if-else and if-elseif statements.

I don't remember writing an else statement for years. It almost always can be avoided (and the rare cases where it makes sense I prefer the switch statement).

Yet I never grasp why people do:

```
if(someCondition) {
// huge nested code block
} else {
throw new Error();
}
```

Instead of

```
if (!someCondition) {
throw new Error();
}
// continue in the normal scope
```

And then we have experts that like doing:

```
if(someCondition) {
if (bar) {
$foo = 'narf';
} else {
$foo = 'poit';
}

// huge code block
if($foo == 'narf') {
if(yetAntherCondition) {
// huge code block
} else {
throw new Error();
}
// huge code block
} else {
throw new Error();
}
} else {
throw new Error();
}
```

Help!

If ever was to design a programming language, I'd forbid the `else` and `elseif` keywords. I have yet to find an instance where I could not replace some `else` by either a guard or an early return or introducing some polymorphism.

Comments
  • 1
    I am also against the huge nested mess but why is a single else worse than the alternatives?
Add Comment