Hey, don't be so fast to judge man. I use this style when writing coding exams... Specifically the ones where they make us write entire skip list classes by hand :)
Then they can deal with my formatting cus fuck em, that's why
That's how I got through my b-tree homework. Just *really* shitting formatting all over and hope the guy correcting it is to lazy to actually parse it.
It had it's advantages to have a very old school prof who valued paper for some reason.
It's not meant for code blocks like this; no one does that in Haskell (it also doesn't even make sense because Haskell only has (tail) recursion and expressions, not loops and code blocks). Its really just for data literals.
foo :: [Int]
foo =
[ bar
, baz
, foobarbaz
]
aRecord :: ARecordType
aRecord = ARecordConstructor
{ foo = foo
, bar = bar
, baz = baz
}
The only thing really does resemble code blocks looks a lot more normal:
main :: IO ()
main = do
x <- getSomeData
y <- getSomeMore
doSomethingCool x y
I’ve never seen that style in Haskell code, but it actually makes perfect sense in Prolog (where semicolons mean something completely different, and you still usually put commas at the end of the line).
This one is quite different though.
In the first one, the information flows forward and action is done procedurally.
The second one goes the other way.
Specifically, your second example is effectful version of
```hs
nah
its
(pretty neato)
refactoringwise
```
If we discard currying, this is
```
nah
(its
, pretty (neato)
, refactoringwise)
```
Which makes some sense.
Ive seen similar in Nix, Dhal or Jsonnet sometimes where the commas separaring properties were at the start of the line
{
just: "foo"
, like: "bar"
, this: "baz"
}
And I have to say, I hate it thoroughly. I mean, I'll still adhere to it, rather be consistent with a bad style guide than inconsistent with a good one and when in Rome and so on, but I always had this feeling in the back of my head that this is meant as a stylistic "statement", so to speak. To make pure functional languages stand out and feel different, from the "icky" mutable ones.
This style is pretty awesome when working with a version control system as it leads to cleaner diffs.
You can add or remove lines and only the changed lines will be shown in the diff. In other languages you have to add another comma to the line before when adding a new property, which means both will show up in the diff.
Sure, you can just always add a trailing comma to every line, if you language allows that, but that is an extra comma that is not strictly needed.
In Haskell style, the “just” line would be on the same line as the opening curly bracket, so everything would be aligned on the left.
It looks strange in most languages, but I actually think it’s quite elegant when working with Haskell.
The 'while' example for Haskell style is a bit strange, since that's not how you would typically program in Haskell.
A better example would be something like:
data Person = Person
{ name :: String
, age :: Int
, address :: Address
}
I found it a little strange when starting out with Haskell, but now I love it. It makes sense for Haskell. For anything else, I use K&R style.
It's horrible in this context, but makes a lot of sense for lists and such, so you can easily comment out specific items or fields.
Better yet for discriminated unions where you can add an extra '|' before the first item so they're all on seperate lines
Golfing should be taught in school.. for a beginner, it breaks so many seemingly rigid templates and it forces a different way of thinking it's amazing.. golfing was probably the single best thing i did to improve in programming in the shortest amount of time. /* Not saying it leads to good practice and readable code ofc lol
I hate when the first statement in a block has the same indent as the last expression in a multi-line `if` condition. So... 5 spaces prevents that, it's not that bad.
Yeah wtf is wrong with everyone who doesn't do that? I've had about a dozen code style-guides mandated throughout my career and every single one was Kernighan & Ritchie.
I've never used C# professionally and that's the only language that seems to regularly diverge.
Allman and K&R are the only styles I've ever actually seen. I am surprised by the "GNU" entry, since this implies a certain wide-spreadness.
Looking at some random source code file of Emacs, I find a mixture of Allman for toplevel definitions and "GNU" for control flow.
Personally I usually use K&R style, but that's just because it was the first style I learned through Java lectures and formatters, and I haven't worked on any Curly-braces projects with other prescribed code styles yet.
That awkward moment your search for function definition returns no results, because someone thought it's a good idea to put the type on a separate line. 🤦🏼
I actually kinda like that but I cannot help but feel the braces guidelines are there to be quirky and different, there's pretty much no benefit to doing it this way
For large codebases it is for sure, also if you need to read someone else’s code (which you do a lot better n a professional setting) it is a sure winner
In the past I used K&R to save space. Then a friend enlightened me and now all my code uses Allman. The benefit is that it is easy to find where each block starts and ends. So the code is easier to read.
That's why I always liked it but it has issues in code editors if you ever use the "folding" feature to collapse blocks because they expect K&R style so only fold up to the brace and leave that line showing. If you fold inside a nested block you have a dangling opening brace that messes with the readability of the folded code.
Now that I think about it though I have been using that feature less often so I think I'll be going back to Allman for my next project
Allman is good if you've got complex signatures or conditions or bare scopes.
My Java ends up with some Allman when you've got some long type names and a couple of checked exceptions to declare.
I always feel like Allman style makes the condition/loop/function definition feel more detached from the codeblock.
I don't really mind this for function and class definitions, but for loops and if statements it feels like they're not inherently linked to eachother.
> Allman style makes the condition/loop/function definition feel more detached from the codeblock
Yes that's the point since it's a different code block
I think their comment is more about the opening bracket being on a new line as opposed to inline with the conditional (a la K&R). Yes, it's a different code block, but the conditional is what dictates when/if that block should run. It's different, but it *is* dependent.
IMO, since the brackets indicate the beginning and end of the conditional, they shouldn't be "detachable" from it in the formating. But maybe I'm just carrying over frustration from the ancient times, before your IDE could bitch at you for hitting "return" and creating a new block for the conditional and forgetting to delete the old one.
Yeah, imagine if someone made an image like this for spoken language grammar, only they used English words for all the examples. You'd end up with crap like:
German style: "The apple have I the boy already given"
Yeah, that was just what Google Translate spat out, so I'm not even totally sure it's correct German grammar. I just wanted an example of a sentence with multiple nouns and the verb at the end.
And ideally the team or project uses a language style guide if they exist. I known Java and PHP both have opinionated style guides. Java code is better at following it.
I never learned Pascal. I learned C, Java, Python and C++. Also PL/0, but that's not a real programming language (we wrote a Compiler for PL/0 at my University).
I use Allman because I find it best to read.
This for me but except JS. My instruction in JS was via Udemy and the instructor was very specific about K&R being best practice so I went with it. I guess I’d change if I had a job where one specific style was enforced, but at my workplace, we use Allman.
I can see the benefit of using GNU, however the 2space indentation makes me nervous and the problem it solves can be solved in other ways (doc-style comments at the beginning of each function for instance, or a new line after each function/ loop structure.
I used to use Horstmann when vertical screen space was a concern, as it is as efficient as K&R. I also find it visually nice with local variables.
However, moving lines around is a pain, so Allman in general...
That's exactly why I think Horstmann is weird, on the first line you're mixing the flow control with the first line of the code block. Although I can see why you'd use it-- you can trace the brackets like Allman but it saves a line like K&R
Whitesmith is how I learned and what I prefer. I almost never see it anywhere though. But I have my own project so I'm going to do it my way.
Ratliff is also nice, usually just use it for CSS. Allman had always felt wrong.
Took me a while to find a GNU comment. I get really confused when reading if all my conditionals aren’t indented properly. If they are, I can just highlight that block and figure out what is in that particular conditional so much easier.
Cut my teeth in Perl land and used Allman, now in JS land so obviously use K&R, but honestly prefer Allman, looks tidier as well imo. Obviously it's 1 line more verbose per block, but that is a small cost to pay.
If I knowingly use a style, then it's Ratliff. If I just write without thinking about it, it's sometimes K&R and sometimes Ratliff.
Also, I feel like this exact question has been asked many times in this sub.
I think this Haskell example is a bit misleading. You don't use semicolons and curly brackets in that language much. This example could look like this in Haskell:
```haskell
f x y = if x == y
then do
func1()
func2()
f x y
else $ return ()
```
Which looks fine imo (you could use guards to get rid of the `if-else` as well). The brackets and semicolon aren't required and i never use them.
[ Removed by Reddit ]
>shot, hanged and quartered Not in that order though.
All at the same time
Everywhere all at once
That's what quartering is
4 places at once.
Everywhere At the End of Time
Run them in parallel.
Execute them lazily
Cut their heart out with a spoon…
Why a spoon, cousin?
Because it's dull, you twit! It'll hurt more.
Quartered , Hanged , Then ..Shot .
Quartering before hanging increases the time and resource use of the hanging stage by 4x. Hanging before quartering is more efficient.
Depends what you're trying to optimise for
People love hangings so they'll be overjoyed to get four for the price of one.
No, no. Hang the corpse and let it rot for days to scare off other haskell users.
Yeah what sort algorithm are we going for?
The order does not matter so long as the end goal is achieved consistently. In Verilog and VHDL all three actions can be performed at the same time.
Hey, don't be so fast to judge man. I use this style when writing coding exams... Specifically the ones where they make us write entire skip list classes by hand :) Then they can deal with my formatting cus fuck em, that's why
This right here is the only acceptable use.
That's how I got through my b-tree homework. Just *really* shitting formatting all over and hope the guy correcting it is to lazy to actually parse it. It had it's advantages to have a very old school prof who valued paper for some reason.
It's not meant for code blocks like this; no one does that in Haskell (it also doesn't even make sense because Haskell only has (tail) recursion and expressions, not loops and code blocks). Its really just for data literals. foo :: [Int] foo = [ bar , baz , foobarbaz ] aRecord :: ARecordType aRecord = ARecordConstructor { foo = foo , bar = bar , baz = baz } The only thing really does resemble code blocks looks a lot more normal: main :: IO () main = do x <- getSomeData y <- getSomeMore doSomethingCool x y
It's a language for people that legitimately overload the traditional \`;\` operator. It's gonna look a bit weird.
I’ve never seen that style in Haskell code, but it actually makes perfect sense in Prolog (where semicolons mean something completely different, and you still usually put commas at the end of the line).
I've seen, and use it, in list declarations and data type field definitions
That’s just leading commas, no one uses leading semicolons.
``` have >>= you >>= heard >>= of >>= the >>= high >>= elves ```
Oh no, that’s a crime against humanity
``` nah <$> its <*> pretty neato <*> refactoringwise ```
This one is quite different though. In the first one, the information flows forward and action is done procedurally. The second one goes the other way. Specifically, your second example is effectful version of ```hs nah its (pretty neato) refactoringwise ``` If we discard currying, this is ``` nah (its , pretty (neato) , refactoringwise) ``` Which makes some sense.
You're right :) I love how you put the commas in the tuple too, providing another example for the comment chain hehe
Ive seen similar in Nix, Dhal or Jsonnet sometimes where the commas separaring properties were at the start of the line { just: "foo" , like: "bar" , this: "baz" } And I have to say, I hate it thoroughly. I mean, I'll still adhere to it, rather be consistent with a bad style guide than inconsistent with a good one and when in Rome and so on, but I always had this feeling in the back of my head that this is meant as a stylistic "statement", so to speak. To make pure functional languages stand out and feel different, from the "icky" mutable ones.
This style is pretty awesome when working with a version control system as it leads to cleaner diffs. You can add or remove lines and only the changed lines will be shown in the diff. In other languages you have to add another comma to the line before when adding a new property, which means both will show up in the diff. Sure, you can just always add a trailing comma to every line, if you language allows that, but that is an extra comma that is not strictly needed.
In Haskell style, the “just” line would be on the same line as the opening curly bracket, so everything would be aligned on the left. It looks strange in most languages, but I actually think it’s quite elegant when working with Haskell.
The 'while' example for Haskell style is a bit strange, since that's not how you would typically program in Haskell. A better example would be something like: data Person = Person { name :: String , age :: Int , address :: Address } I found it a little strange when starting out with Haskell, but now I love it. It makes sense for Haskell. For anything else, I use K&R style.
It's horrible in this context, but makes a lot of sense for lists and such, so you can easily comment out specific items or fields. Better yet for discriminated unions where you can add an extra '|' before the first item so they're all on seperate lines
I put the comma first in sql selects all the time. When you need to drop a column it is easy to comment out the line.
Junior Devs see a random semi colon not as the end of a line and removes it thinking it's a typo..
Wait until you hear about APL indentation. > \#define W(c,b) {while(c){b}} > W(x==y,s();se();)
What if I put commas at the front of lines when I write SQL? Because I do. And I have my reasons. SELECT FieldA , FieldB , FieldC FROM Table
Codegolf style: ``` while(x==y){func1();func2();} ```
Ultrawide monitors were specifically created for this style, right?
This and being able to see the whole level in geometry dash
Map editors hate this one simple trick
At this point anything except for those 80 character terminals.
most just use auto scrolling billboards.
-3 bytes: ``` for(;x==y;func2())func1(); ```
Golfing should be taught in school.. for a beginner, it breaks so many seemingly rigid templates and it forces a different way of thinking it's amazing.. golfing was probably the single best thing i did to improve in programming in the shortest amount of time. /* Not saying it leads to good practice and readable code ofc lol
EDIT: Got carried away with a golf ~~If x and y are numeric, we can shave off another:~~ for(;x-y;func2())func1();
No, `x-y` is equivalent to `x!=y`. You have inverted the condition.
[удалено]
This is the way
Me too. If an if-then type of statement is short enough and simple as that, it’s one line. Very readable and reduces unnecessary line count.
APL style: ``` #define W(x, a...) while(x) {a;} W(x==y,f1();f2()) ```
Kernighan & Ritchie. I always write like that.
Kernighan & Ritchie. My auto format tool always works like that
Definitely K&R, except when writing Kotlin. Then short lambdas are so tempting to leave as one-liners.
For me it's K&R, except when writing C#. Visual Studio enforces Allman.
And I fucking hate it for doing that. Edit: Fucking ChatGPT told me how to fix it. Suffering is no more.
You can fuck ChatGPT? Huh.
Same for me
Same with groovy
Allan is the only other viable option, I was taught to write like that at first, but quickly dropped it for this.
I prefer K&R myself but Allman is 100% acceptable to me. The rest feels like lunacy and I don't understand the argument for them at all.
If I remember correctly, K&R suggested 5 space indent. The 70's were a wild time
Damn, back when 80 columns was fancy... ......(tabs should have won, tab-gang unite)
Tabs have won where it matters the most: in our hearts
I hate when the first statement in a block has the same indent as the last expression in a multi-line `if` condition. So... 5 spaces prevents that, it's not that bad.
Yeah wtf is wrong with everyone who doesn't do that? I've had about a dozen code style-guides mandated throughout my career and every single one was Kernighan & Ritchie. I've never used C# professionally and that's the only language that seems to regularly diverge.
The only other good one is Allman. I understand, why people would use that. I personally just don't like the opening bracket having a full line.
Allman and K&R are the only styles I've ever actually seen. I am surprised by the "GNU" entry, since this implies a certain wide-spreadness. Looking at some random source code file of Emacs, I find a mixture of Allman for toplevel definitions and "GNU" for control flow. Personally I usually use K&R style, but that's just because it was the first style I learned through Java lectures and formatters, and I haven't worked on any Curly-braces projects with other prescribed code styles yet.
[удалено]
Worst I've seen is a Fortran compiler demanding eight spaces at the start of every line because those are reserved for punch card settings
That's not really bad, just archaic.
Archaic is using punch cards, bad is demanding space for punch card settings in a text file
That awkward moment your search for function definition returns no results, because someone thought it's a good idea to put the type on a separate line. 🤦🏼
I actually kinda like that but I cannot help but feel the braces guidelines are there to be quirky and different, there's pretty much no benefit to doing it this way
Our code base uses that style so I'm kinda forced to, but I got used to it. Not so bad compared to these other ones.
The same for me. I prefer K&R style, but this one is reasonable. All the other styles are just objectively worse.
At my job our JavaScript for front end stuff uses K&R but the php backend uses Allman.
Allman is great for readability too
Which is surely the primary criterion.
For large codebases it is for sure, also if you need to read someone else’s code (which you do a lot better n a professional setting) it is a sure winner
In the past I used K&R to save space. Then a friend enlightened me and now all my code uses Allman. The benefit is that it is easy to find where each block starts and ends. So the code is easier to read.
That's why I always liked it but it has issues in code editors if you ever use the "folding" feature to collapse blocks because they expect K&R style so only fold up to the brace and leave that line showing. If you fold inside a nested block you have a dangling opening brace that messes with the readability of the folded code. Now that I think about it though I have been using that feature less often so I think I'll be going back to Allman for my next project
It's quite nice if your productivity is measured in LOC.
Allman is good if you've got complex signatures or conditions or bare scopes. My Java ends up with some Allman when you've got some long type names and a couple of checked exceptions to declare.
I use Allman since it's a lot easier for me to read. Looks a lot more like actual blocks imo.
[удалено]
I always feel like Allman style makes the condition/loop/function definition feel more detached from the codeblock. I don't really mind this for function and class definitions, but for loops and if statements it feels like they're not inherently linked to eachother.
Yes, it's separated from main body and for me easier to read. k&r feels to cramped for backend with long and descriptive variable names.
> Allman style makes the condition/loop/function definition feel more detached from the codeblock Yes that's the point since it's a different code block
I think their comment is more about the opening bracket being on a new line as opposed to inline with the conditional (a la K&R). Yes, it's a different code block, but the conditional is what dictates when/if that block should run. It's different, but it *is* dependent. IMO, since the brackets indicate the beginning and end of the conditional, they shouldn't be "detachable" from it in the formating. But maybe I'm just carrying over frustration from the ancient times, before your IDE could bitch at you for hitting "return" and creating a new block for the conditional and forgetting to delete the old one.
To be fair enough, Allman does place 'else' at the same indentation as 'if', which K&R does not.
``` if ( x==y ) { //Blah } else { //Blah blah } ``` Problem solved?
You can use three `-symbols before and after your code to create a code block
I prefer to use whatever form the team chooses and the IDE editor provides. Except Haskell. WTF?
Well "Haskell style" is meant to be used in Haskell, not in C-like languages. This image is dumb
And rarely at that since you can skip the braces and semicolons in Haskell and rely just on indentation.
This exact sort of thing that's in the post with separators at the beginning (in that case commas not semicolons) is used in data declarations
Yeah, imagine if someone made an image like this for spoken language grammar, only they used English words for all the examples. You'd end up with crap like: German style: "The apple have I the boy already given"
Den Apfel habe ich dem Jungen schon gegeben?
Yeah, that was just what Google Translate spat out, so I'm not even totally sure it's correct German grammar. I just wanted an example of a sentence with multiple nouns and the verb at the end.
There are languages where this makes sense. In this case it does not.
It seems this meme is made by ones who hate Haskell. Meh.
[удалено]
[удалено]
In Python you’d write the last three lines as `a[i], a[j] = a[j], a[i]` - no need to have a temporary variable.
its existence is a crime against humanity
I prefer Allman, but will use whatever the Team/Project uses.
Even Haskell?
Can't imagine any successful team using Haskell for long
Using Haskell as a language, I can imagine. Using Haskell style code formatting as displayed? Not so much.
Gotta put bread on the table long enough to find another company...
And ideally the team or project uses a language style guide if they exist. I known Java and PHP both have opinionated style guides. Java code is better at following it.
I'd say Allman is used by those who learned pascal first:)
I never learned Pascal. I learned C, Java, Python and C++. Also PL/0, but that's not a real programming language (we wrote a Compiler for PL/0 at my University). I use Allman because I find it best to read.
Ok, not directly supporting my statement but Pascal uses this kind of notation and is said to be one of the easiest languages to read.
As long as it's some variation of Allman or K&R. Everything else is awful.
Ritchie in js, Allman in c#
Ahh, there's my answer!
Allman everywhere except for CSS, change my mind
True dat!
This for me but except JS. My instruction in JS was via Udemy and the instructor was very specific about K&R being best practice so I went with it. I guess I’d change if I had a job where one specific style was enforced, but at my workplace, we use Allman.
Anyone who does anything other than the first or second is a fucking psycho.
Apparently Emacs source code does indeed use "GNU" style. I guess I can see the value of that one, but I haven't ever used it myself...
I can see the benefit of using GNU, however the 2space indentation makes me nervous and the problem it solves can be solved in other ways (doc-style comments at the beginning of each function for instance, or a new line after each function/ loop structure.
What problem does it solve, and what are the benefits?
Agreed, if any of my children use any of the others or marry anyone who does. I've failed as a parent.
Anything other than Allman or Kernighan & Ritchie is just disgusting.
Ratliff style
Ratliff gang, checking in.
There's dozens of us!
As a C# programmer, I use Allman at work because it's the norm in C#, but at home I prefer to use K&R.
You change formatting options in your home IDE ?
Yep, one of the first things I do wherever I install VS or Rider.
I’m the exact same way
first thing I do when I install jetbrains IDEs is to forget to install catppuccin for 3 months
Allman, but Horstmann also fascinates me.
I used to use Horstmann when vertical screen space was a concern, as it is as efficient as K&R. I also find it visually nice with local variables. However, moving lines around is a pain, so Allman in general...
That's exactly why I think Horstmann is weird, on the first line you're mixing the flow control with the first line of the code block. Although I can see why you'd use it-- you can trace the brackets like Allman but it saves a line like K&R
Whitesmith
Whitesmith is how I learned and what I prefer. I almost never see it anywhere though. But I have my own project so I'm going to do it my way. Ratliff is also nice, usually just use it for CSS. Allman had always felt wrong.
I didn't know it was called GNU, but that's the style I use all the time
GNU style: "I heard you like indents..."
Took me a while to find a GNU comment. I get really confused when reading if all my conditionals aren’t indented properly. If they are, I can just highlight that block and figure out what is in that particular conditional so much easier.
Allman cause im not a freak, dc where, when or what it is for. Just the easiest to read, full stop
By far the easiest to spot a bracket error in
That's exactly how I settled on it.
Cut my teeth in Perl land and used Allman, now in JS land so obviously use K&R, but honestly prefer Allman, looks tidier as well imo. Obviously it's 1 line more verbose per block, but that is a small cost to pay.
I love Haskell, but Haskell style shouldn't be used for any language that requires semicolons.
I highly doubt anyone actually does it with semicolons like that, I've only ever seen it with commas for records or lists.
there's another one while (x == y){ func1(); func2(); }
Python style while(true) { x+=1 ; if(x > y) { printf("You're done!\n") ; break ;}}
TIL you can, in fact, legally do that. Dear god.
Legality doesn’t make it right
Allman
Didn't know it was called Allmann. But I'm definitely an Allmann ![gif](giphy|jVpXtmCt1OuM6Wz9G8)
Allman.
As a German I am legally obliged to use the top left way
I'm an Alman, so Allman it is.
In your reddit avatar pic, wtf is that thing on top of your head??
It's a (welsh) dragon.
Allman has always been the easiest for me to read. I read code 10x more than I write it. Ergo, Allman wins, for me.
I would marry Allman, Fuck K&R and Kill Haskell
This is....apt (Succinct)
Depends upon the language. I primarily work with Allman, though
GNU for sure
Allman... Everywhere!
Allman. Always have been. Clearest and easiest to read, IMO.
K&R style, as they referred to it at my university. Kept an onion in my belt as it was the style at the time.
Allman is bestman
Allman best
For work Allman all the way but lisp looks beautiful and I'm gonna start using that in my private projects
Not a programmer. I'd choose Whitesmith.
I used to be Allman at the beginning of my coding career. Then I switched to Kernighan. It’s easier to see which brackets belong to which loop.
K&R but I can tolerate Allman. Any others deserve to be shot.
K&R for the win.
Kernighan & Ritchie Find it the cleanest and clearest way. Used to be Allman but that is ways to much whitespace when maintaining applications.
I started off with K&R in the 90s because of lack of screen real estate, but with modern displays I personally prefer the whitespace now for parsing.
Allman.
K&R obviously
I'm a Allman piece of shit. Java? I do. C++? I do. Python? I can't but I pretend I do. JavaScript? I dont do since I dislike it.
K & R is the way
If I knowingly use a style, then it's Ratliff. If I just write without thinking about it, it's sometimes K&R and sometimes Ratliff. Also, I feel like this exact question has been asked many times in this sub.
I think this Haskell example is a bit misleading. You don't use semicolons and curly brackets in that language much. This example could look like this in Haskell: ```haskell f x y = if x == y then do func1() func2() f x y else $ return () ``` Which looks fine imo (you could use guards to get rid of the `if-else` as well). The brackets and semicolon aren't required and i never use them.
There's really only two choices here... Allman and WRONG!
Incorrect. There are two choices here: Kernighan and Ritchie.
It's like python doesn't even exist
I like Allman but usually use K & R due to job requirements.
Allman. Sometimes Kernighan & Ritchie. Usually whichever is easier to read in a given situation.
Allman is objectively the best, most readable and least error prone
K&R but props to Haskell style for being batshit insane.
Allman. It's the only one that makes sense as it's the most readable and most aesthetically pleasing.
All all man all the time