DEV Community

loading...

Discussion on: Write better code and be a better programmer by NEVER USING ELSE statements

Collapse
dglsparsons profile image
Douglas Parsons Author

Else being borrowed from real life is an interesting point - remember that the language we use every day is often incredibly ambiguous and driven hugely by context.

My wife said: "Please go to the store and buy a carton of milk and if they have eggs, get six." I came back with 6 cartons of milk She said, "why in the hell did you buy six cartons of milk"

"They had eggs"

The rules we follow in programming aren't the same as in natural language too.

When programming, you often have to deal with vastly complex systems (compared to a very simple sentence). As a result, I'd argue removing as much ambiguity, and as much context as possible helps readability massively.

Collapse
kant2002 profile image
Andrii Kurdiumov

To answer question which you ask Cinematic.

So, to answer your own question... why 'should' a language have an 'else' statement? What's the use case that makes it mandatory?
Enter fullscreen mode Exit fullscreen mode

You most likely need else to express computations which has 2 and only 2 options. These should be probably small computations hidden in their own function.

These situations very often arise when you express business rules for example. And written in if/else fashion these business rules can be discussed with business persons without to teaching them new abstractions.

Collapse
stcinematicapp profile image
Studio Cinematic

Look, I really get what you're trying to do/say (and I was aware of the joke) but it's simply not true. In fact, to me, this yells "I really don't have enough experience, and yeah I've written some code, so here's what I think".

I've been programming for 23 years, and "else" is a very useful construct. In fact, it's more than a "must".

Thread Thread
f6adt profile image
f6ADt • Edited

if/else:

function foo1(value: string | undefined) {
    if (!value) {
        // ... multiline processing ...
        throw Error()
    } else {
        ...
    }
}
Enter fullscreen mode Exit fullscreen mode

if with short-circuit:

function foo2(value: string | undefined) {
    if (!value) {
        // ... multiline processing ...
        throw Error()
    }
    ... happy path
}
Enter fullscreen mode Exit fullscreen mode

In what way is the first alternative a "must"? These snippets use TypeScript as an example - the compiler will even narrow value to string in the line after the if statement for you, leveraging control flow analysis (2nd snippet). Happy path stands out without further indention, arguably more readable. You might even count the used characters to be objective.

Thread Thread
stcinematicapp profile image
Studio Cinematic

By "must", I meant something a language should have, not that you need to use it all the time.
If after years of programming you don't see the value of "else", you may want to advance to management.

Thread Thread
dglsparsons profile image
Douglas Parsons Author

So, to answer your own question... why 'should' a language have an 'else' statement? What's the use case that makes it mandatory?

Thread Thread
stcinematicapp profile image
Studio Cinematic

Seriously, you really need to advance to management.

Thread Thread
f6adt profile image
f6ADt • Edited

Well, else is just one tool in the kit - whatever fits readability best. I personally don't exclude native language features categorically.

Expression-based functional programming languages only know if/else as ternary operator. In statement-containing languages as JavaScript, this article has a good point. Replacing else by short-circuits can improve readability in surprisingly many cases.

Instead of blowing out hot air ala "I can't believe what people write when they're bored.", "this advice you give is horrendous.", "this yells "I really don't have enough experience'" or the good old "I've been programming for <insert number here> years", I would recommend a bit more open-mindness.

Thread Thread
stcinematicapp profile image
Studio Cinematic

About "else" is just one tool in the kit - I wholeheartedly agree. I never said "always use the 'else' construct". However, to understand my grievance with this article, please look at the title.

Thread Thread
bernardwiesner profile image
Bernard Wiesner

Things you say like "I have been programming for 23 years" does not give you more credibility. Some people stay stuck and just do the same old thing without advancing, even for decades.

I agree 'else' has some use cases, but it makes your code much easier to read after you get used to not using 'else'. It also helps dealing with complex logic and reasoning about it, not having to deal with multi level nesting.

You should give it a shot after 23 years and try to stop using else. If after a week you still really dont like it you can always go back.

Thread Thread
stcinematicapp profile image
Studio Cinematic

You simply don't get it - saying "else" has some use cases - where do you people come up with this stuff. You really think I'm gonna give up using "else" based on someone's opinion? No thanks. I simply go with "else is a tool in the programmer's box, I use it when it makes sense". I don't deal in absolutes.

But yeah, feel free to stop using else, and then maybe after 5 years, re-read your code AND try to maintain it. Then we'll talk.