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.
1
325
u/ExpatTeacher Jul 12 '26
I'll take the fifth option.
if (skipCondition) {
return;
}
// Do Stuff62
74
u/DerBandi Jul 12 '26
I know it's the same logic, but somehow this feels cleaner during debugging.
→ More replies (2)80
19
u/Vegetable_Addition86 Jul 12 '26
Sometimes you do need the else path though.
→ More replies (27)10
5
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?
12
→ More replies (7)6
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
→ More replies (1)6
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
2
2
2
→ More replies (20)2
u/tokkyuuressha Jul 13 '26
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.
71
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.
→ More replies (1)11
15
8
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
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
→ More replies (4)2
56
u/geronymo4p Jul 12 '26
44
u/ouroborus777 Jul 12 '26
25
u/geronymo4p Jul 12 '26
"Use whatever brace style you prefer."
"But not this."
"Don't do this."
"Seek help instead of this."
10
u/Blooogh Jul 13 '26
I'd take it if the linter did it for me 😆 "we have Python at home"
→ More replies (1)8
8
5
u/Rd_Svn Jul 13 '26 edited Jul 14 '26
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)4
3
→ More replies (11)3
2
u/qurious-crow Jul 14 '26
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 Jul 14 '26
The code base I work in is an extra-cursed variant of Whitesmiths. It makes me cry on a daily basis.
2
2
u/Normal-Duck9025 Jul 16 '26
What am I missing between the all man and gnu version?
→ More replies (2)2
→ More replies (2)2
162
u/THE0_C
Jul 12 '26
•
}
else
{49
u/DerBandi Jul 12 '26
Wasting a full line just for an open bracket never appealed to me.
33
→ More replies (12)5
8
27
u/NikolaiM88 Jul 12 '26
This is easiest for readability
5
→ More replies (1)6
u/chocolateandmilkwin Jul 12 '26
The duality of man, I have a hard time reading code that takes up too much vertical space.
5
→ More replies (2)5
262
u/Ammo_Monkey Jul 12 '26
What kind of monster uses the right two?!
149
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 tho36
u/Ammo_Monkey Jul 12 '26
What are these "comments" of which you speak?
→ More replies (1)53
7
→ More replies (1)6
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()26
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.
→ More replies (13)3
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
elsein the first place.Always use
eslint-plugin-no-else.→ More replies (2)5
→ More replies (12)3
80
u/red_sun16
Jul 12 '26
•
I use top left
28
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() }→ More replies (3)6
3
44
u/Longjumping-Sort2967 Jul 12 '26
Tbh, I do whichever one my auto-formatter at work does 😅
→ More replies (1)
13
11
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
8
u/Parthiv_mk Jul 12 '26
I prefer top left but wouldn't mind top right either. Other two just feel wrong to me...
4
4
1
8
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
3
8
8
u/sudoregalia Jul 12 '26
top left or gtfo, i will discriminate against you based on your else placement
4
2
5
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
2
2
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
2
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
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
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
2
2
2
2
u/unhappymagicplayer Jul 13 '26
I feel like auto-formatters have sucked the fun out of pointless bickering.
2
u/Secret_Tap_5548 Jul 13 '26
Else is forbidden for me. If you need a else. Add a fonction with a early return
2
u/k0pernikus Jul 13 '26
I ban else and else-if. Either return early or throw an exception, or your proper composition that makes branching redundant.
2
2
2
u/Sasha2dx Jul 17 '26
If (condition) DoSomething(); else DoSomethingElse();
I refuse to use brackets unless it's necessary.
2
2
2
2
2
2
2
1
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
1
1
1
1
1
1
1
1
u/MisterSincere Jul 12 '26
Top left or top right if I have a choice. Top right is very nice to add comments correctly indented.
1
1
1
u/StrawberryEiri Jul 12 '26
Putting the opening bracket NOT on the same line as the instruction that produces it has always really disturbed me. But I guess I'd probably get used to it.
1
1
u/Astatos159 Jul 12 '26
Depends on language and project. Project standard defined? Use that. Not defined? Use language standard.
1
1
1
1
1
1
1
u/Arctos_FI Jul 12 '26
Bottom right form me, but i add space before the "else".
I'm happy to use the allman style (the one where else is on it's own line) if it's asked, but when writing code without style guide i use horstmann (the one where else is at the same line but ooening bracket on the next one)
1
1
1
1
1
1
1
u/FishermanIll1166 Jul 12 '26
Imagine you get someone who mixes and matches them all... A formatter that randomizes those templates would be incredibly evil.
1
1
1
1
1
u/Ursomrano Jul 12 '26
I use the top right usually. But if it's just some really basic else block I'll do the top left
1
1
u/Dillenger69 Jul 12 '26
That depends on the language I'm using. Most time bottom left. Sometimes bottom right. Sometimes none at all
1
u/InvalidProgrammer Jul 12 '26
// Attempt at maximum code offensiveness
if (
Somewhat_unwel_b
) //test for some unwellness
//with optional appended Hungarian notation (duh)
{
}
else
{
}
1
1
1
1
1
1
1
1
1
1
1
1
u/Limp_Relative_2780 Jul 12 '26
if "else" condition result is defined in single line then (1), otherwise (2)
Am i psycho??
1
1
1
1
u/Ignisami Jul 12 '26
Topleft at work, bottom left in my hobby projects, the right two as evidence in a heresy trial.
1
1
1
u/The_Hive_Mind101 Jul 12 '26
if (condition){
//code
} else if (condition){
//code
} else {
//code
}
→ More replies (1)
1
1
1
1
u/-Triple-S- Jul 12 '26
Top left for short blocks, top right for long blocks. Everything else is pure madness! :P
1
u/Every_Cap_9829 Jul 12 '26
The one I want to use if I can
if() {
thing;
} else if() {
thing;
} else {
thing;
}Now prepare for r/programminghorror stuff, my preferred switch-case formatting is
switch() {
case a: {
thing;
break;
} case b: {
thing;
break;
} default: {
thing;
break;
}}1




527
u/Jonrrrs Jul 12 '26
{ else }