DEV Community

loading...

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

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.