Coding Style

Discussion in 'C Programming' started by Eric, May 12, 2004.

  1. Eric

    Eric Guest

    I thought it might be fun to run a simple vote to discover the most
    preferred spacing style for a simple if statement with a single, simple
    boolean test. By my count, there are 32 possible variations for this
    case. Here is a complete list.

    AA: if ( a > b )
    AB: if ( a > b)
    AC: if ( a >b )
    AD: if ( a >b)
    AE: if ( a> b )
    AF: if ( a> b)
    AG: if ( a>b )
    AH: if ( a>b)
    AI: if (a > b )
    AJ: if (a > b)
    AK: if (a >b )
    AL: if (a >b)
    AM: if (a> b )
    AN: if (a> b)
    AO: if (a>b )
    AP: if (a>b)
    AQ: if( a > b )
    AR: if( a > b)
    AS: if( a >b )
    AT: if( a >b)
    AU: if( a> b )
    AV: if( a> b)
    AW: if( a>b )
    AX: if( a>b)
    AY: if(a > b )
    AZ: if(a > b)
    BA: if(a >b )
    BB: if(a >b)
    BC: if(a> b )
    BD: if(a> b)
    BE: if(a>b )
    BF: if(a>b)

    So, rank the available options from your most favorite to your least
    favorite. It is not necessary fully rank all available options. Two or
    more options can be ranked as being essentially equivalent. An example
    ballot would look like:

    AA > AJ > AG > AP > AQ = AW = AZ = BF

    In this case, AA AJ AG AP AQ AQ AZ & BF, are all ranked above those
    options which were left unranked. AQ AW AZ & BF are considered
    essentially equivalent in terms of which would be preferred over the
    other.

    Feel free to e-mail your ballot to me to keep the group from filling up
    with the votes. I'll announce the results (if there are any to announce)
    when I stop receiving votes.

    If you're interested in the method I will use to determine the result,
    you are welcome to ask me via a private message.

    If this receives a sufficient amount of interest, I'll do more just like
    it out of some morbid curiosity concerning what is the most preferred
    (within well known limitations) style around here.
     
    Eric, May 12, 2004
    #1
    1. Advertising

  2. Eric

    Dan Pop Guest

    In <1gdo7jq.3gkwmyn064qoN%> (Eric) writes:

    >I thought it might be fun to run a simple vote to discover the most
    >preferred spacing style for a simple if statement with a single, simple
    >boolean test.


    You're wasting your time. There is no fun in a religious war about the
    usage of white space to improve the code readability.

    > AJ: if (a > b)


    IMHO, this is the best, as it uses the minimum amount of white space
    for a maximum of effect. if(a > b) comes close, but I prefer to omit
    the space before the left parenthesis only for function calls.

    Of course, other people have different preferences and there is nothing
    fun or interesting in that.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 12, 2004
    #2
    1. Advertising

  3. Eric

    Mike Wahler Guest

    "Eric" <> wrote in message
    news:1gdo7jq.3gkwmyn064qoN%...
    > I thought it might be fun to run a simple vote to discover the most
    > preferred spacing style for a simple if statement with a single, simple
    > boolean test. By my count, there are 32 possible variations for this
    > case. Here is a complete list.
    >
    > AA: if ( a > b )
    > AB: if ( a > b)


    void f1(void)
    {
    }

    void f2(void)
    {
    }

    void f3(void)
    {
    void (*f[])(void) = {f1, f2};
    f[a > b]();
    }

    -:)

    -Mike
     
    Mike Wahler, May 12, 2004
    #3
  4. Mike Wahler wrote:
    > ...
    > void f1(void)
    > {
    > }
    >
    > void f2(void)
    > {
    > }
    >
    > void f3(void)
    > {
    > void (*f[])(void) = {f1, f2};
    > f[a > b]();
    > }
    >
    > -:)
    > ...


    (a > b && (f1(), 1)) || (f2(), 1)

    is more elegant since it doesn't need the extra array :O)

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 12, 2004
    #4
  5. Eric

    CBFalconer Guest

    Dan Pop wrote:
    > (Eric) writes:
    >
    >> I thought it might be fun to run a simple vote to discover the
    >> most preferred spacing style for a simple if statement with a
    >> single, simple boolean test.

    >
    > You're wasting your time. There is no fun in a religious war
    > about the usage of white space to improve the code readability.
    >
    >> AJ: if (a > b)

    >
    > IMHO, this is the best, as it uses the minimum amount of white
    > space for a maximum of effect. if(a > b) comes close, but I
    > prefer to omit the space before the left parenthesis only for
    > function calls.
    >
    > Of course, other people have different preferences and there is
    > nothing fun or interesting in that.


    The millenium has arrived, 1229 days late. Dan and I are in
    complete agreement.

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    CBFalconer, May 12, 2004
    #5
  6. Style is a very subjective and personal consideration.
    C and C++ programmers develop or adopt a style
    in order to make their code easier for themselves
    and other programmers to read, understand and maintain.
    If you are developing your own style, there are no rules
    except that you should try to be consistent.
    Otherwise, you should try to adopt a style
    with which other C and C++ programmers are comfortable,
    familiar or that they will at least recognize.
    Personally, I try to use the same punctuation rules
    that are used for ordinary (mathematical) typesetting.
    Here are my recommendations:

    Terminators always follow immediately after an expression

    x@ for all @ in {?, :, ,, ;}

    and are followed by at least one white space.
    Write

    x? y: z

    instead of

    x ? y : z

    or

    x?y:z

    and write

    void f(int, int, int); void g(double);

    instead of

    void f(int,int,int);void g(double);

    for example.

    There is no space
    between some binary operators and their operands

    x@y for all @ in {::, ., ->, .*, ->*, *, /, %, &, ^, |}

    but there is always a space
    between other binary operators and their operands

    x @ y for all @ in {+, -, <<, >>;, <, <=, >, >=, ==, !=,
    &&, ||, =, *=, /=, %=, +=, -=, <<=, >>=, &=, |=, ^=}

    except when expressions appear as subscripts.
    Write

    x + y

    instead of

    x+y
    and

    x*y

    instead of

    x * y

    for example.
    But you may wish to write

    A[i+1][j-1]

    instead of

    A[i + 1][j - 1]

    for example to subscript array A.


    Most unary prefix operators never have any whitespace
    between themselves and their operands

    @x for all @ in {::, ++, --, ~, !, -, +, &, *}

    but others do

    @ x for all @ in {sizeof, new, delete, delete [], throw}

    No unary postfix operators

    x@ for all @ in {[], (), ++, --}

    ever have any whitespace between themselves and their operands.

    Use the normal typesetting rules for parentheses (),
    square brackets [], angle brackets <> and curly brackets {}.
    No space after (, [, < or { and no space before ), ], > or }.
    Write

    (x)

    instead of

    ( x )

    or

    (x )

    or

    ( x)

    and write

    [x]

    instead of

    [ x ]

    or

    [x ]

    or

    [ x]

    for example.
    There are, of course, exceptions
    where extra white space helps to make your code more readable:

    double A[2][3] = {{ 1, -1, 0},
    {-10, 11, -21}};


    Don't give identifiers cryptic, mangled names.
    Use ordinary, meaningful words, conventional symbols
    or abbreviations with annotations.
    Write

    double distance, velocity, acceleration, mass, Force;

    Force = mass*acceleration;

    or

    double x; // distance
    double v; // velocity
    double a; // acceleration
    double m; // mass
    double F; // force

    F = m*a;

    for example.

    Don't rely on defaults. Make declarations explicit.
    Write

    int i = 1;

    instead of

    i = 1;

    to declare and initialize integer i and write

    class X {
    private:
    // Representation
    int I;
    public:
    // Constructors
    // ...
    };

    instead of

    class X {
    // Representation
    int I;
    public:
    // Constructors
    // ...
    };

    to define the private data members of class X for example.


    Use indentation to emphasize scope.
    Everybody is comfortable with standard indentation:

    void f()
    {
    // indent
    }

    But I indent curly brackets to the scope of the function body:

    void f()
    {
    // indent
    }

    And I include the open curly bracket with the function heading:

    void f() {
    // indent
    }

    to save a line of code.

    I always indent just two spaces at a time and
    I place just one statement on each line so that
    there is usually room for a comment at the end of each line
    beginning in column 33 or 41.

    Write

    if (condition) {
    // statements
    }

    instead of

    if(condition) {
    // statements
    }

    and

    while (condition) {
    // statements
    }

    instead of

    while(condition) {
    // statements
    }

    to distinguish flow control structures from function calls.

    I use

    // comment

    for comments in C++ and I reserve

    /*
    a = b;
    // comment
    b = c;
    */

    to comment out code which may include comments.


    If you find yourself in an environment
    that requires you to conform to style rules with which you are not
    comfortable,
    consider investing a little time and effort in a program like astyle

    Artistic Style
    http://astyle.sourceforge.net/

    which changes the appearance of C or C++ programs
    by inserting or deleting whitespace.

    Write

    constant == variable

    instead of

    variable == constant

    when comparing a variable to a constant for equality
    so that if you write

    constant = variable

    by mistake, the compiler will detect the error.

    I always write

    x < y

    or

    x <= y

    instead of

    y > x

    or

    y >= x

    when comparing two values so that the expression is true
    when the left hand side is to the left of the right hand side
    on the real number line.
     
    E. Robert Tisdale, May 12, 2004
    #6
  7. Eric

    Alan Balmer Guest

    On Wed, 12 May 2004 18:53:33 GMT, CBFalconer <>
    wrote:

    >Dan Pop wrote:
    >> (Eric) writes:
    >>
    >>> I thought it might be fun to run a simple vote to discover the
    >>> most preferred spacing style for a simple if statement with a
    >>> single, simple boolean test.

    >>
    >> You're wasting your time. There is no fun in a religious war
    >> about the usage of white space to improve the code readability.
    >>
    >>> AJ: if (a > b)

    >>
    >> IMHO, this is the best, as it uses the minimum amount of white
    >> space for a maximum of effect. if(a > b) comes close, but I
    >> prefer to omit the space before the left parenthesis only for
    >> function calls.
    >>
    >> Of course, other people have different preferences and there is
    >> nothing fun or interesting in that.

    >
    >The millenium has arrived, 1229 days late. Dan and I are in
    >complete agreement.


    Another vote for AJ - that's how I've configured my editor to do it.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, May 12, 2004
    #7
  8. Eric

    Eric Guest

    Dan Pop <> wrote:

    > In <1gdo7jq.3gkwmyn064qoN%>
    > (Eric) writes:
    >
    > >I thought it might be fun to run a simple vote to discover the most
    > >preferred spacing style for a simple if statement with a single, simple
    > >boolean test.

    >
    > You're wasting your time. There is no fun in a religious war about the
    > usage of white space to improve the code readability.


    Not interested in discussion about what is best style.

    I am only interested in the most preferred style based upon the rankings
    of readers of comp.lang.c.

    > > AJ: if (a > b)

    >
    > IMHO, this is the best, as it uses the minimum amount of white space
    > for a maximum of effect. if(a > b) comes close, but I prefer to omit
    > the space before the left parenthesis only for function calls.


    So, you would rank:

    AJ > AZ

    Do you see any other merit differences between the various options or
    would this ballot be the one you would submit?
     
    Eric, May 12, 2004
    #8
  9. Eric

    Eric Guest

    Mike Wahler <> wrote:

    > "Eric" <> wrote in message
    > news:1gdo7jq.3gkwmyn064qoN%...
    > > I thought it might be fun to run a simple vote to discover the most
    > > preferred spacing style for a simple if statement with a single, simple
    > > boolean test. By my count, there are 32 possible variations for this
    > > case. Here is a complete list.
    > >
    > > AA: if ( a > b )
    > > AB: if ( a > b)

    >
    > void f1(void)
    > {
    > }
    >
    > void f2(void)
    > {
    > }
    >
    > void f3(void)
    > {
    > void (*f[])(void) = {f1, f2};
    > f[a > b]();
    > }
    >
    > -:)


    :p

    Ok, fine....there are more variants, but let's just stick to the ones I
    defined.
     
    Eric, May 12, 2004
    #9
  10. Eric wrote:
    > I thought it might be fun to run a simple vote to discover the most
    > preferred spacing style for a simple if statement with a single, simple
    > boolean test. By my count, there are 32 possible variations for this
    > case. Here is a complete list.


    I reject any style with different spacing after '(' and before ')' or
    with different spacing before and after an operator. That means your
    list is immediately collapsed to:
    AA: if ( a > b )
    AG: if ( a>b )
    AJ: if (a > b)
    AP: if (a>b)
    AQ: if( a > b )
    AW: if( a>b )
    AZ: if(a > b)
    BF: if(a>b)
    >
    > So, rank the available options from your most favorite to your least
    > favorite.


    Count the 24 omitted ones as tied for dead last.
    Of the remaining ones, I dislike (with decreasing vehemence)
    a) shoving the '(' up next to the 'if',
    b) omitting spaces around the operator '>', and
    c) spaces afet '(' and before ')'.

    This leaves the order as roughly
    AJ > AA > AP > AZ > AG > AQ > BF > AW > (All the rest)

    I am not adamant about any of this except for finding the non-symmetric
    spacing unacceptable. Any style which you find readable is OK for your
    own use, obviously. And I would change mine to fit any required house
    style.
     
    Martin Ambuhl, May 12, 2004
    #10
  11. Eric

    Ed Morton Guest

    Eric wrote:

    > I thought it might be fun to run a simple vote to discover the most
    > preferred spacing style for a simple if statement with a single, simple
    > boolean test. By my count, there are 32 possible variations for this
    > case. Here is a complete list.
    >
    > AA: if ( a > b )

    <snip>
    > AJ: if (a > b)

    <snip>

    AJ > AA

    I'd never consider using any of the rest.

    Ed.
     
    Ed Morton, May 12, 2004
    #11
  12. On Wed, 12 May 2004 12:17:18 -0400, in comp.lang.c ,
    (Eric) wrote:

    >I thought it might be fun to run a simple vote to discover the most
    >preferred spacing style


    You have a curious idea of fun..... :)

    > AJ: if (a > b)
    > AP: if (a>b)
    > AX: if( a>b)
    > BF: if(a>b)


    any of those 4, not fussy. The others, with odd spacing round things, look
    silly.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, May 12, 2004
    #12
  13. Eric

    Eric Guest

    Ed Morton <> wrote:

    > Eric wrote:
    >
    > > I thought it might be fun to run a simple vote to discover the most
    > > preferred spacing style for a simple if statement with a single, simple
    > > boolean test. By my count, there are 32 possible variations for this
    > > case. Here is a complete list.
    > >
    > > AA: if ( a > b )

    > <snip>
    > > AJ: if (a > b)

    > <snip>
    >
    > AJ > AA
    >
    > I'd never consider using any of the rest.


    Whether you'd consider using them or not is irrelevent. The only thing
    that matters is whether you see any merit differences between them.

    For example, based on your two rankings, it would seem that you value a
    space between the if statement and the left paren.

    So, it might be that you would rank the remaining ones which match that
    feature below AA, but above those that do not have that feature.

    But, then, AJ > AA could easily be your complete ranking.

    thank you.


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, May 12, 2004
    #13
  14. Eric

    Default User Guest

    Eric wrote:

    > Not interested in discussion about what is best style.
    >
    > I am only interested in the most preferred style based upon the rankings
    > of readers of comp.lang.c.



    To what end? It's one of the more useless discussion around.



    Brian Rodenborn
     
    Default User, May 12, 2004
    #14
  15. On Wed, 12 May 2004 21:47:49 GMT, in comp.lang.c ,
    (Eric) wrote:

    >Ed Morton <> wrote:
    >
    >> Eric wrote:
    >>
    >> > I thought it might be fun to run a simple vote to discover the most
    >> > preferred spacing style for a simple if statement with a single, simple
    >> > boolean test. By my count, there are 32 possible variations for this
    >> > case. Here is a complete list.

    (snip)
    >> >

    >> AJ > AA
    >>
    >> I'd never consider using any of the rest.

    >
    >Whether you'd consider using them or not is irrelevent. The only thing
    >that matters is whether you see any merit differences between them.


    Thats a bit rude. The question you asked was which we preferred. Ed
    answered that - AJ or AA.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, May 12, 2004
    #15
  16. Eric

    Ben Pfaff Guest

    Mark McIntyre <> writes:

    > On Wed, 12 May 2004 12:17:18 -0400, in comp.lang.c ,
    > (Eric) wrote:
    >
    >> AJ: if (a > b)
    >> AP: if (a>b)
    >> AX: if( a>b)
    >> BF: if(a>b)

    >
    > any of those 4, not fussy. The others, with odd spacing round things, look
    > silly.


    You don't think AX looks silly with its asymmetric spacing?
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, May 12, 2004
    #16
  17. Eric

    Malcolm Guest

    "Eric" <> wrote in message

    AQ > AZ > AA > AJ

    all the rest are either too compact or show very odd use of whitespace.
     
    Malcolm, May 12, 2004
    #17
  18. Eric

    Eric Guest

    Mark McIntyre <> wrote:

    > On Wed, 12 May 2004 21:47:49 GMT, in comp.lang.c ,
    > (Eric) wrote:
    >
    > >Ed Morton <> wrote:
    > >
    > >> Eric wrote:
    > >>
    > >> > I thought it might be fun to run a simple vote to discover the most
    > >> > preferred spacing style for a simple if statement with a single,
    > >> > simple boolean test. By my count, there are 32 possible variations
    > >> > for this case. Here is a complete list.

    > (snip)
    > >> >
    > >> AJ > AA
    > >>
    > >> I'd never consider using any of the rest.

    > >
    > >Whether you'd consider using them or not is irrelevent. The only thing
    > >that matters is whether you see any merit differences between them.

    >
    > Thats a bit rude.


    I'm sorry you feel that way.

    > The question you asked was which we preferred. Ed
    > answered that - AJ or AA.


    I asked people to rank the available options from most to least
    preferred, not to rank only the options they would actually consider
    using.

    It is not necessary to rank all of the available options and two or more
    options can be ranked differently.

    However, if one see a merit difference between two or more of the
    available options, they should be ranked. Only by doing this or assuming
    this has been done can the most preferred option be found.

    Is this clear now?


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, May 13, 2004
    #18
  19. Eric

    Eric Guest

    Eric <> wrote:

    > Ed Morton <> wrote:
    >
    > > Eric wrote:
    > >
    > > > I thought it might be fun to run a simple vote to discover the most
    > > > preferred spacing style for a simple if statement with a single, simple
    > > > boolean test. By my count, there are 32 possible variations for this
    > > > case. Here is a complete list.
    > > >
    > > > AA: if ( a > b )

    > > <snip>
    > > > AJ: if (a > b)

    > > <snip>
    > >
    > > AJ > AA
    > >
    > > I'd never consider using any of the rest.

    >
    > Whether you'd consider using them or not is irrelevent. The only thing
    > that matters is whether you see any merit differences between them.


    I am sorry if my bluntness caused offense.

    I am sincerely interested in whether you see any further merit
    differences among the remaining options.


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
     
    Eric, May 13, 2004
    #19
  20. Eric wrote:

    > I am sorry if my bluntness caused offense.


    I see that you've already met some of our indigenous trolls.
    If you decide to continue your subscription to the comp.lang.c newsgroup,
    you will learn to recognize and ignore them.
     
    E. Robert Tisdale, May 13, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Baxter

    style for coding latches

    Paul Baxter, Aug 10, 2003, in forum: VHDL
    Replies:
    7
    Views:
    1,168
    Mike Treseler
    Aug 15, 2003
  2. Willem Oosthuizen

    Coding style to prioritize certain inputs

    Willem Oosthuizen, Sep 2, 2003, in forum: VHDL
    Replies:
    5
    Views:
    514
    Mike Treseler
    Sep 4, 2003
  3. Analog Guy

    Coding style for CPLD vs FPGA

    Analog Guy, Mar 11, 2005, in forum: VHDL
    Replies:
    3
    Views:
    5,512
    Klaus Falser
    Mar 14, 2005
  4. calmar
    Replies:
    11
    Views:
    960
    calmar
    Feb 21, 2006
  5. Ken Varn
    Replies:
    0
    Views:
    538
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page