Thread: standard naming conventions?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267

    standard naming conventions?

    is there a standard naming convention in c++?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, there are lots of them in fact!

    This is the one I use:
    • Variable names begin with a small letter and the first letter of each word in the name afterwards is capital (eg. int avgStudentMarks).
    • Classes, Structs, Enums and Public Functions are the same as variables, except they begin with a capital letter.
    • Private & Protected functions are the same as variable names.
    • Class Member Variables ALWAYS start with m_ (eg. std::string m_FirstName).
    • Macros & constants are all capitals.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    This is purly programmer preference, but when using a reference, I do this:

    Code:
    rMyName;
    the small " r " helps to remember I am using a reference to another variable.

    Also, with a variable you have declared as "const", use uppercase letters;

    Code:
    const int MAX_ARRAY = 5;
    Leaving it uppercase ensures you know that you are dealing with a special variable, hence you know it must be constantly assigned a value.
    Double Helix STL

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Any reasonably large company or other organization will have it's own conventions.

    I personally (when I do "hobby programming" use something similar to cpjust.

    One example is Linux, which of course isn't C++, use all identifiers except macros as lower-case, using underscore to split parts of names up, e.g. avg_student_marks.

    Another example is Microsoft, where they use a "hungarian notation", where a "pointer to zero terminated string" starts with psz then the name, e.g. pszFileName. All pointers start with p, a character starts with c, an integer with i, structures with "structname", e.g. a "struct rect" would be rectDrawRegion - and a pointer to a struct rect would be prectDrawRegion. This makes it easier to understand what the type of a variable is without looking up at the top of the function. Of course, opponents to this techniqeu would say that it adds "meaningless" info to the variables, since if you already know what type the variable is, then you don't need these "name decorations".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I like joels article about wrong understood vs. useful type of hungarian notation.

    http://www.joelonsoftware.com/articles/Wrong.html

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There are as many styles as there are programmers. (Yes, I've said that before.)

    It's one of those things. Indentation. Whitespace. Naming conventions. Mention them and you often start a flame war . . .

    Also, with a variable you have declared as "const", use uppercase letters;
    This applies to preprocessor stuff as well. (cpjust mentioned this.) It's a convention inherited from C, and it's reasonably widespread.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    I use a special naming convention where if the next letter in the word as ascii code is equal to the start of the next letters ascii code I switch the letter to the third letter if exsisting and delete it and type something random.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by DrSnuggles View Post
    I use a special naming convention where if the next letter in the word as ascii code is equal to the start of the next letters ascii code I switch the letter to the third letter if exsisting and delete it and type something random.
    OK, that just caused my brain to crash... I sure hope you're just being sarcastic though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  2. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM