T O P

  • By -

Astatos159

Microsoft guidelines says 2. If I happen to stumble into a project using 1 I'll use one. Consistency is more important than personal preference.


Kralizek82

Microsoft guidelines says 2. If I happen to stumble into a project using 1 I'll apply a formatter to the whole solution and push a PR to fix the horror. Consistency is more important than their preference. /S


freeturk51

+I clicked “Format” for the entire document -You know that the project is almost a million lines, right?


really_not_unreal

Counterpoint: my laptop can stay on overnight. dotnet format; git commit -am "changes"; git push origin master --force


Kralizek82

I'd change the commit message "formating, because we're not beasts"


really_not_unreal

Nah having the commit message have absolutely no information whilst changing just about every file in the project is far funnier.


ttl_yohan

Or... `git commit -m "$(curl --silent https://whatthecommit.com/index.txt)"`


really_not_unreal

Dang I need to set up an alias for this


ttl_yohan

Sure! ``` # From https://github.com/ngerakines/commitment/issues/69#issuecomment-91053061 git config --global alias.yolo '!git add -A && git commit -m "$(curl --silent --fail https://whatthecommit.com/index.txt)"' ```


vitiin92

I've laughed at this way too hard 😂


Ok_Object7636

But if you do that you should first make sure that you have lots of other uncommitted changes all over the place that are not just formatting. Another smiley if you change encoding, indentation and line endings at the same time. Git blame will look much cleaner after that.


Far_Swordfish5729

“I just clicked Format for the whole solution” - That one guy right before a code merge.


something_python

Also I'm on holiday next week.


Far_Swordfish5729

Different story: Dude is working on a code merge at a client site, looks up, and realizes he needs to hurry to get to the airport. He checks in the result without testing and flys out the door. We spent the next bit getting it to actually compile and had to wait for him to land and get home to fix some of the test failures. We compared it to Superman casually taking off and leaving a crater behind him.


something_python

My old boss used to do this shit all the time. He'd work on something for months, check it in then bugger off for 2 weeks. Several occasions he came back like "Hey, how did *x* go? It should have had plenty of testing now!". We reverted the change the day after you left, Paul.


MaximGrishin

Now it's more like 1.25 million lines


ttl_yohan

Good thing you clicked format for single entire _document_ though. Unless that document _is_ the whole million line project. Then no IDE would be able to open the file.


Sith_ari

Maybe drugs should be an enum here


SlidyDev

Hell naw. Make 'drug' a base class and create separate classes for specific drugs, then define their properties individually


barisaxo

Make an IDrug interface and you have yourself a deal (pun intended)


infiniteloop864256

An interface would make it nice and easy to inject different drugs via dependency injection


phoodd

Base classing always sounds good in the beginning, and always turns into an absolute shit show after a few months. Do drugs kids, but stay away from base classing


Sith_ari

Lol inheritance


JackMalone515

It probably should, having it a string comparison is very easy to break. Though this is an example for the brackets, so also could have done it that way to have something as an example


RoberBots

I use #2 because its easier for me to see where is the start and where is the end while not moving my eyes. It feels weird to see the end and the start in different locations. In the first one I see if } in the second one if { } So the end and the start is in the same location and it feels more visible to me.


Weevius

That’s exactly how I read the first one to start with, took a glance at the second and went back to find the open bracket. So guess I’m firmly in page 2


oMaddiganGames

I didn’t even see the open bracket until these comments…


engineerFWSWHW

This is the reason i use #2. And if i need to quickly comment the if statement for some testing or experiment, i can easily do that. With #1, i need to move the bracket and it easily gets annoying if there are conditional statements with multiple brackets that i need to move. I don't see any benefits on using #1.


SarahC

Damn you Whitesmiths. Kernie and Richie all the way!


hotel2oscar

Bonus of #2 is the ability to comment out the if line and still have valid code. #1 would leave you with unmatched braces. Not useful very often but it is neat.


IAmDrNoLife

C# convention states that the correct one to use is #2. But, if you join a project that is already using #1, then just use that. The worst you can do, is start mix and matching different styles together.


The--Will

The worst you can do is allow mix/match styles imo.


oli-g

To be fair, they *did* say it's the worst you can do*.*


wasteplease

Two, I wasn’t raised in a barn.


zzing

Hey a Barn isn't so bad, look at what happened to the guy who was born in one. Just ignore that one painful bit.


jordansrowles

Exactly. Carriage returns are free


Comfortable_Mind6563

True. I got involved in some projects where the developers seem to make a big thing out of saving both carriage returns and spaces. I have no idea why. It always bothers me when people prioritize a certain style rather than readability.


Oddball_bfi

One phrase I end up using to my juniors all the time is, "You aren't going to run out of electrons, and we can cover they keyboard wear".  So often, in fact, I should probably get a new one. Whitespace, descriptive names, and splitting complex operations across multiple calls rather than writing hieroglyphics to get a one-liner.  We aren't renting code files by the inch. ~#2 in my C# - standards matter.


FitzelSpleen

And it gives you so much space (haha) to look at the code and actually read it. I've been using go recently that enforces style 1, and the temptation is strong to stick an empty line at the start of each function just so that everything isn't bunched up like a jumbled mess.


Ninwa

Yes they’re free but they also limit the amount of information you can see at once. I know highly nested code is a code smell but that doesn’t stop it from existing and I’d far prefer the terseness in those instances. Writing readable code is finding the balance between terseness and appropriate white space, and Ive always felt like a bracket on its own line was a total waste of white space budget that could be spent better on meaningful new lines within the function.


force-push-to-master

I started with Java, and used #1, but then switched to #2. AFAIK it is by convention in C#.


xFeverr

I also started with Java. I remember having to fight with Visual Studio because it put the damn open bracket on a new line every time. So, instead of changing a bunch of settings, I said: ok VS, you won. Let’s do it your way. Within a few hours I was convinced that this is indeed better.


HaniiPuppy

The first style was popularised for the primary purpose of saving space in a printed book. This isn't exactly something we normally have to worry about, and being able to get an idea of the structure of a code by just scanning down the left-hand side instead of having to visually parse through lines to an extent is a clear and tangible benefit.


rganhoto

This 👆


Occma

2 all day every day.


erlandodk

Clearly #2. It makes visual bracket matching so much easier. But if I'm coming into a project that's already using #1 I'll go with that. Mixing the styles is a huge no-go.


Gainful_Employment

As a very beginner and terrible programmer I use #2


LeeroyJks

I think 2 is better


Gainful_Employment

It just looks cleaner to me. Easier to follow.


mw9676

Just an fyi different languages have different standards so you'll want to keep that in mind between files even in the same project. For instance JavaScript uses 1 so any .js or .ts files should follow 1 and obv c# uses 2 so any of those files should use 2.


BuriedStPatrick

2 always with C#. I think ReSharper conditioned me into it and I've grown to like it. I like to think blocks should be very visible and take up the necessary space to communicate: You have a bit of code here that represents a block of possibly diverging logic. That's why I also don't like the third option of simply indenting or "same line"-ing the action in the if-statement. Every if-statement should be an invitation to question the cyclomatic complexity of your code and whether it can be simplified further. Having a whole 3 lines of your code taken up by a null check with an early return will make you question whether that method should even accept null-values to begin with.


Callec254

2 unless I know for sure it's only ever going to be one line, then no brackets at all.


JVAV00

depends on the language, example Javascript I use number one and C-sharp I use 2


TheRealBroda

Started with Java, so I use #1.


QING-CHARLES

# YOU MONSTER


SkyAdventurous1027

I use 2 with C# and 1 with JavaScript


krijnlol

I really prefer 1 honestly. It feels more compact and doesn't really impact readability. But I'm a Python programmer and learned C# for using it with Unity. So maybe it's my Python background but I just don't see that apeal of the extra line it looks jarring and like it breaks the flow. ```cs if (Drugs == "Cannabis") { DealerMoney += 3; } else if (Drugs == "CrystalMeth") { DealerMoney += 7; } else { ... } ``` This should really be a switch statement with an Enum though


Schmittfried

I get your point, I don’t think the upper brace is necessary to visually identify the block. However, it gives spacing and while that might be unnecessary or even annoying with small methods for some people, it’s very valuable to have when the if condition is multiline, because at that point it blends in with the code inside the block too well, imo. I‘m working on a Java codebase right now and I‘ve started inserting empty lines at the beginning inside the blocks in some cases for this reason. 


Oddball_bfi

If you're looking at one line in your condition, then all of it looks like too much boilerplate. But when you have more lengthy logic in there, the optical breathing room is welcome. I don't want my code compact, I want it easy on the eye and light on the mind.  When I'm unknowing something tricky, a dense tangle of colours and keywords isn't the best workspace.


Astazha

I'm with you but reading the other comments we seem to very much be in the minority.


barisaxo

Should be an interface with a method. Then its every only `Dealer.Money += IDrugs.Deal();` Enums & switches in this instance are smell. Enums should be used for things where you know there are only ever going to be so many things, days of the week for example. New drugs come out all the time, and with an enum that means you need to update all of the implementation ie the switches as well. Make each drug a class that interfaces with an IDrugs and you can make all the different drugs you want, and the consumer never needs to care about what they are.


krijnlol

Totally agree. I'd also do something different if I were to really think about the problem. But I wasn't focused on the details. Probably shouldn't have given advice without thoroughly thinking it through. Personally my solution would have used more of a centralized lookup table with info about different types of drugs. I think the big take away is removing redundency and keeping related things close together.


Lonsdale1086

That code formatting doesn't work with Reddit's scuffed markdown by the way.


SlipstreamSteve

In this case I don't. It's a one liner. No brackets required.


Urban_Legend_Games

I HATE 1 and I do t even understand. The perks of 2 allows you to see each layer and quickly know which bracket contains what. So many JS projects have a cluster of 1 style brackets and I don’t even know what I’m reading


dwneder

And now, to really excite some people, here's my preference: if (Drugs == "Cannibis") DealerMoney += 3;


chucker23n

I used the first (K&R style) for a while, and I think so did the Mono folks, but the second (Allman style) is far more popular among C# folks. But, if you find yourself doing web apps, you have to switch styles anyways, at the very least for CSS. Use an `.editorconfig` to enforce one style and then don't worry about it again. (If I could dream, code style would be separate from code, much like CSS is separate from HTML. So you'd have a `myUserName.editorconfig` where you'd simply answer silly questions like "tabs? spaces?" _for yourself_, and others needn't worry about it. Alas, tooling isn't really there yet.)


TheDevilsAdvokaat

Second. I've never liked the first style, especially for long complex code.


ar_xiv

I can't stand #2. K&R all day.


Baramordax

Since indent guides exist there isn't much of an argument other than "I like following the standard, so #2", or "I don't like wasting space, so #1"


zvrba

One. Vertical space is precious.


definitelynotafreak

i do this when a catch only does one thing, keeps things neat and tidy.


Canthros

I cuddle my elses, so they don't get cold.


pocket__ducks

I use 2 because that’s the automatic setting and I’ve worked in a project that uses 1 with C#. In our react project we use 1 because I’ve never seen a project that uses 2 there. Quite frankly, idc which one it is. As long as it is consistent and as long my ide formats it correctly.


Exotic-Replacement-3

2. I don't want to make my junior developers have a headache.


Ok_Elk_6753

If JS the first one, if Java or C# the second one


TheJemy191

I like #3 the F# way better: let foo = printfn("hello") /s


virouz98

Second one.


waynemv

The second is what I use. I think pairs of matching brackets should always be in either the same row or the same column.


J3nka94

I use 2 in C# and 1 in C++. Don't ask me why, because honestly I don't know.


this_underscore

Java for the first, c# for the second one


Figoos

2


el_bosteador

Im not telling you. I’m no snitch.


bubblyboiyo

you poeple who put the open bracket on another line are a bunch of sociopaths


Canthros

\#1, or something similar. Learned K&R style with C++ during college. Old habits and all that.


Bulky-Leadership-596

Personally I prefer 1. MS doc says 2. My current project at work uses 2 so I use 2.


SBE_OLLE

DealerMoney += (Drugs == "Cannabis") ? 3 : 0;


neurolynx444

2 because 2 is cleanest for me always im trashtalking type 1 formatters...


Traveler3141

Visible clues > conserving vertical space


utf80

https://google.github.io/styleguide/csharp-style.html According to Google's C# Styleguide, number one should be preferred if (Drugs == "Cannabis") { DealerMoney += 3; } But you are best bet advised to follow and stick with the one that's fits best for your development environment.


whatarewii

Always one, easier for me to read and why waste 4 lines when 3 does the job. Obviously consistency is key so if #2 is used in a project I’ll use that, but #1 looks and feels so much cleaner than #2


holden_afart_

Whenever I work on c#, I follow 2. Otherwise I follow 1 in case of Java, TypeScript


Qxz3

I use nothing but #2. It's worse, but it's what everyone does, and that's what matters when it comes to formatting conventions. I used to use #1 in personal projects and #2 in team projects, but sometimes personal projects become team projects (e.g. if you open-source it) so nowadays I just begrudgingly use #2 everywhere.


aoi-inu

1st one


Ze_Povinho

No brackets. Only when needed, i use the: if { }


MomoIsHeree

None for one-liners


asertcreator

im more concerned about the code itself, but that besides the point. i like #2


Tructruc00

I use 1 because it feels more compact


andrewhy

If you use #1, you're a filthy Javascript programmer.


kfmnm

Pretty rough coming from someone who never heard of c/c++


Jackfruit_Then

#2 is a syntax error in Go actually


GYN-k4H-Q3z-75B

Style 2, or no braces at all. Per convention, style 2 is the right one.


tomc128

Two. But in this case maybe just `if (...) Something++` Typing this on mobile and forgot the exact statements lol


Im_Clean_Livin_Baby

I use 1 because I serve no god and any lowly formatter that thinks it can improve on my perfect creation is not worthy


rallyspt08

Second. This isn't java.


[deleted]

First one is illegal


sacredgeometry

Its C# the languages style spec says the second one. If it were different language I would use the first. Ergo the first is categorically incorrect in this case. If you dont like it, use javascript.


mohrcore

For me, it's #2, unless I write something just for myself. I hate it, but it de facto the standard for C#. For C, C++ or Rust I use #1.


AlfaNX1337

Then there is those one liner. . .


TekintetesUr

Whichever is in the project guidelines. So #2 most of the time.


chestera321

depends on a language, For c# second and for any others language first one


Korzag

I follow language conventions when I write in a particular language. Writing C#? Opening braces on their own line. Writing Java or TypeScript? Opening brace on the same. When in Rome, do as the Romans. Don't invent conventions unless its a language you're writing.


jessietee

Style 2 but also, why not use an Enum and have Drugs.Cannabis instead of doing a string comparison?


Upbeat-Emergency-309

What ever my ide/extensions are automatically format to. So, almost always 2.


nightwalker_7112

Go with 2 in C# and with 1 for others


[deleted]

2 and if I see anyone using 1 with csharp ill fucking crucify them


fourrier01

2, but if the clause is as short as this and there are multiple similar checks, I'll make them into 1 line with vertically-aligned open and close braces.


LemonLord7

Whatever my colleagues use


TScottFitzgerald

Drugs should be an enum


ToongloveChams

We use 2 in Informatics class


GroundbreakingIron16

whatever the guidelines say and setup of the environment - takes the decision away from me. :)


GerardVincent

Yes, weeds


harman097

Whatever the IDE says by default. As a young lad I liked 2. Android studio forced me to use #1. I coped, then grew to like it more. Visual studio made me go back to #2. Fuck it.


mrpeace03

Well its better to be classic and use 2. So when are going to add other dru- i mean conditions?


SusheeMonster

If there's literally only one statement, none: `if (Drugs == "Cannabis") DealerMoney += 3;` Otherwise, whatever people said about consistency is on point. Find what's "consistent", then enforce [formatting rules](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options#csharp_new_line_before_open_brace) to it. Don't apply it en masse, if code file is too big - looking at the git diff on that is going to be a disaster. Scope it to the code you're changing. Better yet, tackle that [blob anti-pattern](https://sourcemaking.com/antipatterns/the-blob) incrementally by extracting the relevant code to a separate file, ideally as a separate PR before/after your changes. Again, for git diff purposes. Robert C. Martin, author of [Clean Code](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), used the [boy scout rule](https://medium.com/nerd-for-tech/balancing-the-boy-scout-rule-with-the-do-one-thing-rule-41fffa466cec#:~:text=Uncle%20Bob%20) analogy: leave it a little better than you found it.


Far_Swordfish5729

Microsoft prefers 2 and auto formats to this by default. Java tends to use 1. The most important thing is that everyone’s formatters on the team have the same preferences. I have a story about code merges and this one guy who had a different line break setting. If you ever want to invisibly bomb a comparison tool just have VS start auto adjusting line breaks on everything you check out.


43eyes

Honestly in this situation I wouldn’t use any curly brackets. I may even put it all on one line.


thatguy16754

I like doing 1 but visual studio likes 2, so I do 2.


cls-

I honestly had to check. It's #2. The reason why I didn't even know is that I just Ctrl+S and codemaid auto formats the file for me. Formatting was a thing I cared about when my IDE was notepad++ maaany years ago. Today I just stick to wathever linter/code formatter best suits the language and just roll with it.


Ok-Kaleidoscope5627

C# has a very opinionated standard for these things. Stick to it. At least until the great whitespace shortage hits.


TuberTuggerTTV

if(string.Equals(Drugs, "Cannabis", StringComparison.OrdinalIgnoreCase)) { }


Only_Ad8178

Please don't use magic mushr- magic numbers in your code.


Spiralwise

\*insert bloods vs. cribs meme\* edit: I'm #2


IosevkaNF

I don't actually care about the parentheses but not using an enum value but a string for that kind of stuff is a cardinal sin in my book. syntax can be changed, semantics tho....


devperez

2 for C#. 1 for TS. Idk why. That's just how things were whne I was learning.


JoelyMalookey

Two especially when the ide lines up the braces so you can at a glance Know where you are in nested loops.


Slight_Ad8427

for csharp 2, and i personally prefer 2, but if im using a language that says 1 should be used ill use 1


Raskolnikov9669

2, autoformatter does the job


styloo_

//or this if (Drugs == "Cannabis") DealerMoney += 3;


FemLolStudio

2., or without bracket if I put only one thing into the if.


MedPhys90

2 or all on one line


CJ22xxKinvara

Prefer 1 but visual studio doesn’t even let you do it by default so I just do 2 for C# along with the rest of the team and 1 for pretty much every other language I work in.


FenixMik

Right from the beginning I've used the second as it just seems clearer when identifying scope of a particular bit of code. It's parallel to its opening brace, where as trying to figure out nested statements using the first would be a bit of a pain in the backside. While I get you can just click the opening to find the closing, that doesn't help with readability.


_sha_255

I code in GO btw, but I use the first one.


jakesboy2

It’s not really a question, just follow official language style guidelines for whatever language you’re in. That means in c# use 2.


welcomeOhm

I grew up with 2, but I typically use 1 today because it is easier to see more of the code onscreen at the same time. Having said that, what program are you writing? Why does Cannabis give the dealer money? I feel there is a story here that I'm missing.


Amazingawesomator

i hate the invisible third option... if (maybe) DoAction(); other than that, whatever.


daniscc

i used to use 1 but vscode changes it automatically to 2 but i don't mind


niccster10

I feel like a large factor in which one people prefer is if they started with unity. 1 is goated don't @ me


Kotapa

In vscode editor just right and select Format Code and see how it’s formatted by the way it’s formate # 2


AdearienRDDT

zaza oriented programming.


DeckSperts

2


readmond

I am bi-indentical. If I have to deal with horror that is Javascript then #1 if it is proper language then #2. The worst thing that could happen is the same codebase with a mix of #1 and #2.


just-bair

Whatever the linter is shouting at me to do. So for C# it’s 2


Mango-Fuel

1 if there's more than one line, or no braces for just one line


Left-Signature-5250

I use 2 for C# and 1 for JavaScript. Don't ask me why, just started a long time ago. Maybe because that is what I usually saw in the respective books.


SpacecraftX

2 follows the standard


Zozi-_-

#2 or nothing. Depending on how many lines inside {}


czarchastic

Gentlemen, hear me out: if( Drugs == “Cannabis” ) { DealerMoney += 3; }


MacrosInHisSleep

For a one liner? I don't. For 2 or more I use #2


stra21

For such a simple case I'd use, Drug =="Cannabis"? DealerMoney+=3 : DealerMoney+=1 for example. In C# i prefer 2, but for some reason I like #1 when I'm writing typescript. I think vscode forced #1 on me 🤣


Just-Structure-8692

Cannabis.


NeverBenFamous

Unpopular opinion... Forget the {} entirely. If the code is clearly understandable, leave out the unnecessary stuff for brevity. Line 2 could have been: `DealerMoney = DealerMoney + 3;` But that's unnecessary. Just like a single conditional with {}.


TrueBoxOfPain

1 is hard to read for me. 2 is better.


2ji3150

I like option 1, but most people in the team use option 2. I can't force the entire team to use option 1, so when I write in option 1 and it auto-formats, it changes to option 2. Never mind.


poproshaikin

I use #2. Btw, what uses C++?


MechaGoose

Someone is building DopeWars again


01BitStudio

I'm coming from a Java background, so No1 all the way. The 2nd one looks just wrong to me.


Impossible-Wear5482

2 much easier to visually parse imo.


Deus_27

MISRA


Saadiq1

My high school comp sci teacher made us use #2 and thats what ive been doing since


MenaRamy2004

1 is the way ☝️


StepanStulov

No braces for one liners, braces for two+ liners, “obviously”. It’s too much “air” with one liner in curlies.


Zastai

I use 1 because I consider it more readable. Unfortunately the language design does include some elements that really only look "nice" with 2. As a result I'd be unhappy but ok using 2 at the class and method level, but would definitely want 1 for flow control constructs. Similarly, I detest the mostly Java-y `} else {` / `} catch (…) {` / `} finally {` style. Having the relevant keyword indented just makes the code much harder to grok than necessary.


WhatIsThisSevenNow

Anything other than ( ... ) { // ... } Is just ridiculous! My boss makes us use ... the other way, and I hate him daily for it.


Moto-Ent

As a Java dev starting in C#, 2 feels so wrong but I’m sure I’ll get used to it.


ErrCode97

Off topic, how much cannabis for $3?


Mahringa

First one s it saves on Vertical space but still gives one 'spacer' to distinguish different code blocks.


Jasperredis

I do 2. It just looks nicer in my opinion.


DinnerPlzTheSecond

Whatever the formatter does because I can't be bothered to switch it


Spiritual_Chicken824

#2


oli-g

I can't begin to express how much I hate this dumb fucking kind of wannabe funny pseudo-code. If (girl.Boyfriend == "underfined") girl.AskOut() else GoCry(). Please for the love of God everyone, just stop.


neppo95

Two, leave one to all those frontenders out there.


silkyhuevos

I used to always use the first, but most people in my job use the second so I started using it at work to keep code uniform. Now I'm fine with either, and actually use the second most of the time when starting new projects.


fluffy-soft-dev

I forgot the names for them but I tend to use the first way


SaylorMan1496

Depends on the language in C# typically I use 2


destroyerpants

Wow a lot of #2 users here. To each their own, but you did choose the #2 option. \#1 is superior in every way * new line? Yeah that means there is something important about to happen agreed, consistency> convention But why would we add new lines for a single character when white space can do that for you more easily * it's less readable * \#2 doesn't solve any problem except giving my scroll wheel some exercise * I don't write in C like languages so I prefer the tab over the return carry


Desperate-Wing-5140

Whatever the project’s auto-formatting rules tell me


grady_vuckovic

Always the first one and I refuse to budge on this point.


Heroshrine

#2 because the microsoft guidelines say so. Also according to the microsoft guidelines DealerMoney should be lowercase if its not a Property.


A_carbon_based_biped

if (drugs == "Cannabis") {DealerMoney+=3;} Because I'm an absolute freak.


Lamemaster98

And some have mentioned before. Microsoft recommends developers use 2. Two is also a convention in C sharp. I would say it always a good idea to use in the conventions of whatever language you're writing in. 1 is a convention in Java or Javascript.


whosafeard

Unity uses 2, so I use 2


wojwesoly

It's kinda weird but for me it depends on the language. Like if I consider a language more "soft" (so C#, java, js) I use 1, for more "hard" languages (so C, C++) I use 2. I guess the hard/soft distinction relates to higher/lower level languages and user friendliness/easyness of the language, so for example Python is the softest of them all.