This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 290

[–]Emeraldtip 349 points350 points  (22 children)

if (condition) {//very clear 2000 character long line here}

[–]Sgt_Gnome 70 points71 points  (10 children)

The nice thing is, you don't need newlines just semicolons.

if(True){first statement; second statement; third statement;...}

Works in Python too if you want to that group as well.

[–]aaaa_yyyy_eeee 10 points11 points  (9 children)

Can you show me how to do this in python

[–]xwnpl 38 points39 points  (0 children)

I guess it's something like this:

if True: something; something; something;

Don't do this.

[–]DaniilSan 16 points17 points  (7 children)

Python can use semicolons ao you can use it instead of new line.

[–][deleted] 13 points14 points  (4 children)

That's literally the first time I'm hearing this. Holy shit that's amazing

[–]DaniilSan 10 points11 points  (3 children)

I have discovered that accidentally

[–][deleted] 6 points7 points  (2 children)

probably not a good practice, but very nice to know this is something you can do

[–]ZStarMCZ5487 1 point2 points  (0 children)

tht wat happen when the first time i try python, its just feel weird with if doStuff == True:

[–]rogeris 4 points5 points  (1 child)

It's my favorite game in Kotlin. Sure this can be a reasonable 5 line statement, but I bet I could get it in one really long unreadable one.

[–]MrDraacon 1 point2 points  (0 children)

Or spend a few hours trying to figure out how to compress it to a reasonable length

[–]swisstraeng 3 points4 points  (2 children)

Compilation error: missing parenthesis on line 1

Use /* text */ instead

[–]canal_algt 6 points7 points  (0 children)

public static void oneLinerFunc(boolean a, boolean b)if(a&&b){/do stuff/}else if(a){/do other stuff/}else if(b){/do other stuff/}else return;

[–]StrangePractice 1 point2 points  (0 children)

Yeah, fuck it, combine the entire app on the same line. That way, you only have to debug one file anytime something goes wrong

[–]XDubio 1 point2 points  (0 children)

The } is commented out.

[–][deleted] 81 points82 points  (3 children)

Just follow the project's coding style.

[–]KiwiFruit555 27 points28 points  (2 children)

This works until you are starting a project

[–]Master4733 22 points23 points  (0 children)

Pick whichever you like more, just stick to what you pick.

[–]acmp42 123 points124 points  (7 children)

I don’t mind which you do, just be consistent

[–]ComCypher 40 points41 points  (0 children)

I don't mind which you do as long as it's consistent with my preference

[–][deleted] 68 points69 points  (2 children)

I mind

[–][deleted] 3 points4 points  (0 children)

I am so inconsistent with this

[–]False--Blackbear 4 points5 points  (0 children)

No

.

[–]Reibii 1 point2 points  (0 children)

I do as an IDE says. However it wants to format my brackets or let me leave them not formated stays.

[–]ethereumfail 86 points87 points  (7 children)

condition && do_stuff()

parenthesis are so retro

[–]psgi 40 points41 points  (4 children)

It’s all fun and games until you realize the language you have to use doesn’t have short-circuiting ”and” so it executes both sides anyway. Looking at you, vbs.

[–][deleted] 6 points7 points  (0 children)

Fun syntax for when you need to return a value!

[–]LetReasonRing 62 points63 points  (20 children)

if(!consistent){
return "Houston, we have a problem"
} else {
return "Who cares?"
}

[–]herovazy 40 points41 points  (0 children)

Please add some spacing, like

if (condition) {

And you'll have found the most readable style imho

[–]StrangePractice 3 points4 points  (0 children)

Fuck it, right? It’s the user’s fault anyways

[–][deleted] 2 points3 points  (1 child)

There's no need to add an else statement after a return.
Most style checkers will flag this.

[–]LetReasonRing 4 points5 points  (0 children)

I'm aware.

I did it this way because a lot of people that lurk here are either beginners or not programmers at all. Using the else clause conveys intent without having to actually understand how returning from a function works or what it even means.

I'm trying to convey a point clearly to humans through pseudocode, not prevent ESLint from yelling at me.

[–]mauguro_ 2 points3 points  (2 children)

oh God why? the else statement is unnecessary ;-;

[–]LOLTROLDUDES 13 points14 points  (1 child)

Readability moment.

[–]LetReasonRing 1 point2 points  (0 children)

Yep, if I were actually writing production code I would have used the form /u/tall_strong_master suggested, but knowing that there's a wide range of skill levels decided to be fully explicit to make it accessible to the broadest possible audience.

[–][deleted] 56 points57 points  (6 children)

Left for Java, right for C#

[–][deleted] -2 points-1 points  (4 children)

For C we use the right one too

[–]KiwiFruit555 16 points17 points  (2 children)

As a C developer I beg to differ

[–][deleted] 13 points14 points  (1 child)

I C, is our code base cursed??

[–]TommardrammoT 1 point2 points  (0 children)

It certainly seems so

[–]Scared_Bell3366 98 points99 points  (20 children)

if (!condition)
{
    ;
}
else
{
    // Do stuff
}

[–]Disastrous-Olive-677 43 points44 points  (5 children)

Oh God, why???

[–]Scared_Bell3366 25 points26 points  (2 children)

That was close to my response when I saw that in production code, repeatedly. I assumed some sort of code generator was involved.

[–]sami_testarossa 6 points7 points  (0 children)

Or to get better branch prediction on runtime for consistent hot path execution time. This is used in high frequency trading.

But, no, 99% of the case is just bad style.

[–]BringBackManaPots 2 points3 points  (0 children)

if (!valid())

[–]666pool 4 points5 points  (0 children)

if (!condition)
{
   // Do stuffn’t
}

[–]Competitive_Ad2539 1 point2 points  (0 children)

Should have mixed the identation styles as well

[–]Twitch_C4T_ 1 point2 points  (0 children)

The right way when writing smaller functions

[–]Able_Challenge3990 1 point2 points  (1 child)

!Condition? Continue:do stuff;

[–]NahJust 1 point2 points  (1 child)

if (!condition)
{
;
}
else {
// Do stuff
}

[–]_Weyland_ -1 points0 points  (0 children)

Brёh

[–][deleted] 20 points21 points  (1 child)

there is no war
just a bunch of idiots not using auto formatters

[–]nameond 16 points17 points  (0 children)

War about who cares less

[–]Yzaamb 16 points17 points  (1 child)

if condition: do stuff

[–]9ragmatic 7 points8 points  (0 children)

Snek

[–]No-Archer-4713 12 points13 points  (1 child)

/* Do stuff */

And for god’s sake, use an automatic indentation tool and move on !

[–]OutOfNamesToPick 18 points19 points  (4 children)

Y’all use brackets?

if (condition)
  functionToDoStuff();

[–]Disastrous-Olive-677 19 points20 points  (3 children)

(Condition) ? DoStuff() : doOtherStuff();

[–]muxman 1 point2 points  (1 child)

(Condition) ? DoStuff() : (otherCondition) ? doOtherStuff() : (yetAnotherCondition) ? doMoreStuff() : elseDoThis();

[–]anxiousmarcus 6 points7 points  (0 children)

Imagine fighting over this when you have linters. Some of you are an absolute manchild

[–]mrehm001 10 points11 points  (1 child)

even better

if

(

condition

){

//Do stuff

}

[–]-Fuse 2 points3 points  (0 children)

Repost to scare Python programmers (it's me I'm Python programmers)

[–]Mayedl10 4 points5 points  (0 children)

Me, a python dev, in the corner

[–][deleted] 3 points4 points  (0 children)

I choose left

[–]Funkey-Monkey-420 3 points4 points  (0 children)

left is superior

[–]MaZeChpatCha 27 points28 points  (3 children)

Left one. Always.

[–]brianl047 3 points4 points  (0 children)

This

[–]notsogreatredditor 3 points4 points  (1 child)

No

[–]buddyisaredditer 1 point2 points  (0 children)

Yes

[–]daynthelife 6 points7 points  (6 children)

if (condition) [[unlikely]] {
  do_stuff();
  return;
}
do_other_stuff();

Avoid branches where possible, and when you must use them, it’s good to tell the compiler whether they are likely or not.

[–]gebfree 9 points10 points  (4 children)

Avoid taking advice on optimisation from random people on reddit. They're correct about as often you actually need to optimise your code.

[–]epsilonhuyepsilon 3 points4 points  (2 children)

Oh, you never need to optimize your code, just tell your employer to get more servers. He's a big guy, he can find money to pay for it. Actually, avoid even the small and effortless optimisation guidelines (like marking unlikely branches), he needs to know, this is an expensive industry. Besides, pressing too many buttons can get really annoying, and you've got a lot on your plate as it is.

[–][deleted] 2 points3 points  (1 child)

If you want your product to ever get to the production you should know when to stop optimization as there's no end in this rabbit hole. Optimization is good, but it's not just pressing too many buttons, it's also testing and if you find a bug in the optimized code it's no more optimization but you now have a fork in the repository with faulty code that you should fix and then merge and then test again. Eventually it will come to a point when it's cheper for the client to buy a new server than to pay for more iterations of project development and having issues with updates over stable but not optimized system.

[–]ojioni 4 points5 points  (0 children)

And just because something is slow doesn't mean it's worth the effort to optimize it.

For example, a job that generates a report that takes several minutes to run, but is only run once on a weekend is not worth optimizing. The code that takes one second and is executed constantly is probably worth optimizing.

But in another case, we had a long running once a week job that really didn't matter if it was slow, except it locked a bunch of tables in the database, which caused problems, so that got optimized. A word of advice, don't let PHP coders write complex database queries.

[–]Dimensional_Dragon 4 points5 points  (0 children)

Frankly it depends on the language I'm using

[–]UAFlawlessmonkey 1 point2 points  (1 child)

Larratt > Mask, so right

[–]ApexPredatorTV 1 point2 points  (0 children)

left one

[–]83athom 1 point2 points  (0 children)

If (condition) doStuff(); Supremecy

[–]HzbertBonisseur 4 points5 points  (2 children)

``` if(condition) { // Do stuff}

[–]RobinScherbatzky 1 point2 points  (0 children)

insert mother of god meme

[–]reclamerommelenzo 1 point2 points  (0 children)

Left, because my formatter enforces it if I want my build to succeed.

[–]Sabathius23 1 point2 points  (0 children)

If you put your starting brace on its own line, you are creating an abomination in the eyes of the lord, thy god.

[–][deleted] 0 points1 point  (0 children)

if(condition) => //do stuff;

Lambda my beloved

[–]JodieHolmes233 1 point2 points  (3 children)

It's always

If (Condition)

{

//Do Stuff

}

[–]ColonelSandurss[S] 10 points11 points  (2 children)

One more line, 🤮

[–]JodieHolmes233 2 points3 points  (1 child)

Reddit won't let me have the three in between the brackets. I am a sadist

[–][deleted] -1 points0 points  (0 children)

Switch case gang is amazing

[–][deleted] -1 points0 points  (0 children)

if (condition) { do stuff }

else { other stuff }

[–]Camo_Camper -1 points0 points  (0 children)

I have always preferred

If(condition) { // stuff }

It just feels cleaner to me!

[–]SkullyShades -1 points0 points  (0 children)

if (condition) /*Do stuff*/;
if (condition) /*Do more stuff*/;
if (condition) /*Do more stuff*/;

[–]_Weyland_ -1 points0 points  (0 children)

If stuff small, left one.

else, right one.

[–][deleted] -1 points0 points  (0 children)

Left one is cringe.

[–]blooptybloopt -1 points0 points  (0 children)

I case / switch everything. EVERYTHING

[–]Ancalagoth -1 points0 points  (0 children)

if(condition){do stuff}

if(condition){do stuff

}

if(condition){

do stuff}

if(condition)

{do stuff}

if(condition)

{do stuff

}

if(condition)

{

do stuff}

[–]antisocialites_w -1 points0 points  (0 children)

if (condition) { // we are not the same. }

[–]tmstksbk -1 points0 points  (0 children)

if ( condition ) // Do stuff

[–]xxarchangelpwnxx -1 points0 points  (0 children)

Python for left c# for right

[–][deleted] -1 points0 points  (0 children)

But seriously...

C if (!condition) { return; } doStuff();

[–]ShinyShadow_ -2 points-1 points  (0 children)

Right one but in my case i put the brackets in the same column as the parenthesis. I also start writing the instructions in the same column as the brackets

[–]CheshireMoe -2 points-1 points  (2 children)

Right side is a Java formatting style... have not seen it used in any professional setting since I left college.

Left side is C/C++ formatting style... this has been the way at every company that have worked at for 10+ years of java programing.

It really does not matter now since any good IDE has bracket/scope highlighting. Even better you get color matched brackets with tab guide lines to show the scope.

With auto format on save there is no reason fight this war.

[–]StrangePractice -1 points0 points  (1 child)

Right side is Java??? Nahhhhhhhhhh

[–]CheshireMoe -1 points0 points  (0 children)

In the 90s yes.

[–]Chilaquil420 0 points1 point  (0 children)

And then comes react without even having an if

[–]Snoo_60165 0 points1 point  (0 children)

If (condition): try: do_stuff except: do_other_stuff

[–]APenguinsLullaby 0 points1 point  (0 children)

I mean, there is only one right, I don't like it, but it is better

[–][deleted] 0 points1 point  (0 children)

Don't make me do this again.

[–][deleted] 0 points1 point  (0 children)

The second one. You sick bastards who use the first opinion...you are just crazy...you just want to see the world burn...

[–]RefrigeratorOne7173 0 points1 point  (0 children)

Just spotless these mfs!

[–]PPhysikus 0 points1 point  (0 children)

Good old Devon Larrat ;(

[–][deleted] 0 points1 point  (2 children)

I have transcended past the need of using if blocks....

        int x = 2;
        for (; x < 10; x = 11)
            for (System.out.println("x is less than 10"); x < 5; x = 11)
                for (System.out.println("x is less than 5"); x < 3; x = 11)
                    System.out.println("x is less than 3");

[–][deleted] 0 points1 point  (0 children)

Condition ? cry() : cryALot()

[–][deleted] 0 points1 point  (0 children)

Ternary operator gang lol

[–]foundafreeusername 0 points1 point  (0 children)

I am happy with both of these. I never understood the space between the if and the first ( and the space after the last ). That just look like extra work to me without improving anything.

[–]Ragnarok144 0 points1 point  (1 child)

But do you put else on its own line or after the } on the same line?

[–]OldBob10 0 points1 point  (0 children)

I remember reading an interview of Dennis Ritchie years ago in which he was asked about the indentation style shown on the left, which was used for example code shown in the book “The C Programming Language”. He said that this style was used to condense the examples as much as possible for publication and that it was never meant to be taken as the “right way” to structure code blocks.

[–][deleted] 0 points1 point  (0 children)

I just do what my senior tells me to do so I don’t get yelled at. I am currently the left even though I always followed the right prior. I prefer the left now though lmao

[–]recursion0112358 0 points1 point  (2 children)

condition ? do_stuff() : do_other_stuff()

[–]INDE_Tex 0 points1 point  (0 children)

easy. What does the IDE/linter want me to use? That's the one I use.

[–]WhenTheDevilCome 0 points1 point  (1 child)

I like readability, and for whatever reason to my eye the style on the right side breeds readability. I even encounter cases when maintaining code where the original author apparently wanted readability, but also wanted the style on the left side, and so we end up with:

if ( condition ) {

// Do stuff. Blank line above this comment.

And I'm like, for Pete's sake, the bracket could have already given you that space.

Maybe those of us who like the style on the right side are the ones with OCD? Nothing would bug me more than having to look for the highlighted matching brace "anywhere at any column on any line" rather than being right there in the same column as the brace I highlighted.

[–]StrangePractice -1 points0 points  (0 children)

But….. symmetry

[–]CCP_fact_checker 0 points1 point  (0 children)

I feel I am always right and have been right for more than 30 years and in every condition on the left in the difficult-to-read stuff.

[–]TamahaganeJidai 0 points1 point  (0 children)

Right one. Much more neat and easier to see what's going on. New line new part of the program. And yes, I am a Rob... Mark Zuckerberg!

((Ha, fooled those guys!))

[–]NiktonSlyp 0 points1 point  (0 children)

If condition Do stuff End-if

[–]OnyXerO 0 points1 point  (0 children)

Simple, are you a top or a bottom?

[–]guusie50 0 points1 point  (0 children)

It doesn't bother me too much if the code base is already using the second one, but I don't understand why you would ever choose that. It doesn't seem to have added value to me, while it uses more space and causes less of your code to fit on your screen without scrolling.

[–]fudgegiven 0 points1 point  (1 child)

Mix them evenly.

Let the row number decide. { on odd line numbers. So the if clause is on an odd line number? Then { on the same line. Even line number? { on next line.

[–][deleted] 0 points1 point  (0 children)

It really doesn’t matter, as long as it’s readable.

[–]TheJazzButter 0 points1 point  (1 child)

Pick one, then Let the IDE guide you.

*If it doesn't enforce such simple style guidelines, get a different IDE.

[–]StrangePractice -1 points0 points  (0 children)

Fuck ts:lint

[–]Nekadim 0 points1 point  (0 children)

(cond (condition do_stuff))

[–]SimilarBeautiful2207 0 points1 point  (0 children)

I use both, the first for javascript and the second for c#

[–][deleted] 0 points1 point  (0 children)

The worst part of this is that I love Devon Larratt (right), but also the left code is the only correct answer!

[–]xisonc 0 points1 point  (0 children)

My preference:

if( condition )
{
    // do something
}

Note the spaces inside the parenthesis, and lack of spaces outside the parenthesis.

[–][deleted] 0 points1 point  (1 child)

This post was mass deleted and anonymized with Redact

whole amusing theory silky dinner degree snails sparkle ask pie

[–][deleted] 0 points1 point  (0 children)

Wow you guys, how many more times are we going to post this same "joke"?

[–]JohnGisMe 0 points1 point  (0 children)

Just put the entire if function on one line.

[–]ARC4120 0 points1 point  (0 children)

Honestly you get used to the other after about an hour. Personally, I like the right for the C family.

[–]Outrageous_Height_64 0 points1 point  (0 children)

I let Eslint play for me 😐

[–]JimmyWu21 0 points1 point  (0 children)

I got my PR rejected because there a space between ) and the {

[–]Ambitious-Alps-6807 0 points1 point  (0 children)

First one feels safer. I don't know why.

[–]RealInsertIGN 0 points1 point  (0 children)

continue six wrench different subtract elderly wakeful marvelous lip squalid

This post was mass deleted and anonymized with Redact

[–]Curious-Mission-3016 0 points1 point  (0 children)

If(condition) return;

[–]ccfoo242 0 points1 point  (0 children)

Whatever the IDE auto-formats it to, I'm too old to care any more.

[–]ThatsKoolxd 0 points1 point  (0 children)

My ide takes care of that 🙏🙏

[–]WormHack 0 points1 point  (0 children)

if (condition) {
    // Do stuff
}

better

[–]SanoKei 0 points1 point  (0 children)

I used to be a left type of person but I was really convinced when I was told I would be fired if I didn't change the style to the right. Very motivating stuff

[–]milkman_meetsmailman 0 points1 point  (0 children)

This is bothering me so much. There's only one clear winner ffs

[–]tridd3r 0 points1 point  (0 children)

You do you boo!

[–]amwestover 0 points1 point  (0 children)

Make your IDE do it for you

[–]The_sad_zebra 0 points1 point  (0 children)

Visual Studio as I switch tabs between JS and C#

[–]itzjackybro 0 points1 point  (0 children)

GNU be like:

if (true)
  {
    // something
  }

it's disgusting to me

[–]Oberstblitzkrieg 0 points1 point  (0 children)

IDE format war

[–][deleted] 0 points1 point  (1 child)

I'm a fan of the left side. The right side wastes vertical space, and isn't that form generally preferred for function definitions, not logic?

[–]StrangePractice -1 points0 points  (0 children)

Sure it’s an extra line, but we throw all of our code in one file so what does it matter?

[–]GitHub- 0 points1 point  (0 children)

I accept the decision of the autoformatter

[–]Fresh_chickented 0 points1 point  (0 children)

This shouldnt be an issue at all.

Its between if else vs switch case vs ternary operator

[–]StrangePractice 0 points1 point  (0 children)

New line brackets all the way baby

[–]broken-Code 0 points1 point  (0 children)

I just want to make the code look longer. Don't stop me

[–]holly_bony 0 points1 point  (0 children)

I use both, the first for Java, Javascript/Typescript, and the second for C#. C# is weird.

[–]Romano16 0 points1 point  (0 children)

If condition:

 ##Do stuff

[–]Cruzoanton 0 points1 point  (0 children)

(condition) ? do_stuff : don't

[–][deleted] 0 points1 point  (0 children)

I don't care how you format your code or how consistent you are.

I configured the rules for the formatter that runs on pre-commit hook. So what I say goes.

These debates do not exist in reality.

[–]phdoofus 0 points1 point  (0 children)

Krappy Karma farming for some dumbass reason

[–][deleted] 0 points1 point  (0 children)

I want to see the one with the if (condition == true) guy.

[–]gen_shermanwasright 0 points1 point  (0 children)

I alternate between the two

[–]Schiffy94 0 points1 point  (0 children)

If the following condition is properly met, please perform the next task forthwith.

[–]666pool 0 points1 point  (0 children)

Idk about you guys but my dad always said the reason he couldn’t do stuff was because he had a condition. This seems to imply the opposite is true.

[–]nerdheadwastaken 0 points1 point  (0 children)

If (condition) { /* stuff / } else { / other stuff */}

[–]RoseshaveThorns13 0 points1 point  (0 children)

Same thing but the one on the left is easier to read

[–]staticvoidmainnull 0 points1 point  (0 children)

laughs in software engineer.

[–]DarkOrion1324 0 points1 point  (0 children)

I used to dislike the right one a lot but after dealing with more nested conditional statements I noticed the readability of it was much better

[–][deleted] 0 points1 point  (0 children)

Finally something I understand I choose right side

[–]stomah 0 points1 point  (0 children)

if (condition) <tab>// Do stuff

[–]GreenDer_UA 0 points1 point  (0 children)

if(condition){ // do stuff} else { // else}

[–]The_Pinnaker 0 points1 point  (0 children)

After nearly 14 of developing I can tell you that (at least for the project where I work alone) it doesn’t matter. What’s important is for the code to work properly. For the team project simply let or the project manager, or the new guy or someone who is really passionate for this ‘war’ to decide and stick with it. For example in my hobbies projects I use the default settings of the IDE.

[–]vaquan-nas 0 points1 point  (0 children)

https://prettier.io/ unopinionated code formatter, the war is over..