New coding standards: use underscores, hyphens or mixed case in command (and identifier) names

Discussion in 'C Programming' started by James Harris, Jul 17, 2004.

  1. James Harris

    James Harris Guest

    Before I embark on a new long-term project I'd appreciate your advice on how to split up
    long names. I would like to keep the standards for command names the same as that for
    variable names. Looking at the examples below, which ones seem better?

    Straight names
    echoclient
    lastcharoffset
    helloworld

    Internal underscores
    echo_client
    last_char_offset
    hello_world

    I could also use embedded hyphens as my minus sign must be surrounded by whitespace
    (please suspend disbelief while looking at these. I know they will look unfamiliar!)
    echo-client
    last-char-offset
    hello-world

    Mixed case
    EchoClient
    LastCharOffset
    HelloWorld

    Initial lower case then mixed
    echoClient
    lastCharOffset
    helloWorld

    In some ways I like the mixed case versions using an inital capital, especially as I may
    want to prefix some names with a code for an abstract data type, which, when present,
    could begin with a lower case. Is this getting too Microsoft-ish? Is it getting to
    Hungarian? Is Hungarian bad when used with abstract data types rather than inbuilt ones?

    Advice on which is or is not thought to be acceptable would be much appreciated. Please
    bear in mind that I intend these names for commands/instructions as well as variables and
    types. Constants would be in all caps.

    --
    Thanks,
    James
    James Harris, Jul 17, 2004
    #1
    1. Advertising

  2. James Harris <no.email.please> scribbled the following:

    > Before I embark on a new long-term project I'd appreciate your advice on how to split up
    > long names. I would like to keep the standards for command names the same as that for
    > variable names. Looking at the examples below, which ones seem better?


    > I could also use embedded hyphens as my minus sign must be surrounded by whitespace
    > (please suspend disbelief while looking at these. I know they will look unfamiliar!)
    > echo-client
    > last-char-offset
    > hello-world


    Since when was the hyphen a legal character in a C identifier?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "How can we possibly use sex to get what we want? Sex IS what we want."
    - Dr. Frasier Crane
    Joona I Palaste, Jul 17, 2004
    #2
    1. Advertising

  3. James Harris a formulé la demande :
    > Before I embark on a new long-term project I'd appreciate your advice on how
    > to split up long names. I would like to keep the standards for command names
    > the same as that for


    There is not standard about that. It may have company rules you are
    supposed to follow (or create)

    > variable names. Looking at the examples below, which ones seem better?


    What do you mean by 'command name' ? No such a thing like a 'command'
    in C.

    > Straight names
    > echoclient
    > lastcharoffset
    > helloworld


    Unreadable to me (and others, I guess).

    > Internal underscores
    > echo_client
    > last_char_offset
    > hello_world


    Ok

    > I could also use embedded hyphens as my minus sign must be surrounded by
    > whitespace


    No such a thing in C.

    > Mixed case
    > EchoClient
    > LastCharOffset
    > HelloWorld


    Ok

    > Initial lower case then mixed
    > echoClient
    > lastCharOffset
    > helloWorld


    Sounds ugly to me, but who cares...

    > In some ways I like the mixed case versions using an inital capital,
    > especially as I may want to prefix some names with a code for an abstract
    > data type, which, when present, could begin with a lower case. Is this
    > getting too Microsoft-ish? Is it getting to Hungarian? Is Hungarian bad when
    > used with abstract data types rather than inbuilt ones?


    Original Hugarian is good,

    a_xxx : array
    p_yyy : pointer
    s_zzz : string
    etc.


    but Hungarian à-la-Microsoft (typed) is evil and hard to read and
    maintain.

    --
    Ceci est une signature automatique de MesNews.
    Site : http://mesnews.no-ip.com
    Emmanuel Delahaye, Jul 17, 2004
    #3
  4. James Harris

    -berlin.de Guest

    James Harris <no.email.please> wrote:

    > Before I embark on a new long-term project I'd appreciate your advice
    > on how to split up long names. I would like to keep the standards for
    > command names the same as that for variable names. Looking at the
    > examples below, which ones seem better?


    Please fix your line lenghts to something of around 70 (I reformatted
    the text to make things readable).

    > Straight names
    > echoclient
    > lastcharoffset
    > helloworld


    > Internal underscores
    > echo_client
    > last_char_offset
    > hello_world


    I would much prefer the second since it's way easier to read and
    typos are easier to recognize. Just compare

    lastcharoffset <-> last_char_offset
    lastcharofset <-> last_char_of_set

    and it should become clear immediately why I feel that way.

    > I could also use embedded hyphens as my minus sign must be surrounded
    > by whitespace (please suspend disbelief while looking at these. I know
    > they will look unfamiliar!)
    > echo-client
    > last-char-offset
    > hello-world


    Huh? Unless you do some automatic conversion of the source code before
    you pass it to the compiler that won't do what you expect...

    > Mixed case
    > EchoClient
    > LastCharOffset
    > HelloWorld


    > Initial lower case then mixed
    > echoClient
    > lastCharOffset
    > helloWorld


    > In some ways I like the mixed case versions using an inital capital,


    Well, I still would prefer the version with underscores since they
    seem still easier to read (you immediately see the subdivision in
    words because we're trained to expect spaces between words) but
    having the upper case at the start of words gives you at least a lot
    more visual clues then without. What becomes a problem with the
    use of upper case characters is when you want to code some extra
    meaning into the use of upper/lower case, e.g. have global variables
    always start with an upper case character but everything else in
    lower case and upper case for all word starts for typedef'ed stuff
    like in

    Var_array <- a global variable
    Var_Type <- a type with a typedef somewhere in the headers

    > especially as I may want to prefix some names with a code for an
    > abstract data type, which, when present, could begin with a lower
    > case. Is this getting too Microsoft-ish? Is it getting to Hungarian?
    > Is Hungarian bad when used with abstract data types rather than
    > inbuilt ones?


    Well, lots of people loath Hungarian notation (and I am admittedly
    one of them) because

    a) all that stuff at the start makes it harder to read since you
    always have to scan for the real variable name, especially when
    used without an intervening underscore and you end up with names
    like 'rgbsyHash' or 'rgwDic' (as taken directly from an article

    http://cm-solutions.com/cms/technical/guide/hungarian_naming_convention.htm

    by its inventor Simonyi, it shows rather nicely why not to use it,
    the example code mostly looks to me as if it had been rot13'ed),

    b) if you're that far away from the definition of a variable that
    it gets tedious to look it up (and you can't remember it) then
    your function probably has already become too long

    c) or, if this is a variable that is defined somewhere else, it
    should have a name that at least gives you a clue to what its
    type is.

    > Advice on which is or is not thought to be acceptable would be much
    > appreciated. Please bear in mind that I intend these names for
    > commands/instructions as well as variables and types. Constants would
    > be in all caps.


    I tend to write function names in lower case only - that it's a
    function normally is obvious because of the parenthesis following
    it.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Jul 17, 2004
    #4
  5. James Harris

    Ed Morton Guest

    Re: New coding standards: use underscores, hyphens or mixed casein command (and identifier) names

    James Harris wrote:

    > Before I embark on a new long-term project I'd appreciate your advice on how to split up
    > long names. I would like to keep the standards for command names the same as that for
    > variable names. Looking at the examples below, which ones seem better?
    >
    > Straight names
    > echoclient
    > lastcharoffset
    > helloworld


    No, you've got to separate words somehow for readability.

    > Internal underscores
    > echo_client
    > last_char_offset
    > hello_world


    OK, but obviously takes one more char than mixed case.

    > I could also use embedded hyphens as my minus sign must be surrounded by whitespace
    > (please suspend disbelief while looking at these. I know they will look unfamiliar!)
    > echo-client
    > last-char-offset
    > hello-world


    No, not legal C and worth avoiding in any language that would allow it.

    > Mixed case
    > EchoClient
    > LastCharOffset
    > HelloWorld


    OK, but the assumption then is that your variables start with an
    upper-case letter and, if you follow that rather than special-casing it,
    then your one-char tmp counter names should be, say, "I" which looks
    weird and probably invades your other naming conventions such as
    reservaing all-upper-case for enum, macro, and type names.

    > Initial lower case then mixed
    > echoClient
    > lastCharOffset
    > helloWorld


    I'd go with that.

    > In some ways I like the mixed case versions using an inital capital, especially as I may
    > want to prefix some names with a code for an abstract data type, which, when present,
    > could begin with a lower case.


    So if you start with lower-case then "helloWorld" would be "xHelloWorld"
    - that seems fine. You could also introduce an underscore between the
    code and the rest of the word if that's important.

    Is this getting too Microsoft-ish? Is it getting to
    > Hungarian? Is Hungarian bad when used with abstract data types rather than inbuilt ones?
    >
    > Advice on which is or is not thought to be acceptable would be much appreciated. Please
    > bear in mind that I intend these names for commands/instructions as well as variables and
    > types. Constants would be in all caps.


    By commands/instructions, I assume you mean "functions" and the above
    would be fine for those.

    Ed.

    > --
    > Thanks,
    > James
    >
    >
    Ed Morton, Jul 17, 2004
    #5
  6. James Harris

    runtime Guest

    In other news, Ed Morton <> was quoted today as
    saying:
    > James Harris wrote:


    > No, not legal C and worth avoiding in any language that would allow
    > it.
    >
    >> Mixed case
    >> EchoClient
    >> LastCharOffset
    >> HelloWorld

    >
    > OK, but the assumption then is that your variables start with an
    > upper-case letter and, if you follow that rather than special-casing
    > it, then your one-char tmp counter names should be, say, "I" which
    > looks weird and probably invades your other naming conventions such as
    > reservaing all-upper-case for enum, macro, and type names.


    The answer to which might be to avoid one-char tmp counter names ;)

    >> Initial lower case then mixed
    >> echoClient
    >> lastCharOffset
    >> helloWorld

    >
    > I'd go with that.
    >


    This is what I find myself using as it feels more natural than the others.

    --
    runtime
    runtime, Jul 17, 2004
    #6
  7. Be consistent and choose something readable.

    thisisalongname() is not readable and I can't see many people would endorse
    this way.

    this_is_a_long_name() is fairly accepatble I feel. However at the moment I
    use a style like this:


    ModuleX_ThisIsALongName()

    I use the uncapitalised method for local variables:

    size_t line_count;

    and sometimes I use an 'a' prefix for arguments, although I am not entirely
    happy with this:

    fn( aDest, aSrc, aLen )
    {
    }

    It's not easy to say what is best, but I would avoid anything too
    extravegant like hungarian notation, I prefer naming conventions to give
    information about the semantects of an identifier rarther than it's type.
    i.e. I use aParamater, rathar than saying iParamater ( meaning integer )
    because then you might have to rename all your identifiers if they change
    type.
    Spacen Jasset, Jul 17, 2004
    #7
  8. James Harris

    Malcolm Guest

    "James Harris" <no.email.please> wrote in message
    >
    >
    > Before I embark on a new long-term project I'd appreciate your advice on
    > how to split up long names. I would like to keep the standards for command
    > names the same as that for variable names. Looking at the examples below,
    > which ones seem better?
    >

    You are not using C. However you should have very few variables in scope in
    any structured programming language. Variables within a function can be
    called anything meaningful (eg i for a nondescript counter, N for a count of
    objects, x and y for co-ordinates, theta for an angle, ptr for a pointer,
    ojprice for the price of orange juice etc). The few global variables you
    need should be prefixed g_ and then called anything.

    However you could have many hundreds of function identifiers. What I do is
    name portable (in C, pure ANSI C ) functions in lowercase, whilst
    platform-specific functions have Uppercase Initials.
    Normally you won't have namespace clashes for your portable functions,
    because you are developing them as a corpus. The name can be kept relatively
    short. The exception is when you are doing many operations on a structure
    (eg to get the salary of a programmer, and artist, and a manager). What you
    do is prefix every function with the name of the structure you pass it, so
    we have

    programmer_salary()
    artist_salary();
    manager_salary();

    You shouldn't need longer names, eg programmer_initial_salary is bad because
    it rapidly gets unreadable. Try to think of a short identifier instead.

    Platform-specific functions can multiply to huge numbers, particularly is
    you are writing an interactive program, so initialise the first letter of
    each word.

    MoveMouseCursor()

    etc.

    The problem with both underscores and capitals is that they are not
    pronounced. Most people remember words by sound, because that is the primary
    way we use language. Thus mouse_wheel and mousewheel rapidly become
    confused. So it is important to stick to some consistent convention.
    Malcolm, Jul 17, 2004
    #8
  9. James Harris

    Michael Guest

    hungarian notation might be somthing you can look into but I dont'
    think you will read all that. It really is pointless(IMO) I use
    the opengl way of declaring functions ex:

    glTexSubImage();

    although I have programmed in GTK and they use the _ method

    gtk_radio_button_new_with_label_from_widget

    the latter method really starts to hurt your wrist if you are writing
    a long program and use the correct typing method(Too must reaching +
    it throws off the rythm of typing(slows you down)). I would recommend
    the former Faster and one less keystroke
    Michael, Jul 17, 2004
    #9
  10. "James Harris" <no.email.please> wrote in message
    news:40f91832$0$7803$...
    >
    > Before I embark on a new long-term project I'd appreciate your advice on
    > how to split up long names. I would like to keep the standards for
    > command names the same as that for variable names. Looking at the
    > examples below, which ones seem better?


    Which style you use is a matter of personal preference, as long as you pick
    a single style and stick with it.

    > Initial lower case then mixed
    > echoClient
    > lastCharOffset
    > helloWorld


    This is what I use, but the initial-caps style and underscored (not
    hyphenated) style are both common as well.

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov
    Stephen Sprunk, Jul 20, 2004
    #10
  11. James Harris

    kal Guest

    "James Harris" <no.email.please> wrote in message news:<40f91832$0$7803$>...

    Are you "James S Harris"?
    kal, Jul 20, 2004
    #11
  12. On Tue, 20 Jul 2004, kal wrote:
    >
    > "James Harris" <no.email.please> wrote...
    >
    > Are you "James S Harris"?


    If he is, he's using a different (munged) email, no middle initial,
    and a much, much saner brain. I doubt there's any relation. :)
    (James: JSH is a well-known sci.math kook, in case you haven't
    already heard it a thousand times before. ;)

    -Arthur
    Arthur J. O'Dwyer, Jul 20, 2004
    #12
  13. James Harris

    Dan Pop Guest

    In <> (kal) writes:

    >"James Harris" <no.email.please> wrote in message news:<40f91832$0$7803$>...
    >
    >Are you "James S Harris"?


    What was your C language question?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 20, 2004
    #13
  14. James Harris

    Sam Halliday Guest

    Re: New coding standards: use underscores, hyphens or mixed case incommand (and identifier) names

    Dan Pop wrote:
    > kal writes:
    > >Are you "James S Harris"?

    >
    > What was your C language question?


    well then, what was YOUR C language question?
    Sam Halliday, Jul 20, 2004
    #14
  15. James Harris

    Dan Pop Guest

    In <20040720182631.6de53ef8@localhost> Sam Halliday <> writes:

    >Dan Pop wrote:
    >> kal writes:
    >> >Are you "James S Harris"?

    >>
    >> What was your C language question?

    >
    >well then, what was YOUR C language question?


    Topicality issues are, *always*, topical. So, my question was topical
    even without being about C.

    And your point was?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 20, 2004
    #15
  16. Emmanuel Delahaye <> wrote in
    news::

    >> Initial lower case then mixed
    >> echoClient
    >> lastCharOffset
    >> helloWorld

    >
    > Sounds ugly to me, but who cares...


    I do. I'd use EchoClient to be a type and echoClient to be an "object".
    E.g. EchoClient echoClient; or Foo foo; Leading cap. followed by lower
    case always means a type (not the standard types mind you) in my little
    world.

    My simple 3 rules:

    1) manifest constant or enum:

    #define FOO_ONE 1
    enum Foo { FOO_ONE = 1 };

    2) Types or tags:

    typedef enum Foo { ... } Foo;
    typedef struct Bar { ... } Bar;

    3) Vars. and functions:

    int someVariableOne;
    char arrayOfChars[NUM_CHARS];
    float getFloatFromBar(Bar bar);

    --
    - Mark ->
    --
    Mark A. Odell, Jul 20, 2004
    #16
  17. James Harris

    kal Guest

    [OT]Re: New coding standards: use underscores, hyphens or mixed case in command (and identifier) names

    (Dan Pop) wrote in message news:<cdjadi$2de$>...

    > >Are you "James S Harris"?

    >
    > What was your C language question?


    Don't be so hasty. I am thinking, I am THINKING...

    At present I only have C anxiety.

    I enjoyed hanging out at sci.math (wonderful folks there,
    they are infinitely patient while explaining things to you)
    till the discussion about FLT got way too "technical" for
    me and my antidiluvian computer.

    Just was wondering if I should pack my bags again.
    kal, Jul 20, 2004
    #17
  18. James Harris

    James Harris Guest

    "kal" <> wrote in message
    news:...
    > "James Harris" <no.email.please> wrote in message

    news:<40f91832$0$7803$>...
    >
    > Are you "James S Harris"?


    No, not that one!
    James Harris, Jul 20, 2004
    #18
  19. James Harris

    James Harris Guest

    "Emmanuel Delahaye" <> wrote in message
    news:...

    > What do you mean by 'command name' ? No such a thing like a 'command'
    > in C.


    I was thinking of executable file names, mainly. Hence the hyphenated
    versions. I was thinking of the user having to type the command to run the
    program so,

    helloworld
    hello-world
    hello_world
    helloWorld
    HelloWorld

    I know a file name format is not C per se but it is often used to run a C
    program - my only excuse for including it in this NG.

    I must admit the hyphenated version is growing on me because it doesn't
    require the user to think about or change case, separates the words of the
    command (if it has more than one) and doesn't require the user to use the
    underscore character. Hmm....... Is a hyphen so bad....?


    --
    James
    James Harris, Jul 20, 2004
    #19
  20. James Harris

    James Harris Guest

    <-berlin.de> wrote in message
    news:...
    >
    > Well, lots of people loath Hungarian notation (and I am admittedly
    > one of them) because
    >
    > a) all that stuff at the start makes it harder to read since you
    > always have to scan for the real variable name, especially when
    > used without an intervening underscore and you end up with names
    > like 'rgbsyHash' or 'rgwDic' (as taken directly from an article
    >
    >

    http://cm-solutions.com/cms/technical/guide/hungarian_naming_convention.htm
    >
    > by its inventor Simonyi, it shows rather nicely why not to use it,
    > the example code mostly looks to me as if it had been rot13'ed),


    Ow! I see what you mean. That's not what I was thinking of. I had seen
    Hungarian as szVaviable as a string terminated by a zero. I've never used
    the codes so this may not be right but something like iVariable being an
    integer. (Memories of Fortran....?)

    I can see a benefit to having the broad type of a variable clearly
    visible. - e.g. is it a number or a string? I don't know how far I would
    want this to go, though. Any suggestions?

    --
    Thanks,
    James
    James Harris, Jul 20, 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. Kari Laitinen

    Opinions about underscores in names

    Kari Laitinen, Nov 13, 2003, in forum: C++
    Replies:
    2
    Views:
    316
    Ekkehard Morgenstern
    Nov 13, 2003
  2. Kobu
    Replies:
    7
    Views:
    447
  3. Daniel Brockman

    Hyphens vs. underscores

    Daniel Brockman, Jun 12, 2005, in forum: Ruby
    Replies:
    0
    Views:
    116
    Daniel Brockman
    Jun 12, 2005
  4. Paul Hepworth

    hyphens in variable names

    Paul Hepworth, Jul 7, 2006, in forum: Ruby
    Replies:
    7
    Views:
    140
    Paul Hepworth
    Jul 7, 2006
  5. joe
    Replies:
    1
    Views:
    112
    Avdi Grimm
    Mar 3, 2008
Loading...

Share This Page