Naming variables, functions...

This is a discussion on Naming variables, functions... within the Tech Board forums, part of the Community Boards category; Probably many of you use Hungarian notation for naming variables. But if you don't want to indicate what type of ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    Naming variables, functions...

    Probably many of you use Hungarian notation for naming variables. But if you don't want to indicate what type of the variable it is, how do you name the variable?

    Say, MyVariable, for example.

    I noticed that many put the first word low case, and capitalize other words.

    For example, myVariable.

    Why is that? What is the most common way of naming variables and functions?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I name them to help describe what the variable is for. I only use Hungarian notation when I'm doing windows programming, and then only to identify handles and such.

    For variables that I use and then reassign a lot I give them simple names. My most common ones are tmp#, ch, c#, count, etc (note # means a number not the actual symbol).

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Re: Naming variables, functions...

    Originally posted by Ariod

    I noticed that many put the first word low case, and capitalize other words.

    For example, myVariable.

    Why is that? What is the most common way of naming variables and functions? [/B]
    When you write a variable, you want to have it describe what the variable is. for example, if you wanted a variable to hold the value of some widgets made in 1986, you might write:

    numberofwidgetsmadein1986

    Of course that is kind of hard to read, so you might write it with capital letters to begin each new word, like this:

    numberOfWidgetsMadeIn1986

    Now it is slightly easier to read. I did not capitalise the letter 'n' at the beginning of the variable, as I am lazy, and I don't like pushing shift more than I have to...actually I am lying, as you will see, I like doing more work than a lot of people when I name my variables... No sense in capitalising the first letter though, as its not needing any emphasis, as opposed to the words 'buried' within the variable, like 'widgets' and 'made.'

    Another way to make your variable easier to read (and someone might scream when they see this..lol) is to use an underscore.

    number_of_widgets_made_in_1986

    This is my personal preference (even though it is slower to type with underscores) over against
    Last edited by kermit; 08-18-2003 at 05:01 PM.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    ...over against the 'camel hump' style with the capital letter for the start of each word. I just never found it easy to read camel hump style. Lots of people like it though.

    One other thing, though the variable name should be quite descriptive, it should not be excessively long. A better version of my example variable name might be:

    number_of_widgets_1986

    In this way, you still know what it is. As far as naming functions, you might want to take more care and describe what the function does precisely - in that light, a function name will tend to be longer (if necessary - don't make a name longer than you need to, but don't make it so sparse you forget what the thing does)

    Hope this helps...

    kermit

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I name all my variables one or two character names.

    Err... no, not really, but I did when I was programming in VB. That got QUITE difficult to follow a,b,c,d,e,f,g through m or so, and no comments... lol.

    Keep 'em short and descriptive. I'd change Kermit's
    numberOfWidgetsMadeIn1986 to numWidgetsMade or WidgetsMade. Short; descriptive.
    Away.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    typically variables begin with lower case letters and each additional word in the variable is capitalized as mentioned above. Words that start with an upper case letter are generally used for classes. All caps typically denotes a constant. These are commonly accepted convetions but are not rules. There are other styles.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    My style is generally
    myVariableName or myvar
    MyFunctionName
    MYDEFINENAME

    breaking this convention at programmer's whim...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    In all but a couple of special cases, I call my variable Variable or if two or more word SecondVariable, i.e. with each word capitalised. I shorten some words where this does not introduce ambiguity.

    Exceptions are loop counters, I always start with a lower case i, then j, and so on - hang over from many years of coding in Fortran. Pointers I call pSomething, hang over from an early database system I used whereby that was a requirement, and for some reason, I call handles hSomething.

    Functions follow the same rule as variables.

    With todays IDE's, Hungarian is a relic, if I have forgotten what type a variable is, I put my cursor on it and the IDE tells me and offers to take me to the declaration.

    Often the software house you're working for has installation standards which dictate you must use so-and-so convention for naming.

    *** EDIT ***

    Although I'll agree with WaltP

    >>> breaking this convention at programmer's whim...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Exceptions are loop counters, I always start with a lower case i, then j, and so on - hang over from many years of coding in Fortran. Pointers I call pSomething, hang over from an early database system I used whereby that was a requirement, and for some reason, I call handles hSomething.


    Just curious... did you have to call loop counters i, j, k in Fortran?
    Away.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> did you have to call loop counters i, j, k in Fortran?

    No. The thing with Fortran is that unless you specifically tell the compiler otherwise, any variable name that begins with an i,j,k,l,m,n or o is implicitly typed to be an integer, thus it is not necessary to declare them.

    In fact, for production work, I always insisted all of my coders started their programs with an Implicit None statement, which turned all of the auto declarations off. Thus it was necessary to both type and size all of your variables.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  3. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM

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