naming conventions?

This is a discussion on naming conventions? within the C Programming forums, part of the General Programming Boards category; I've been trying to get some good naming conventions going for when I use C, but so far I can't ...

  1. #1
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6

    naming conventions?

    I've been trying to get some good naming conventions going for when I use C, but so far I can't find one that I'm truly comfortable with.

    The differences in the two naming conventions I use so far are basically as follows
    Code:
    char bBoolean;
    int nCount;
    
    typedef struct SomeStruct {
    } libSomeStruct_t;
    
    void libSomeFunction(int firstNo, int secondNo);
    and...
    Code:
    char boolean_b;
    int count_n;
    
    typedef struct mystruct {
    } lib_some_struct_t;
    
    void lib_some_function(int first_no, int second_no);
    Where 'lib' designates a prefix for the file or library, which is used for globally accessable objects so I know where it's coming from.

    The first I have taken a little from all the Java I write, so it has similarities there. The second is more in line with all the *nix C code I see.
    I find the advantages of the first type is that long descriptive names seem to be more natural in it. Also, since C doesn't have a boolean type, I like to pre/postfix variables that are to be treated as boolean so I know (ie. bCookieEaten); I do similar things with counter variables (ie. nCookies). I just find having the caps sprinkled in the names a little unattractive.
    The second type I find I can read easier, but makes having longer descriptive names a little more awry. Also, the postfix I like to add for booleans and counters (cookie_eaten_b, cookies_n) really doesn't feel right.

    I've been struggling with this for a while now; just can't get it down. I've tried writing projects with one or the other (never mixed), but neither jive just quite right.

    Anyway, I'm wondering what everyone else uses, or if you all have any recommendations?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    What naming convention you use isn't that important, only that you are somewhat consistent.
    Myself, I sometimes vary between them.
    Like typically like bMyVar.
    int MyFunctionArgument
    I also sometimes use all lower, such int my_arg or int my_function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Also, since C doesn't have a boolean type, I like to pre/postfix variables that are to be treated as boolean so I know (ie. bCookieEaten); I do similar things with counter variables (ie. nCookies).
    That is Hungarian notation of the "systems" variety. I would avoid it on the grounds that you must change a variable name if the variable type changes. If you insist on Hungarian notation, then at least use the version that encodes the purpose of the variable into its name, since that is just another way of providing a meaningful name (though one in which you need to know the code in order to decode the meaning, which is a disadvantage).

    I just find having the caps sprinkled in the names a little unattractive.
    The second type I find I can read easier, but makes having longer descriptive names a little more awry.
    I do not find that to be the case, but we are in the realm of subjective feeling. Just be consistent.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, you are going to be more accepted if you follow an existing pattern when modifiying existing code. So if you are modifying code that uses "CamelCase", then you should continue to use CamelCase for your variables/functions. If you are working on code that use only lowercase and _ to split words, then you should continue to do so. There is nothing worse than trying to remember if functions in a.c are using variant 1, 2 or 3 of naming.

    If it's all your code, then you choose a system, and keep to it. I'm with Laserlight, I prefer mixed upper/lower case. But it's a personal taste thing. In a commercial environment, you don't get a choice (unless you are the person deciding the coding style), since any reasonably large company would have a coding style defined that you should follow, and code-reviews should pick up on bad coding style.

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

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    For variables i use camel casing (thisIsCamelCasing)

    For functions i just uppercase the first letter of each word (SomeFunction())

    For constants every letter is uppercase (THIS_IS_A_CONSTANT) and i use "_" for constants (since they're all uppercase, its hard to determine each word)

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    Suggested reading, from Joel Spolsky, if you are becoming interested in developing conventions (of any type)

    Making Wrong Code Look Wrong

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I used to prefer Hungarian notation when I started programming, but now I don't bother, since the name would need to be changed if the type changes.
    One thing I still use though is p for pointers:
    Code:
    int* pNum;
    char* pStr;
    ...
    I use mixed case with functions beginning with a capital letter and variables beginning with lower-case.
    I usually end typedef names with _t and use all capital letters for macros:
    Code:
    #define SOME_STRING   "This is a string"
    
    typedef unsigned char   Age_t;
    
    Age_t GetAge( time_t  birthDate )
    {
        ...
    }

  8. #8
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6
    Thanks for the advice guys.

    One addendum question here though.
    Is it better to name arrays as plural or singular? I remember being told that singular is more proper, but forgot.

    ie.
    int value[]
    or
    int values[]

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    I don't think it matters, but I prefer singular for the sole reason that it seems strange if you say:
    apples[1] (1 of apples?)
    apple[1] (apple 1)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Use plural nouns for array names.
    Do not use this kind of variable names: MyArrogantVariable
    Always use this kind of variable names: myHumbleVariable;
    For user visible variables, functions name them: myUserFunc, myUserVar;
    For private use variables name them: _myPrivateAffair, _myPrivateStuff;

    If doing OOP then name class, structs, enums etc. like this: MyClass;

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by manav View Post
    Do not use this kind of variable names: MyArrogantVariable
    Always use this kind of variable names: myHumbleVariable;
    Not this nonsense again!
    A big letter is not arrogant.

    And further, you are not in charge of what to use. It's a free society, so we can use any naming convention we want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Elysia, I just suggested. I am not his Team Leader to force it on him.
    Secondly, you can use whatever scheme you like, and you do not even have to be consistent in that, as long as, you work alone for yourself, in a project he, will follow his Team Lead guidance.

    Unless infuriated otherwise, most CBoard members will prefer what I suggested as coding style. If not, well ... matters the least!

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by manav View Post
    Elysia, I just suggested. I am not his Team Leader to force it on him.
    It certainly sounds like you are trying to enforce something. Don't use the word "must" if you don't want to imply that.

    Unless infuriated otherwise, most CBoard members will prefer what I suggested as coding style.
    I wonder about that...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by Elysia View Post
    It certainly sounds like you are trying to enforce something. Don't use the word "must" if you don't want to imply that.
    Did I use the word "must"

    I wonder about that...
    Since I have been posting light hearted stuff, which, appears non sense to some, I wonder, if they will support any valid or semi valid argument of mine.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by manav View Post
    Did I use the word "must"
    Well, I guess not >_< :S
    But you did use "use" and "do" and "do not," thus indicating a similar meaning to "must."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forcing code conventions
    By audinue in forum Tech Board
    Replies: 28
    Last Post: 03-31-2009, 08:46 AM
  2. standard naming conventions?
    By h_howee in forum C++ Programming
    Replies: 8
    Last Post: 11-10-2007, 10:53 PM
  3. Naming variables, functions...
    By Ariod in forum Tech Board
    Replies: 9
    Last Post: 08-19-2003, 12:17 PM
  4. C++ conventions
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 03-14-2002, 07:15 AM
  5. C++ and Program Version Naming
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2001, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21