==================================================
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.
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
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.
==================================================