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?
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
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.
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.
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.
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.
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
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.
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)
// Attempt at maximum code offensiveness
if (
Somewhat_unwel_b
) //test for some unwellness
//with optional appended Hungarian notation (duh)
{
}
else
{
}
527
u/Jonrrrs Jul 12 '26
{ else }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.
325
u/ExpatTeacher Jul 12 '26
I'll take the fifth option.
71
u/SysGh_st Jul 12 '26
Upper right one.
Yes, I'm a monster.
56
u/geronymo4p Jul 12 '26
162
u/THE0_C Jul 12 '26 •
262
u/Ammo_Monkey Jul 12 '26
What kind of monster uses the right two?!
80
u/red_sun16 Jul 12 '26 •
I use top left
44
u/Longjumping-Sort2967 Jul 12 '26
Tbh, I do whichever one my auto-formatter at work does 😅
13
u/Saadullahkhan3 Jul 12 '26
🗣️ "Actually I code in Python"
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
u/MinosAristos Jul 12 '26
They're all ugly imo
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
u/Sufficient_Risk_8127 Jul 12 '26
the first one is the only correct answer
4
u/spenpal_dev Jul 12 '26
Whatever Prettier does
1
u/Fearless_Law3460 Jul 18 '26
i dont use braces, guess which dev i'm???
8
u/theGaido Jul 12 '26
It depends:
If it's not much code I use one line:
But for longer code I do soemthing like this
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.
3
u/Choice_Supermarket_4 Jul 12 '26
I like to mix it up in the same file to annoy PR reviewers.
3
u/purpuric Jul 12 '26
top right (:
8
u/Material_Pea1820 Jul 12 '26
}
else
{
8
u/sudoregalia Jul 12 '26
top left or gtfo, i will discriminate against you based on your else placement
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
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
It may seem strange, but I like that it's one block.
elseadds 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
iffalseififtruefails, 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/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/CrazyCommenter Jul 12 '26
Yes
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 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
u/kartblanch Jul 14 '26
}
else
{
Is the only way.
2
u/PrairiePuppies Jul 17 '26
Top left.
2
u/Sasha2dx Jul 17 '26
If (condition) DoSomething(); else DoSomethingElse();
I refuse to use brackets unless it's necessary.
2
u/dourix22 Jul 18 '26
{ else {
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.
2
u/West-Surround-8857 Jul 12 '26
Bottom left
2
u/Sol_Nephis Jul 12 '26
Bottom left
2
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/Tintoverde Jul 12 '26
All 4
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
1
u/ConFroDog Jul 12 '26
Idk let me ask Claude
1
u/iamwillp Jul 12 '26
Programming in Go you have to do top left
1
u/Friday_Lucas Jul 12 '26
Return
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
u/horenso05 Jul 12 '26
I press save and everything formats. I don't care, whatever the default is.
1
u/bigorangemachine Jul 12 '26
else {}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
u/jack-of-some Jul 12 '26
clang.format
1
u/Astatos159 Jul 12 '26
Depends on language and project. Project standard defined? Use that. Not defined? Use language standard.
1
u/fosf0r Jul 12 '26
Top left
1
u/Flame77ofc Jul 12 '26
the first one from the top left
1
u/namotous Jul 12 '26
Whatever my team uses
1
u/cipherSyntax1 Jul 12 '26
} else{
1
u/Diarminator Jul 12 '26
all
1
u/Amro003 Jul 12 '26
i use top left and top right, depending on the language itself.
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
u/s0me0ne_5 Jul 12 '26
Top left
1
u/Living_The_Dream75 Jul 12 '26
1 is the cleanest way imo
1
u/deltadstroyer Jul 12 '26
bottom left
1
u/LegalVegetable Jul 12 '26
} {
else
1
u/cpt_futtbucker Jul 12 '26
Bottom left. Force of habit
1
u/SuperMichieeee Jul 12 '26
who tf uses the fourth one?
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
u/freya584 Jul 12 '26
anything but bottom left scares me
1
u/kantank-r-us Jul 12 '26
Three
1
u/KennyMcCormickIrl Jul 12 '26
Top left ofc
1
u/DerBandi Jul 12 '26
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
u/HalifaxRoad Jul 12 '26
-1,-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
1
u/unknownemoji Jul 12 '26
I always use a switch statement.
1
u/WasteRose Jul 12 '26
1 ftw!
1
u/exxxxkc Jul 12 '26
i use all bc i never tidy up the code
1
u/JPgamersmines150 Jul 12 '26
else:
1
u/R3D3-1 Jul 12 '26
1
u/ElBarbas Jul 12 '26
}else{
nobody reads my code anyway
1
u/TheFlowerOfDarkness Jul 12 '26
if ; else ;
1
u/No-Newspaper8619 Jul 12 '26
else if (C)
elif (Python)
elseif (Lua)
1
u/MilkImpossible4192 Jul 12 '26
[ -> aight , -> wrong ][ not condition] ()1
u/electronic_reasons Jul 12 '26
Whatever the company wants.
1
u/dashinyou69 Jul 12 '26
2nd
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
u/Simple-Olive895 Jul 12 '26
1 or what ever the linter says.
1
u/why_so_sergious Jul 12 '26
2. because you can easily comment an else block
1
u/MFisherIT Jul 12 '26
Impress me, can you name the styles used?
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
u/ChaosCrafter908 Jul 12 '26
if(condition){fahh();} else{faah();}1
u/The_Hive_Mind101 Jul 12 '26
if (condition){
//code
} else if (condition){
//code
} else {
//code
}
1
u/Richard2468 Jul 12 '26
if() return a;
if() return b;
return c;
1
u/ArmNo7463 Jul 12 '26
I am whatever Claude decides these days.
1
u/SceneOutside4377 Jul 12 '26
if (thing) {
do_thing
do_other_thing
} else {
do_thing_3
}
So, option 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
Now prepare for r/programminghorror stuff, my preferred switch-case formatting is
1
u/Gigibesi Jul 12 '26
uuhhh let's see...
if condition then code else code end1
u/IYYpDFqeNq0JdiHwyo6L Jul 12 '26
Whatever rustfmt does
1
u/potato-dranik Jul 12 '26
} else {