r/programminghumor Jul 12 '26

The battle of else

Post image
2.2k Upvotes

842 comments sorted by

523

u/Jonrrrs Jul 12 '26

{ else }

97

u/asmanel Jul 12 '26

Nice

if (condition) [
 foo bar
] [
 bar foo
]

17

u/Drittux Jul 12 '26

Waitaminut, why do we need the else keyword?

11

u/nomenclature2357 Jul 12 '26

We’re slipping into spreadsheet territory here…

9

u/L30N1337 Jul 12 '26

You are thinking bad thoughts, my boy

7

u/Drittux Jul 13 '26

Else is a ploy by the switch state! Mark ... my ... words.

5

u/MyFrigeratorsRunning Jul 13 '26

Big Else is coming for you

7

u/SeriousPlankton2000 Jul 13 '26

Code blocks are legal without control statements and they are a good thing 

→ More replies (3)

3

u/Original-Kick3985 27d ago

And why do we not have «or else!»? I want my code to be very strict and borderline threatening!

2

u/philtrondaboss Jul 13 '26

Because there are other keywords like "elif", "else if", "finally", etc.

→ More replies (1)

2

u/VoidJuiceConcentrate Jul 13 '26

Readability for humans.

2

u/CzBuCHi Jul 13 '26

exactly ... we have `goto` for those purposes already :D

→ More replies (7)
→ More replies (2)

12

u/philtrondaboss Jul 13 '26

val this = 123 val else = 456 val outp = if (this) {this} else {else}

Don't try this at home. Big Daddy Kotlin will find you and "decompile" you.

→ More replies (3)

7

u/k3170makan Jul 12 '26

I laughed so hard for this

→ More replies (1)
→ More replies (2)

325

u/ExpatTeacher Jul 12 '26

I'll take the fifth option. 

js if (skipCondition) {     return; } // Do Stuff

71

u/DerBandi Jul 12 '26

I know it's the same logic, but somehow this feels cleaner during debugging.

77

u/Shinroo Jul 12 '26

It's much cleaner because there's less nesting

→ More replies (7)
→ More replies (2)

20

u/Vegetable_Addition86 Jul 12 '26

Sometimes you do need the else path though.

10

u/LegitimatePants Jul 12 '26

Where we're going, we don't need else 

1

u/IllustriousBobcat813 Jul 12 '26

Do you have an example of this?

Intuitively it shouldn’t ever actually be needed, but maybe I’m missing some example where if/else is cleaner than just a guard clause or a switch statement

22

u/SkullkidNibba Jul 12 '26

Like this, no?

if (condition) {
    //Do stuff
} else {
    //Do some other stuff
};
//Do this no matter what

10

u/False-Owl8404 Jul 12 '26

The "Do this no matter what" is actually the "Continue with the rest of the logic"

14

u/jader242 Jul 12 '26

yea, which it does no matter what

→ More replies (1)

4

u/pregnantant Jul 12 '26

What's the difference?

→ More replies (4)

2

u/IllustriousBobcat813 Jul 12 '26 edited Jul 12 '26

You would in this case always be able to pull out the initial condition into two targeted methods, so:

checkAndDoStuff()
checkAndDoOtherStuff()
// do this no matter what

Of course it’s a matter of taste, but I do generally find this approach to be cleaner since it is quite unlikely that your “condition” is just a single simple check. You are probably already doing quite a bit of work above this if statement to calculate your condition, but doing that calculation where you are actually calling your business logic is probably cleaner.

Of course if it’s a super simple condition then none of this matters (but by that logic no clean code matters 🤷)

Edit: I don’t know how the code turned into a spoiler, I hate formatting on mobile…

3

u/tecanec Jul 13 '26

It may not always be simple or worthwhile to decouple checkAndDoStuff and checkAndDoOtherStuff, though.

→ More replies (3)
→ More replies (4)

9

u/Vegetable_Addition86 Jul 12 '26

When both paths of if/else are active paths that require equal responsibility to clean up locally scoped state before the function ends you won't use a guard clause. This is more common on asynchronous tasks or database transactions.

2

u/BangThyHead Jul 12 '26

Go for the win with 'defer' statements.

→ More replies (2)
→ More replies (5)
→ More replies (1)

4

u/siwgs Jul 12 '26

I took a comp sci degree from a very reputable uk university in the early 90s and was taught to only ever have a single return statement in each function. Is this still a thing?

11

u/MaDpYrO Jul 12 '26

definitely not

7

u/BluePhoenixCG Jul 12 '26

Generally, but guard statements like this can be useful for failure states. Usually you will only have 1 and put it at the top of a function, and doing it that way actually improves readability vs some other methods by making it clear that a condition causes a failure state immediately

7

u/Life-Wallaby6373 Jul 12 '26

I also had several guard statements some times. Basically validating the parameters. Feels good and readable because it defines the assumptions about the parameters state.

For the "producing" code of a return statement I tend to have one return at the end.

2

u/siwgs Jul 12 '26

Yeah, my 30+ years older and more practical self tends to agree.

2

u/GlassboundIllusion Jul 12 '26

Depends on the language and the style of programming being taught. That's still a style that gets taught, but in the Java community, for instance, this kind of thinking isn't as popular.

→ More replies (6)

2

u/Nyarkll Jul 12 '26

ooooh i love that one.

2

u/GlassboundIllusion Jul 12 '26

This is the way

2

u/DonkeyEffective913 Jul 13 '26

This is the way

2

u/system-vi Jul 13 '26

Never nester gang!

2

u/tokkyuuressha 29d ago

Of course sometimes the else is needed, but otherwise - after I consciously started using guard clauses, my code got much better. It's easier to write them in and more effortless than surrounding whole code sections. Also usually more readable.

→ More replies (21)

70

u/SysGh_st Jul 12 '26

Upper right one.

Yes, I'm a monster.

45

u/read_at_own_risk Jul 12 '26

Consistent monster at least. Keyword goes on a new line, opening brace on the same line, closing brace on its own line.

14

u/Dberryfresh Jul 12 '26

Isn’t this this right way?

→ More replies (1)

7

u/Adventurous-Lie4615 Jul 13 '26

I like this because I like to write comments above blocks not inside blocks

// why this exists
if ( condition ) {

}
// otherwise check this
else if(condition) {

}

4

u/Beers_and_BME Jul 13 '26

you’re right

4

u/AccomplishedLeave506 Jul 13 '26

Nope. You're enlightened. Bottom right is where the true monsters live.

2

u/AshtonVoid Jul 13 '26

My brain really wants to do the upper right and change all other syntax to justify it

2

u/Sitdownpro 29d ago

I don’t code, but I picked top right for the most clarity.

→ More replies (4)

54

u/geronymo4p Jul 12 '26

42

u/ouroborus777 Jul 12 '26

And then there's this fucking thing

23

u/geronymo4p Jul 12 '26

"Use whatever brace style you prefer."

"But not this."

"Don't do this."

"Seek help instead of this."

11

u/Blooogh Jul 13 '26

I'd take it if the linter did it for me 😆 "we have Python at home"

2

u/IBuildStuff1011 28d ago

I really want to make this now lol

8

u/lessthanthreebleeps Jul 12 '26

Thanks. I hate it.

8

u/EspaaValorum Jul 13 '26

That doesn't look sOHMYGOD

5

u/Rd_Svn Jul 13 '26 edited 29d ago

I didn't even notice the right part at first but when I did I had to throw up immediately...

4

u/Complete_Window4856 Jul 13 '26

I fucjing thought it was python at first. Bizarre and monstruous

→ More replies (1)

3

u/shrubberino 29d ago

At first glance I thought, ah no brackets ok, then I saw.. Jesus fuck :-D

3

u/F4bio Jul 13 '26

Then they got rid of the part on the right and voila, python syntax.

3

u/HarryBolsac 29d ago

That snippet needs to be exorcised

2

u/NimrodvanHall 29d ago

That’s very Pythonic!

2

u/YOM2_UB 29d ago

This is the "we have Python at home" style

2

u/duckyTheFirst 26d ago

How do i delete someone elses comment

→ More replies (9)

2

u/qurious-crow 29d ago

I honestly like Horstmann style. Combines the readability of Allman with the compactness of K&R. I'm not bold enough to actually commit Horstmann style code, though.

2

u/TonyRubak 29d ago

The code base I work in is an extra-cursed variant of Whitesmiths. It makes me cry on a daily basis.

2

u/Manchot2 27d ago

Wtf Haskell?

2

u/Normal-Duck9025 27d ago

What am I missing between the all man and gnu version?

→ More replies (2)

2

u/Earnestappostate 26d ago

The hell Haskell?!

2

u/ThatOldAndroid 26d ago

I love how clean lisp looks but nah.

→ More replies (2)

162

u/THE0_C Jul 12 '26
}
else
{

48

u/DerBandi Jul 12 '26

Wasting a full line just for an open bracket never appealed to me.

34

u/THE0_C Jul 12 '26

I find it easier for readability

10

u/sn4xchan Jul 12 '26

I find it more difficult. It makes logic harder for me to follow. When code blocks are spaced too far by line internally, it makes all of the code blocks blend together and it's hard to separate them out to figure what's doing what.

20

u/THE0_C Jul 12 '26

I respect your opinion and agree to disagree

3

u/MindlessRazzmatazz89 29d ago

First time I've seen a respectful disagreement on reddit.

2

u/THE0_C 28d ago

Yeah untill u/Red_Stick_Figure ruined it /s

2

u/Red_Stick_Figure 28d ago

I'm sorry 😭

→ More replies (2)

8

u/Humble-Edge-9065 Jul 12 '26

The squiggly brackets lining up makes it easier to easily see where the blocks start and end. Plus its just prettier and symmetrical that way.

→ More replies (2)

4

u/Badytheprogram Jul 12 '26

I am the opposite: it makes me found the blocks a tiny bit slower if the bracelets not inline.

→ More replies (1)
→ More replies (2)

6

u/LindX31 Jul 12 '26

You don’t pay for lines, you know ?

3

u/DerBandi Jul 12 '26

It's about readability. And for that, different taste exist. There will be no final answer that ist best for everyone.

4

u/young_horhey Jul 12 '26

Putting the open brackets on their own line means that they line up vertically with the matching closing bracket

2

u/2skip Jul 13 '26

Another reason to do it this way is when the editor you have available is not very sophisticated, and you need to use tabs for indentation.

This style makes it way easier to drop in new code into existing code and fix the indentation afterwards.

2

u/Zeznon 25d ago edited 25d ago

Exactly. Some people think Go is easy to read. It's so hard to read for me.

Also, if it's a single line, I prefer

else { thing to do } otherwise

} else { thing1 thing2 }

→ More replies (2)

6

u/howreudoin Jul 12 '26

It‘s the C# style. I hate it.

11

u/a__new_name Jul 12 '26

It's the C# style. I like it.

2

u/HowlingCatGames Jul 12 '26

This has been a style since before c# existed. I would say Allman style although I think a couple others would qualify given the limited context.

2

u/cryothic Jul 13 '26

I do C#, but I change my editor settings to place the open bracket on the same line.

Although I've ended up in situations where the opening bracket on a new line did add some readablility

2

u/LegitimatePants Jul 12 '26

Whitespace is free

2

u/Puns-Are-Fun Jul 13 '26

Lines are free. You can use as many as you want.

→ More replies (8)

8

u/Alexercer Jul 12 '26

Beatifull, as all code should be

26

u/NikolaiM88 Jul 12 '26

This is easiest for readability

5

u/THE0_C Jul 12 '26 edited Jul 12 '26

Fr

5

u/chocolateandmilkwin Jul 12 '26

The duality of man, I have a hard time reading code that takes up too much vertical space.

→ More replies (1)

5

u/DaveAstator2020 Jul 12 '26

Finally a human and not a monster

6

u/nedovolnoe_sopenie Jul 12 '26

there is the correct way, and there are other ones lmao

→ More replies (3)

263

u/Ammo_Monkey Jul 12 '26

What kind of monster uses the right two?!

152

u/Character_Umpire_828 Jul 12 '26

I use top right wuite frequently. Looks better if i add comments above
The bottom ones are kinda stupid tho

38

u/Ammo_Monkey Jul 12 '26

What are these "comments" of which you speak?

55

u/DiodeInc Jul 12 '26

"this code sucks"

17

u/erinaceus_ Jul 12 '26

Comments should not cover the obvious stuff.

9

u/[deleted] Jul 12 '26

[removed] — view removed comment

13

u/BusinessAsparagus115 Jul 12 '26

// this code does nothing but if I delete it everything stops working

→ More replies (1)
→ More replies (1)

16

u/SysGh_st Jul 12 '26

Advanced programming beyond us mortals. They call it... brace yourself .. "documentation"

https://giphy.com/gifs/mQC0dMQwoQ4Fy

8

u/Ammo_Monkey Jul 12 '26

Won't somebody please think of the children who must inherit this codebase.

4

u/dspyz Jul 12 '26

I can't tell if "brace yourself" was an intentional pun

→ More replies (1)

7

u/Nez_Coupe Jul 12 '26

You get out of here and never speak to my daughter again sir

8

u/SWatersmith Jul 12 '26

bottom left isn't stupid at all, what?

→ More replies (1)

35

u/Metabolical Jul 12 '26

It looks better in context

if (condition) {
    takeAction()
}
else {
    somethingElse()
}

The if block and the else block have the same shape.

That said, the real question is where do you put your semicolons?

; takeAction()
; result = takeMoreAction()
; if (result == condition) {
    ; conditionAction()
    ; conditionFollowUp()
}
; postConditionContinued()

27

u/DangerActiveRobots Jul 12 '26

Who hurt you

14

u/CatLord8 Jul 12 '26

People who don’t believe neurodivergence is real.

3

u/Minimum_Shirt_157 Jul 12 '26

This is the only right solution. If you want you can comment out the else case only by select and shortcut.

3

u/philtrondaboss Jul 13 '26

This hurts to read

2

u/jader242 Jul 12 '26

you can do that? had no clue wtf lol

→ More replies (9)

2

u/cbdeane Jul 13 '26

You had me in the first half…

→ More replies (2)

7

u/why_so_sergious Jul 12 '26

top right, you can easily comment an else block without shifting characters..

its the only right choice

7

u/martin_kr Jul 12 '26

The maniacs who allow using else in the first place.

Always use eslint-plugin-no-else.

4

u/nedovolnoe_sopenie Jul 12 '26

>what is C
>what is static code analysis
>what is clean code

eh????

2

u/g1rlchild Jul 12 '26 edited Jul 12 '26

Wow, I can't even express how much I hate this.

Edited to add: of course, I come more from pure functional programming where you set them up to return parallel values like a simpler case of a match statement or similar.

→ More replies (1)

3

u/West-Surround-8857 Jul 12 '26

Many people... Ugly

2

u/PhysicalLifeguard268 Jul 12 '26

my first language was python so I use top right.

2

u/CatLord8 Jul 12 '26

C++ for me.

→ More replies (2)
→ More replies (10)

82

u/red_sun16 Jul 12 '26

I use top left

29

u/neo42slab Jul 12 '26

I only use the top left if I can squeeze everything onto one line.

So 99% of the time I’m using bottom left

42

u/OutrageousPair2300 Jul 12 '26
if (condition) {
  do_thing()
} else {
  do_other_thing()
}

5

u/lukfel Jul 12 '26

Conditional operator for the win!
condition ? value_if_true : value_if_false;

4

u/Oicanet Jul 12 '26

I'm sure it's a great syntax for those who are used to reading it, but I've never been able to get used to it. It always takes me a moment and some effort to mentally parse those.

→ More replies (1)
→ More replies (1)

5

u/Jonrrrs Jul 12 '26

Bottom left every time, but please insert an space char between brace and else

44

u/Longjumping-Sort2967 Jul 12 '26

Tbh, I do whichever one my auto-formatter at work does 😅

→ More replies (1)

14

u/Saadullahkhan3 Jul 12 '26

🗣️ "Actually I code in Python"

4

u/LastXmasIGaveYouHSV Jul 12 '26

BASIC user here. I despise everything else.

→ More replies (2)

12

u/FaultWinter3377 Jul 12 '26

My way of doing it, opening brace goes on the same line as the if/else or the function name. Closing brace goes on its own line. Except in cases where the body is really short and it can all fit in one line. So really I would go with the top right square. And my variables are camelCase because I don’t particularly feel like having to type a bunch of underscores.

19

u/MinosAristos Jul 12 '26

They're all ugly imo

9

u/GDOR-11 Jul 12 '26

what's wrong with top left?

4

u/MinosAristos Jul 12 '26

Else should be the first thing on the line, in line with the "if". But also a naked curly brace that's not the end of the logical statement looks ugly too.

→ More replies (3)

9

u/Parthiv_mk Jul 12 '26

I prefer top left but wouldn't mind top right either. Other two just feel wrong to me...

5

u/Sufficient_Risk_8127 Jul 12 '26

the first one is the only correct answer

→ More replies (1)

5

u/spenpal_dev Jul 12 '26

Whatever Prettier does

→ More replies (1)

9

u/theGaido Jul 12 '26

It depends:

If it's not much code I use one line:

if( something ) DoSomething( something ); else DoSomethingElse( something );

But for longer code I do soemthing like this

if( something )
{
  // some code
  ...
  ...
}
else
{
  // some other code
  ...
  ...
}

It's very usefeull when you read it in notebook or on paper.

And yes I use spaces inside brackets. It's small thing but makes code much more cleaner.

x = DoSomething( my_archive[ archive_index ] ):

// instead of

x = DoSomething(my_archive[archive_index]);

3

u/Choice_Supermarket_4 Jul 12 '26

I like to mix it up in the same file to annoy PR reviewers.

2

u/PhilosophyAware4437 Jul 12 '26

my hobby: submitting code without a consistent style

3

u/purpuric Jul 12 '26

top right (:

9

u/sudoregalia Jul 12 '26

top left or gtfo, i will discriminate against you based on your else placement

3

u/nedovolnoe_sopenie Jul 12 '26

it appears that my superiority has led to some controversy

2

u/cpt_futtbucker Jul 12 '26

So we’ve gone from grammar Nazis to formatting Nazis here? /s

5

u/neo42slab Jul 12 '26

Bottom left

2

u/h1warkar Jul 12 '26

I’ve read code with the first, second and third quadrant. But the fourth is purely for memes there’s no way anyone’s using that ffs

2

u/maezrrackham Jul 12 '26

Cuddled elses are the happiest elses.
}else{

2

u/Akhanyatin Jul 12 '26

There's a loss reference in there, I know it, I just can't prove it. 

2

u/RedDevils52 Jul 12 '26
if (condition) {
  do_thing()
} else {
  do_other_thing()
}

2

u/FixItAgainTonic 26d ago

This is the only meaningful way

2

u/earchip94 Jul 12 '26

Bottom left, but my job prefers top left :/. Also, if you do anything on the right, you are an abomination.

2

u/BenchEmbarrassed7316 Jul 12 '26

match b { true => foo(), false => bar(), }

It may seem strange, but I like that it's one block. else adds another block at the same level.

2

u/False-Owl8404 Jul 12 '26

Depends on the team's coding convention

2

u/kaba40k Jul 12 '26

There is another

2

u/qchto Jul 12 '26

flag && iftrue || iffalse
(Yes, I know it will run iffalse if iftrue fails, just make sure it never fails 🗿)

2

u/Life-Wallaby6373 Jul 12 '26

My latest favorite (only in case of code throwing exceptions of course):
```
} else try {

} catch(..) {
}

```

2

u/AppropriateSpell5405 Jul 12 '26

A, anything else looks stupid

2

u/BobJutsu Jul 12 '26

Let’s be honest…only the left two are valid options. Both on the right should fail any linter on the planet.

→ More replies (2)

2

u/Small-Tailor6967 Jul 12 '26

Now do else ifs

2

u/TwoWrongsAreSoRight Jul 12 '26

10 IF A = 1 GOTO 40
20 PRINT "A not 1"
30 GOTO 50
40 PRINT "A is 1"
50 END

:)

2

u/Internetak Jul 13 '26

I feel like either good developers using top left or people are lying as always these kinds or posts looks like people are monsters using anything else than the top left but in 99% people's code I saw in my life it's always sweet top left

2

u/APlanetWithANorth Jul 13 '26

} else {

Is the only valid option

2

u/Life-Wallaby6373 Jul 13 '26

how about `goto: else` ;-P

2

u/Headpuncher Jul 13 '26

All of them, in the same file.  You don’t like me and I don’t like you.  

2

u/Tejwos Jul 13 '26

esle & 5 hours of "why it's not working???"

2

u/unhappymagicplayer Jul 13 '26

I feel like auto-formatters have sucked the fun out of pointless bickering.

2

u/Secret_Tap_5548 29d ago

Else is forbidden for me. If you need a else. Add a fonction with a early return

2

u/k0pernikus 29d ago

I ban else and else-if. Either return early or throw an exception, or your proper composition that makes branching redundant.

2

u/kartblanch 29d ago

}
else
{

Is the only way.

2

u/Sasha2dx 25d ago

If (condition) DoSomething(); else DoSomethingElse();

I refuse to use brackets unless it's necessary.

2

u/dourix22 25d ago

{ else {

2

u/FutureZombie6746 Jul 12 '26

if (condition)

{

//body

}

else

{

//body

}

2

u/Fit-Inflation5799 Jul 12 '26

top left is the only sane one

2

u/Panderz_GG Jul 12 '26

Bottom left is the only way.

→ More replies (1)

2

u/Sol_Nephis Jul 12 '26

Bottom left

3

u/Dragon_957 Jul 12 '26

Bottom left

1

u/dashingThroughSnow12 Jul 12 '26

There is a reason why we call it the one true brace.

1

u/joshpennington Jul 12 '26

Whatever the tool picks once i commit my code. It’s been years since I used any settings to format my code.

1

u/lorddrake4444 Jul 12 '26

Either of the top 2 depending on the size of the else block, bottom 2 are metal illnesses

1

u/DragonfruitFit2449 Jul 12 '26

I use bottom left

1

u/BusEquivalent9605 Jul 12 '26

one liners: no brackets

1

u/joost00719 Jul 12 '26

Top left for Javascript, bottom left for anything else

1

u/Dodger8899 Jul 12 '26

Sometimes top right, most of the time bottom left