CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 50
  1. #16
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Coding standards (rules)

    Quote Originally Posted by treuss
    And you find that easy to read?
    Yes, but with a little more indentation, like 4 spaces. This really is just style, something that Sutter and Alexandrescu recommend to "don't sweat the small stuff (or: know what not to standardize)", so it is more of consistency with something reasonable.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  2. #17
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Coding standards (rules)

    Quote Originally Posted by Lindley View Post
    Personally I usually omit braces entirely when it's a single-line statement.
    That's probably the only thing I yell at junior developers for. Too often hours have been wasted trying to find a bug that was caused by changing code from:
    Code:
    if (a==b)
      foo();
    to
    Code:
    if (a==b)
      DBG("a and b are equal");
      foo();
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #18
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Coding standards (rules)

    Quote Originally Posted by potatoCode View Post
    If it's alright, I was wondering if some of you could share with me the policy on the conding standards of the company you guys work for. Example would be...
    I don't work at Lockheed Martin but they've made public their standard for the JSF project. It's especially interesting because Stroustrup has participated so it's not just any company standard.

    http://www.programmingresearch.com/P...ev_D_JUN07.pdf

  4. #19
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Coding standards (rules)

    Thanks guys.
    That's very helpful. One more question.
    Does this mean that not every employer has a set of coding rules?

  5. #20
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Coding standards (rules)

    Quote Originally Posted by laserlight View Post
    Yes, but with a little more indentation, like 4 spaces.
    You indent with spaces instead of tabs? I though only the devil itself would do that?

    ducking and running away.....
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #21
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Coding standards (rules)

    Quote Originally Posted by treuss View Post
    So you also write
    Code:
    if (a==b)
    {
      foo();
    }
    else
    {
      bar();
    }
    ?

    And you find that easy to read?
    Absolutely. It's very easy to see which brace goes with which.

  7. #22
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Coding standards (rules)

    Quote Originally Posted by potatoCode View Post
    Does this mean that not every employer has a set of coding rules?
    It depends very much on the company. "Engineering" style companies, where software is part of a more complex product, is more likely to have it.

    To be effective a coding standard must be comprehensive and go far beyond the "here we put the braces like this" level. And it must be strictly enforced. Occasional "yelling at junior programmers" won't do. Everybody follows the standard or they get shot. Period. And this should be followed up in regular and formal code reviews where programmers explain their code to others.

    Have a look at the JSF standard I posted. It's the best example I've ever seen, although I don't personally like everything. For example instead of

    Code:
    if (....)
    {
       foo();
    }
    I would rather do,

    Code:
    if (....) foo();
    It's probably because I prefer "one line one thought" over "one line one statement".
    Last edited by _uj; February 7th, 2009 at 01:31 AM.

  8. #23
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Coding standards (rules)

    _uj....agreeing with your post...

    1) Many environments now have good tools for enforcing coding standards. For example Visual Studio can be set up with a set of rules that will completely prevent check-in to the source repository if the rules are not followed.

    2) The "one-liner" has a bad history, which is why many coding standards still prohibit it. Back a "few" years ago, many of the debuggers were only capable of setting a breakpoint on the first part of a line. Thus in your example it would not have been possible to set a breakpoint when foo was about to be called (ie after the if evaluated to true). [you probably already knew this, but I figured it could be helpful to the general population]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #24
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Coding standards (rules)

    Quote Originally Posted by treuss View Post
    You indent with spaces instead of tabs? I though only the devil itself would do that?

    ducking and running away.....
    Indenting should always be done with spaces! You can use the tab key, but only if you set it to add 4 spaces (or some consistent number).

    Otherwise you get code that becomes horribly unformatted when viewed on a system with a different tab size....

    Quote Originally Posted by treuss View Post
    That's probably the only thing I yell at junior developers for. Too often hours have been wasted trying to find a bug that was caused by changing code from:
    Code:
    if (a==b)
      foo();
    to
    Code:
    if (a==b)
      DBG("a and b are equal");
      foo();
    I would consider that the fault of the modifier rather than the original coder. My view is that there is no one "right way" to structure all code-----I feel that code should be like English: complete thoughts should be expressed as concisely as possible, and kept separate from each other. Sometimes that means putting in {} for some extra whitespace, sometimes it means omitting them to remove a few lines.
    Last edited by Lindley; February 7th, 2009 at 09:47 AM.

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Coding standards (rules)

    Quote Originally Posted by TheCPUWizard View Post
    2) The "one-liner" has a bad history, which is why many coding standards still prohibit it. Back a "few" years ago, many of the debuggers were only capable of setting a breakpoint on the first part of a line. Thus in your example it would not have been possible to set a breakpoint when foo was about to be called (ie after the if evaluated to true). [you probably already knew this, but I figured it could be helpful to the general population]
    I agree. One statement per line makes debugging much simpler and it's easier to read. It's easier to overlook a statement if it's combined on line with others.

  11. #26
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Coding standards (rules)

    Quote Originally Posted by GCDEF View Post
    I agree. One statement per line makes debugging much simpler and it's easier to read. It's easier to overlook a statement if it's combined on line with others.
    That's bullshit.

    There's no evidence whatsover that this,

    Code:
    if (...) 
    {
    }
    else
    {
    }
    would be better than this,

    Code:
    if (...) {
    } else {
    }
    Who claims otherwise should show scientific evidence.
    Last edited by _uj; February 10th, 2009 at 09:28 PM.

  12. #27
    Join Date
    Aug 2007
    Posts
    858

    Re: Coding standards (rules)

    Quote Originally Posted by _uj View Post
    That's bullshit.

    There's no evidence whatsover that this,

    Code:
    if (...) 
    {
    }
    else
    {
    }
    would be better than this,

    Code:
    if (...) {
    } else {
    }
    Who claims otherwise should show scientific evidence.
    "More than one statement per line" is one of these,

    Code:
    if (...) foo( );
    
    if (...) foo( ); else bar( );
    
    while (...) foo( );
    
    for (int i = 0; i < 10; ++i) foo(i);
    not the type of thing that you posted.

  13. #28
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Coding standards (rules)

    We have a comprehensive document on coding guidelines.
    These formatting and coding standards are designed to enhance the readability and correctness of software produced by our company. They range from ‘good practice’ developed by the C++ community over the years, to ‘look and feel’ rules that ensure consistency of source code style between different developers.

    Readability
    Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. They ensure consistency and simplify the maintenance process with legible code.

    Consistency
    When code has a consistent style and layout it becomes easier for coders to quickly understand something not written by them.

    Bug avoidance
    Some rules are there because they help to eliminate hard to find bugs or unintentional changes in meaning due simple code changes.

    Harmony
    All the coders have to abide by the rules. There's no arguments of whose favourite style is 'best'.

    Image
    It is often a contractual requirement that we deliver the source code to the customer. It is therefore essential that our code is easy for other programmers to read and understand and that it always conveys our professional standards.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  14. #29
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Coding standards (rules)

    Code:
    if (...) 
    {
    }
    else
    {
    }
    We consider this to be the most readable form, which has also been the preferred choice of the majority of coders who have worked here.
    It's also the form I've found to be used in C++ courses I've attended.
    I've noticed that the other form is often used by C++ coders who have recently come across from Java.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  15. #30
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Coding standards (rules)

    Quote Originally Posted by _uj View Post
    That's bullshit.

    There's no evidence whatsover that this,

    Code:
    if (...) 
    {
    }
    else
    {
    }
    would be better than this,

    Code:
    if (...) {
    } else {
    }
    Who claims otherwise should show scientific evidence.
    Wow dude, take a breath. Maybe step away from the computer for a while. Profanity isn't necessary to express a differing opinion.

    FWIW I can't for the life of me think that anybody could look at the second example and find that more readable.
    Last edited by GCDEF; February 11th, 2009 at 09:05 AM.

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured