C Indentation Style Poll

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;
      }
  }
  • Kernighan & Ritchie, also Stroustrup, Suckless…
  • Allman, also Linux kernel
  • Whitesmiths
  • Horstmann
  • The GNU coding standards

0 voters

1 Like

suckless for me!

Otherwise i know that Allman has one pro - for some text editors block folding doesn’t work properly with suckless style…

So i guess if you deal with something super-huge which can be edited and watched potentially from any editor - that would make sense :slight_smile:

1 Like
# 2 Allman, also Linux kernel

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.

Pudge

1 Like

Strict Suckless style would be to put else in the same line as the closing brace of if:

if (foo) {
    do_something();
} else {
    bar();
}

Also, Kernighan & Ritchie place { in a separate line for function blocks, but not for other code blocks:

int main(int argc, char** argv)
{
    if (foo) {
        do_something();
    }
    else {
        bar();
    }
    return 0;
}

But I didn’t want to complicate the poll with too many options for small variations.

1 Like

That is exactly how i like it :slight_smile:
Also in javascript :laughing:


Forum likes limit sucks :no_mouth:

Definitely the first one plus 120 chars long line. I came to C++ from Java with Google coding style.

2 Likes

A piece of code is never ugly ! :grin:

It seems that it happens to others App also !!

1 Like

Have you seen the GNU coding standards?

“Ugly” is the most charitable word I can think of describing their indentation style in C. :rofl:

They made some great software, though…

1 Like

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

OK - convince me otherwise :grin:

Well, beauty is in the eye of the beholder. Like when my mum told me I’m beautiful :smiley:

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!

2 Likes

OK - maybe - but here’s what I do (shown my way, and apparently GNU way). Beholder beware!:

/* ----------------------- */
/* do the work for getInfo */
/* including stripping the */
/* newline char off result */
/* ----------------------- */
char * audInfo(char * command, char *results, int d_size)
    {
    FILE *fp = NULL;
    int n;
    char * token;

    fp = popen(command, "r");		             // Issue the command
    while ( fgets(results, d_size, fp) )            // read result and store
        {
        token = strtok(results, "\n");           // cheap removal \n char
        } /* end while */
    pclose(fp);
    return (0);
    } /*end audInfo */

/* reformatted (slightly) for GNU specs */

char * audInfo(char * command, char *results, int d_size)
{
FILE *fp = NULL;
int n;
char * token;
fp = popen(command, "r");		             // Issue the command
while ( fgets(results, d_size, fp) )            // read result and store
    {
    token = strtok(results, "\n");           // cheap removal \n char
    } /* end while */
pclose(fp);
return (0);
} /*end audInfo */

That’s called Whitesmiths style :wink: 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 :slight_smile:

1 Like

Didn’t know it had a name - I just used it for clarity (as mentioned). I felt bad about not using K&R :grin: - but I did it anyway…

1 Like

Thanks for the info @Kresimir
Would be possible to create a sondage as :

Which style coder are you ?

1- Whitesmiths
2- GNU
3- Allman, also Linux kernel
4- Kernighan & Ritchie, also Stroustrup
5- Spits hex machine code without spaces

1 Like

5 - spits hex machine code without spaces :dragon_face:

I would add a sondage on the name of @Kresimir if you agree with ?

I’m not sure you could create it yourself after fews days on Discourse !
I do believe you need time to have access of it…

Let’s say we could put it tomorrow, if some new code style appears.

I think I can do it, even as a TL1 member. Give me a few minutes, please :slight_smile:

here are countless other C styles and variations, but these are the four main ones.

1 Like

Let me know if it works ? I think it’s better to open a new Thread for it…

EDIT: in General System or / Community Contributions seems to be the best place for it !

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! :grin: 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 { )

1 Like