After an idea by @FLVAL, here is a poll for programmers who use C, and/or other C-like languages (C++, Rust, Java, etc…) which use curly braces for denoting code blocks.
Which one of the following five indentation styles do you find the most aesthetically pleasing?
Here is an example of each of the five possible choices. These examples are all with mandatory braces (which may not be the case in the actual specification), just to make them equivalent. Also, there are countless variations, but these are the main ones.
/* 1. Kernighan & Ritchie, also Stroustrup, Suckless... */
while (foo) {
if (bar) {
do_something();
foobar();
}
else {
return 0;
}
}
/* 2. Allman, also Linux kernel */
while (foo)
{
if (bar)
{
do_something();
foobar();
}
else
{
return 0;
}
}
/* 3. Whitesmiths */
while (foo)
{
if (bar)
{
do_something();
foobar();
}
else
{
return 0;
}
}
/* 4. Horstmann */
while (foo)
{ if (bar)
{ do_something();
foobar();
}
else
{ return 0;
}
}
/* 5. the GNU coding standards */
while (foo)
{
if (bar)
{
do_something();
foobar();
}
else
{
return 0;
}
}
That is the way I was taught in the late 70’s & early 80’s. I learned C while working for AT&T, so one would think the Kernighan & Ritchie way. Don’t know why, perhaps instructor peference.
Hmmm - I just took a look at the link, and I don’t see the ugly…
Strangely enough, it seems to follow the same principles I have always used (they work in Pascal and Modula-2 as well, and elsewhere). I always have opening and closing braces for just about anything at the same level (column) as it makes things MUCH faster when cruising through code - as well as catching errors when an edit goes wrong! One of my pet peeves is ‘formatters’ that insist on changing what you put in to fit some other standard (I think it’s Stack Exchange that does that to you, for example).
Well, beauty is in the eye of the beholder. Like when my mum told me I’m beautiful
For me, it’s not the problem of opening braces being on their separate lines (so-called Allman style, although I personally prefer K&R style), it’s the goofy indentation or braces (with two spaces, when everything else is indented four spaces). Look at this else statement, only its mother (i.e. Richard Stallman) thinks it’s beautiful:
if (x < foo (y, z))
haha = bar[4] + 5;
else
{
while (z)
{
haha += foo (z, z);
z--;
}
return ++x + bar ();
}
Too true. What always got me was that some of the styles being proposed seemed to be trying to save vertical ‘real estate’ - as if it matters electronically! I guess when you were relying on fanfold printouts you could save some paper - but that’s too old school for me!
That’s called Whitesmiths style It’s different from the GNU style.
/* Whitesmiths */
if (foo)
{
bar;
}
/* GNU */
if (foo)
{
bar;
}
/* Allman, also Linux kernel */
if (foo)
{
bar;
}
/* Kernighan & Ritchie, also Stroustrup, and Suckless */
if (foo) {
bar;
}
Personally, I only like the last two, K&R being my preferred, but Allman being ok, too.
Whitesmiths makes sense, too, I just don’t like it. GNU style is just goofy, IMO
Guess I didn’t take it all in when looking through that link. It might be goofy - but I didn’t pay all THAT much attention as I was reading it, as I have no intention of changing at this point! Not to mention I’m not up to writing GNU code anyway.
Thanks for the info -and I’m happy that someone likes my style enough to name it - even if some don’t like it. I suspect if we did a project together, we’d have to use Allman for consistency (and for some of the utilities that expect column 1 positioning for { )