How to name variables

前端 未结 24 1210
抹茶落季
抹茶落季 2020-12-01 00:37
  • What rules do you use to name your variables?
  • Where are single letter vars allowed?
  • How much info do you put in the name?
  • How about for exam
相关标签:
24条回答
  • 2020-12-01 01:20

    Since I work as a contractor, moving among different companies and projects, I prefer to avoid custom naming conventions. They make it more difficult for a new developer, or a maintenance developer, to become acquainted with (and follow) the standard being used.

    So, while one can find points in them to disagree with, I look to the official Microsoft Net guidelines for a consistent set of naming conventions.

    With some exceptions (Hungarian notation), I think consistent usage may be more useful than any arbitrary set of rules. That is, do it the same way every time.

    .

    0 讨论(0)
  • 2020-12-01 01:21
    • I only use single character variables for loop control or very short functions.
    for(int i = 0; i< endPoint; i++) {...}
    
    int max( int a, int b) {
        if (a > b)
           return a;
        return b;
    }
    
    • The amount of information depends on the scope of the variable, the more places it could be used, the more information I want to have the name to keep track of its purpose.
    • When I write example code, I try to use variable names as I would in real code (although functions might get useless names like foo or bar).
    • See Etymology of "Foo"
    0 讨论(0)
  • 2020-12-01 01:21

    I never use meaningless variable names like foo or bar, unless, of course, the code is truly throw-away.

    For loop variables, I double up the letter so that it's easier to search for the variable within the file. For example,

    for (int ii=0; ii < array.length; ii++)
    {
        int element = array[ii];
        printf("%d", element);
    }
    
    0 讨论(0)
  • 2020-12-01 01:22

    One rule I always follow is this: if a variable encodes a value that is in some particular units, then those units have to be part of the variable name. Example:

    int postalCodeDistanceMiles;
    decimal reactorCoreTemperatureKelvin;
    decimal altitudeMsl;
    int userExperienceWongBakerPainScale
    

    I will NOT be responsible for crashing any Mars landers (or the equivalent failure in my boring CRUD business applications).

    0 讨论(0)
  • 2020-12-01 01:22

    I would say try to name them as clearly as possible. Never use single letter variables and only use 'foo' and 'bar' if you're just testing something out (e.g., in interactive mode) and won't use it in production.

    0 讨论(0)
  • 2020-12-01 01:24

    What rules do you use to name your variables?

    • I need to be able to understand it in a year's time. Should also conform with preexisting style.

    Where are single letter vars allows?

    • ultra-obvious things. E.g. char c; c = getc(); Loop indicies(i,j,k).

    How much info do you put in the name?

    • Plenty and lots.

    how about for example code?

    • Same as above.

    what are your preferred meaningless variable names? (after foo & bar)

    • I don't like having meaningless variable names. If a variable doesn't mean anything, why is it in my code?

    why are they spelled "foo" and "bar" rather than FUBAR

    • Tradition.
    0 讨论(0)
提交回复
热议问题