[RESOLVED] Coding standards (rules)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 50

Thread: [RESOLVED] Coding standards (rules)

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

    [RESOLVED] Coding standards (rules)

    Hello guys!

    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...
    1. Use typedef for all data types
    2. Use the following block style[code]void foo() {
    ...
    }
    3. Capitalize the first letter of a function
    4. No Hungarian notation
    5. And so forth...

    I've read the general coding standards
    but I'm far more interested in hearing from all the working professionals here,
    so that in the future when I get a job, I'd be better prepared.

    As always, thanks!

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Coding standards (rules)

    Here's a pretty good one. It covers a lot of things, so it's easy to compare it to your own style.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,263

    Re: Coding standards (rules)

    Have you read C++ Coding Standards by Sutter and Alexandrescu? There is alot more to be considered than just stylistic guidelines.
    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Coding standards (rules)

    My company doesn't have a single monolithic set of coding standards, but there are guidelines within some specific projects.

    Personally when I'm working with existing code I just follow the extant style, and when I'm writing my own I impose my preferred style. Really not a fan of your #2 incidentally.

  5. #5
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Coding standards (rules)

    IMPO, Braces should be on their own line 99.5% of the time. The other 0.5% it may be acceptable to put the entire "construct" on one line:
    Code:
    void foo() { return 1; }
    This is typical when there are many of the same time sequentially in the file...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; 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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Coding standards (rules)

    Quote Originally Posted by potatoCode View Post
    2. Use the following block style[code]void foo() {
    ...
    }

    As always, thanks!
    Words cannot describe how annoying I find that style of brace use.
    Code:
    void foo()
    {
        //code
    }
    is so much easier to see.

    I use hungarian notation frequently.

    I'd add that white space is important

    Don't write multiple statements on a single line

    Don't include superflous comments

    Use meaningful variable and function names
    Last edited by GCDEF; February 6th, 2009 at 12:14 PM.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Coding standards (rules)

    The most important thing is: Think through every function you add to an interface completely. If knowing how to use it requires any understanding of the internal implementation, you're doing it wrong.

  8. #8
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Coding standards (rules)

    Quote Originally Posted by GCDEF View Post
    I use hungarian notation frequently.
    This is one I find very interesting. Consider how many class names there are and the FLA's (First Letter Acronyms) and other abbreviations regulrly clash.

    For example what your use you for variables of the following types:
    Code:
    std::vector<int>  
    std::vector<double>
    std::vector<vector<double>
    std::vector<std::pair<int,double>>
    std::vector<std::pair<doublie,int>>
    I would find svspidData and svspdiData to be very confusing to refer to the last two types...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; 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. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,263

    Re: Coding standards (rules)

    Quote Originally Posted by TheCPUWizard
    For example what your use you for variables of the following types:
    I think that it depends on which variant of Hungarian notation is used. Joel Spolsky points out in Making Wrong Code Look Wrong that Simonyi's original Hungarian notation concept was concerned with the semantics of the variables, not their types as declared in the program. I tend to be more sympathetic towards this "Apps Hungarian", although I still do not understand why such an encoding is really necessary when spelling out the word would be easier to read for those uninitiated to the abbreviations.
    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

  10. #10
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Coding standards (rules)

    Quote Originally Posted by GCDEF View Post
    I use hungarian notation frequently.
    Yuck!
    Why??

    I know in some cases its forced on us if you program in MFC or win32 but even then I'd only use it the absolute minimum you can get away with.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  11. #11
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Coding standards (rules)

    Quote Originally Posted by laserlight View Post
    I think that it depends on which variant of Hungarian notation is used. Joel Spolsky points out in Making Wrong Code Look Wrong that Simonyi's original Hungarian notation concept was concerned with the semantics of the variables, not their types as declared in the program. I tend to be more sympathetic towards this "Apps Hungarian", although I still do not understand why such an encoding is really necessary when spelling out the word would be easier to read for those uninitiated to the abbreviations.
    Even semantic usage gets a bit complex to say the least...

    I find that the vast majority of the time simply hacing the class name in ProperCase a local variable with the exact same name in camelCase is sufficient.

    For "Collection" classes, I tent to end with the Collection Type (CarList, StateMap, etc) variables can then simple be plural)

    (psuedo code)
    Code:
    CarList cars;
    foreach (Car car in cars)
    {
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; 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

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Coding standards (rules)

    Quote Originally Posted by Russco View Post
    Yuck!
    Why??

    I know in some cases its forced on us if you program in MFC or win32 but even then I'd only use it the absolute minimum you can get away with.
    Habit I guess. Mainly just for PODs.

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

    Re: Coding standards (rules)

    Quote Originally Posted by TheCPUWizard View Post
    IMPO, Braces should be on their own line 99.5&#37; of the time.
    Quote Originally Posted by GCDEF View Post
    Code:
    void foo()
    {
        //code
    }
    So you also write
    Code:
    if (a==b)
    {
      foo();
    }
    else
    {
      bar();
    }
    ?

    And you find that easy to read?
    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.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Coding standards (rules)

    Personally I usually omit braces entirely when it's a single-line statement. Either that or use ? notation.

  15. #15
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Coding standards (rules)

    Take a look at this. Evidently, Bjarne helped develop some coding standards.
    http://www.research.att.com/~bs/bs_faq2.html

    I used to use hungarian notation a bit. It is flexible. I like the idea of including scope into variable names to avoid confusion between locals and members. Globals should be within a namespace at the very least which helps to clarify when using the '::' operator to indicate where the variable is located. It gets a bit ridiculous when you try to include type info into the variable name.

Page 1 of 4 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center