Allman style v. K&R style/one true brace style

Talk about anything
User avatar
jeeswg
Posts: 4812
Joined: 19 Dec 2016, 01:58
Location: UK

Allman style v. K&R style/one true brace style

10 May 2018, 13:53

- Usually in programming, there are advantages and disadvantages to different approaches. I am not usually concerned with other people's individual style choices, only that they have a consistent style.
- However, I'm finding more and more that in the indentation style debate, an unfortunate style is quite prevalent (K&R style/one true brace style), whereas I don't see the IMO much better Allman style as much as I would like.
- Would anyone be willing to try to defend the K&R/1TBS style?

- Here's an example I saw recently. I find the 'after' version much easier to take in and understand, and for larger and more complicated scripts, in particular scripts where object classes are defined, the advantage of using Allman style is even greater.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


- A useful link:
Indentation style - Wikipedia
https://en.wikipedia.org/wiki/Indentation_style

- Btw I'm not saying anything re. 'braces are not omitted for a control statement with only a single statement in its scope'. I feel pretty neutral on the subject. At the moment I always omit braces for one-liners.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


- For styles more generally.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Nextron
Posts: 1173
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: Allman style v. K&R style/one true brace style

10 May 2018, 16:15

I think your Wikipedia link makes a good point:
Programmers who rely on counting the opening braces may have difficulty with indentation styles such as K&R, where the starting brace is not visually separated from its control statement. Programmers who rely more on indentations will gain more from styles that are vertically compact, such as K&R, because the blocks are shorter.

If you don't indent or do it wrong, either style will be hard to read, although Allman will probably suffer less. I see indenting as the actual way to make code readable and braces just as an annoying requirement of the language, which I don't want to spend another dedicated line on by putting a opening brace on its own line, thereby stretching out the code.

I think Python manages this in an awesome way by making indentation levels functional by grouping lines into blocks. If that would be possible in AHK I would use it in a second.
The more I know:
The more I know,
I know nothing.
User avatar
Flipeador
Posts: 964
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Allman style v. K&R style/one true brace style

10 May 2018, 19:24

I think Python manages this in an awesome way by making indentation levels functional by grouping lines into blocks. If that would be possible in AHK I would use it in a second.

+999 ;)
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
jeeswg
Posts: 4812
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Allman style v. K&R style/one true brace style

19 May 2018, 22:13

The 1 true style - xkcd
http://forums.xkcd.com/viewtopic.php?t=15060

Code: [Select all] [Download] GeSHi © Codebox Plus

if(condition) {
statement();

to me looks too similar to this:

if(condition)
statement();


java - Only want to ever see Allman style code and save K&R style code - Stack Overflow
https://stackoverflow.com/questions/6876617/only-want-to-ever-see-allman-style-code-and-save-kr-style-code

From my own anecdotal research, the main advantage to K&R most often cited, other than homage to earlier precedent, is that K&R is a style favored by those that like to see more code on one screen. Yes, it is true, that's the main reason. Code density. I thought this issue would have died its final death once we got past the age of 80 x 25 character console screens in the early 90's, but alas, but it seems not to have mattered. So for those that like code density so they can see more code on one screen at a time, we suffer with millions of lines of K&R style code. * Sigh *


The Only Correct Indent Style | Terminally Incoherent
http://www.terminally-incoherent.com/blog/2009/04/10/the-only-correct-indent-style/

I created a table of the results:

Code: [Select all] [Download] GeSHi © Codebox Plus

6300	37.4%	K&R
8950 53.2% Allman
520 3.1% Whitesmith
527 3.1% GNU
150 0.8% Horstman
109 0.6% Pico
278 1.7% Banner

16834 Total Votes

Started: April 6, 2009


The Only Correct Indent Style | Terminally Incoherent
http://www.terminally-incoherent.com/blog/2009/04/10/the-only-correct-indent-style/

==================================================

Allman is the ONLY indent style you should be using. Visual Studio (best IDE out there) has it as a default, as does ANSI standard. The only reason K&R used to be popular, is because back in the 80’s all programmers had were crappy 24 line monitors, so you had to preserve space. If you can’t afford a decent 20″ or bigger monitor these days, you’ve got bigger problems than indentation… A serious programmer needs at least 2 or three monitors anyway…

K&R is for amateurs – listen to professionals and stick to Allman’s.

==================================================

Allman. K&R is a travesty. People trying to save that one extra line at the expense of clarity should be shot.

You like saving space so much, why don’t you go a step further and just write the entire block on one line.

if(x==y){x++;foo();}else{x–;bar();}

As for those other styles WTF, are they TRYING to be ridiculous?

@ tino “This is how the people that invented the language wrote the code, and if you are using their syntax, why do you second-guess their indentation? They knew what they were doing.”

You do know they had tiny green monitors, right? They tried to conserve a bit of space, and I don’t blame them, but today’s programmers have no excuse.

==================================================

Allman. The opening brace should be below its statement. The extra line and the fact that the braces line up vertically makes it easier to tell the structure of the code. Also I insist that the braces must be at the same indentation level as the associated control statement or function declaration, so those other weird styles are definitely out. I once worked with a guy who liked Horstmann, and found it to be acceptable to me. K&R tends to drive me nuts; its compactness inevitably makes code harder to read – I consider conciseness to be primarily a trait of the code itself, not the indent style around it.

==================================================

K&R tends to drive me nuts; its compactness inevitably makes code harder to read – I consider conciseness to be primarily a trait of the code itself, not the indent style around it.

==================================================

For some this might look something small. I believe it is something that has to do with logic and how do we perceive things. In all my works I had to do use the ugly old fashioned K&R style , but when I could I used Allman style and although that weren’t the policy, every one else could read my code easier so they didn’t complained. I am very happy to see that Allman style is gaining, and maybe in few years will be the programming industry main coding style.

==================================================

I prefer Allmann. Blocks are easier to identify viusally by level of indentation.

==================================================

It’s Allman dude! I’m really really frustrated when reading K&R. It makes my eyes squint :(

==================================================

when I started learning C Allman always made most sense to me – without knowing that it was “Allman”. For me it feels most intuitive and natural. Most will argue that K&R is the original and that Allman is a waste of space but I think that’s a low price for maintainability. I’m surprised that it got most of the votes now because “out there” (work and especially in the open source area) you mostly see K&R or Stroustrup style. I never could convince people to use Allman. So I had to to compromise and also got used coding in other styles. Jeet Kune Do or Bruce Lee style if you will. :P
In the end I don’t think you can really argue about it because it all comes down to personal preference.

==================================================

ALLMAN style is by far the best… and I wish it was made the defacto standard for languages like Java, C++, and C# because I’m tired of being told to do it the K&R way.

The reason some of the other styles were invented was to save space on screen by reducing the number of lines of code. This allowed book publishers to save some paper at the cost of reduced readability.

Bottom line: Allman style is the easiest to read and the most “foolproof”. You are less likely to forget an opening brace using Allman style, and it’s easier to work with blocks of code. Start spreading the word and get people to switch back to this style! (It is standard at Microsoft by the way.)

==================================================

Fair enough. Whether it’s page space in textbooks or screen space on tiny low resolution displays from the 1970’s, the K&R style of formatting tries to solve a problem that is no longer a problem at the expense of making code more difficult to read. The notion of “saving lines of code” is outdated.

When looking at other coding styles besides Allman, they all have serious drawbacks:

– K&R looks awful (the code looks broken), wrecks the block structure of the code, and makes it easy to forget opening braces by making them difficult and unpredictable to locate. (Since every line is a different length.)

– GNU requires too many tabs and too much maintenance regarding the indenting. It also wrecks the block structure of the code by creating an imaginary additional indent “level” that does nothing.

– Horstman is horrendous because you can’t easily copy/paste code lines around without blowing away the braces.

– Pico and Banner are a “dog’s breakfast” to try and read. Compact yes, but horrific to look at and likely the cause of most of the world’s programming bugs.

– Whitesmith is an inaccurate/failed version of Allman because the braces that delineate the start and end of the block are not where the actual start and end of the block is. The braces are simply at the wrong level.

The only drawback to using Allman style is that the code is spread over more lines, a problem long since resolved with larger, higher resolution monitors, and the use of classes to break code into smaller more manageable chunks.

==================================================

I guess I would strongly question how that code base was designed and why it has functions with over a thousand lines of code in them. As you suggested, it sounds like there’s more wrong there than any particular indenting style alone can fix.

Another point I wanted to mention was ROW LENGTH. Some of the most unreadable code that I’ve ever seen is unreadable because of artificially restrictive row lengths on the code lines. Instead of having one easy to read line, it gets split into two or even three lines of code. While it may be true that humans prefer narrow columns when reading novels, programmers prefer to maintain the block and command structure in their code. If a command statement such as a compound IF can be kept to one line instead of two, it becomes more readable. If a function call displays all arguments passed in on the same line, it’s easier to read than having them waterfall underneath.

I guess the three areas of programming I would change (if I could) is the following:

1. Allman style indenting would be the standard for all languages that use braces.

2. Row lengths would be increased to take advantage of wide screen monitors and reduce unnecessary line continuations in code.

3. I would beat programmers over the head with a stick for not putting comments in their code. (Arguably my greatest pet peeve.)

To date, there has never been a software project that had too much documentation. 99.9% of software projects don’t have enough or don’t have any. Having been put into the “wonderful” position of having to maintain other programmer’s junk on several occasions, I’m the guy that companies hire to tell them how their system actually works (since nobody understands it) and to document it so that future developers have an easy job. I can literally pick up a piece of my code from 10 years ago, look at a few of my comments, know exactly what I need to do, and pick up where I left off without missing a beat. But I digress…

==================================================

Another point I wanted to mention was ROW LENGTH. Some of the most unreadable code that I’ve ever seen is unreadable because of artificially restrictive row lengths on the code lines.

==================================================

When I first learned C, the professor used the K&R style, but when I wrote my code, it looked like the Allman style. It just seemed more organized to me. It was easier for me to keep track of the braces. My professor would comment about how easy it was to read my code (I assumed it was because I coded so well–maybe it was just the coding style).
I use the same style whether I’m in C, C++, C#, or Java.

==================================================

My professor would comment about how easy it was to read my code (I assumed it was because I coded so well–maybe it was just the coding style).

==================================================

@James:

I agree. I was taught to use the Allman style in school, and it was the default “template” in Visual Studio for writing code when I was in school. Whenever an editor tries to enforce a K&R style, I change the template right away.

I’ve noticed that the K&R community has been trying very hard to foist their style on everyone, and I truly believe that over the long run it will lose out to the Allman style.

A standard is determined by what the majority of people want to use, not what a small group of eggheads in ivory towers think the world should use. That’s why it isn’t surprising that Allman style is the preferred style. Allman style is what people want to use because it’s more natural, easier to read, and less prone to bugs. Those factors make it the clear winner on indenting style and brace location.

==================================================

A standard is determined by what the majority of people want to use, not what a small group of eggheads in ivory towers think the world should use. That’s why it isn’t surprising that Allman style is the preferred style. Allman style is what people want to use because it’s more natural, easier to read, and less prone to bugs. Those factors make it the clear winner on indenting style and brace location.

==================================================

Holy sht K&R is disgusting. Allman is clearly the choice of the mentally superior crowd. There’s simply no logical benefit to placing a brace on the end of the line instead of at the beginning of the next, if you’re going to have a blank line after the K&R version anyway… which you should, because this is just fcking hideous:

Code: [Select all] [Download] GeSHi © Codebox Plus

 void thisCoderHasBadStyle() {
string noBlankLinez4Me;
if (noBlankLinez4Me == "ugly") {
cout <<"damn this is some gross code.";
cout <<"just a cluster of sht.";
}
}


==================================================

It’s like religion. No matter how much evidence is shown that the K&R believers believe in things that do not exist, it is ignored. You can check in Allman code into the repository till the cows come home. Even when it clearly looks much neater and easier to scan, they will simply not see it.
I absolutely can not stand K&R. It’s horribly cryptic. Try reading code with long lines. It’s really awful.

==================================================

Allman hands down.

Code should be clean and easy to read, If code would only be relevant to a single person then all this shitty messy styles would be okay, but that’s far from the truth.

I hate working with people that write code as fast as they can, without thinking who might read their hieroglyphs other than themselves,

An example a successfull Allman’s style C++ beast, is Unreal4, they have a very strict convention that makes contributing to the source a pleasure.

==================================================

An example a successfull Allman’s style C++ beast, is Unreal4, they have a very strict convention that makes contributing to the source a pleasure.

==================================================

I started with K&R and said Allman is ugly. Then I tried Allman and now K&R is ugly for me. Use a style for a while and it becomes your style.
K&R is nice compact, but Allman is better to read. If you read your code after 1 year, readability is important, not a compactness.

==================================================

K&R is nice compact, but Allman is better to read. If you read your code after 1 year, readability is important, not a compactness.

==================================================

@ Tino:
“They knew what they were doing”.

Yeah, and they probably had to work with tiny green phosphor CRT screens where you couldn’t afford to waste any screen real estate. But this is 2016, and we all get to work with big LCD displays with millions of pixels, so the “more compact” argument doesn’t hold water.

And I don’t buy the habit argument either. Having braces aligned vertically *does* make it faster to quickly spot blocks, and plays nice with folding editors.

Allman FTW.

==================================================

Return to “Offtopic”

Who is online

Users browsing this forum: AnnettLent5150, DenisZea23063, Gia90W2230155, KimberlyJacks, PhilippSiede51 and 7 guests