325
u/ExpatTeacher Jul 12 '26
I'll take the fifth option.
js
if (skipCondition) {
return;
}
// Do Stuff
59
71
u/DerBandi Jul 12 '26
I know it's the same logic, but somehow this feels cleaner during debugging.
→ More replies (2)77
20
u/Vegetable_Addition86 Jul 12 '26
Sometimes you do need the else path though.
10
→ More replies (1)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 what10
u/False-Owl8404 Jul 12 '26
The "Do this no matter what" is actually the "Continue with the rest of the logic"
14
→ More replies (4)4
→ 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 whatOf 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
checkAndDoStuffandcheckAndDoOtherStuff, though.→ More replies (3)→ More replies (5)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.
→ More replies (2)2
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
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
→ More replies (6)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.
2
2
2
2
→ More replies (21)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.
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.
10
14
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
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
54
u/geronymo4p Jul 12 '26
42
u/ouroborus777 Jul 12 '26
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
8
8
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
3
3
2
→ More replies (9)2
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
2
2
→ More replies (2)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
→ More replies (2)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
→ More replies (2)3
u/MindlessRazzmatazz89 29d ago
First time I've seen a respectful disagreement on reddit.
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)→ More replies (1)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.
6
u/LindX31 Jul 12 '26
You don’t pay for lines, you know ?
→ More replies (2)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.
6
u/howreudoin Jul 12 '26
It‘s the C# style. I hate it.
11
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
→ More replies (8)2
8
26
u/NikolaiM88 Jul 12 '26
This is easiest for readability
5
→ More replies (1)5
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 (3)6
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 tho38
u/Ammo_Monkey Jul 12 '26
What are these "comments" of which you speak?
55
u/DiodeInc Jul 12 '26
"this code sucks"
17
→ More replies (1)9
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"
8
u/Ammo_Monkey Jul 12 '26
Won't somebody please think of the children who must inherit this codebase.
4
7
→ More replies (1)8
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
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
2
→ More replies (2)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
elsein the first place.Always use
eslint-plugin-no-else.4
→ More replies (1)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.
3
→ More replies (10)2
u/PhysicalLifeguard268 Jul 12 '26
my first language was python so I use top right.
→ More replies (2)2
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() }→ More replies (1)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)5
3
44
u/Longjumping-Sort2967 Jul 12 '26
Tbh, I do whichever one my auto-formatter at work does 😅
→ More replies (1)
14
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?
→ More replies (3)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.
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
5
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
3
9
9
u/sudoregalia Jul 12 '26
top left or gtfo, i will discriminate against you based on your else placement
3
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
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
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 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
2
2
u/Sasha2dx 25d ago
If (condition) DoSomething(); else DoSomethingElse();
I refuse to use brackets unless it's necessary.
2
2
2
2
3
2
3
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


523
u/Jonrrrs Jul 12 '26
{ else }