94

In all honesty, if there was a course on giving variables good, informative and not completely stupid names... I would take it.

Comments
  • 4
    I usually start off with short, cryptic names because I find it easy to work with in the beginning of a project.
    When I start refactoring things I try to make sense of what the variable does (booleans usually get a name like ...Toggle or ...Mode, factors ...Factor, counters ...Counter and so on). In my opinion this makes for great readability conpared to using plain names.

    Also, this way, there are no random variables called 'temp' with unknown meanings (tempCounter, tempTokens, tempText etc is a whole lot better than just temp).

    Hope this helps a bit...
  • 0
    @hendrik actually yes, thank you ^^
  • 0
    I comment with variable names
    Like
    hasUserVisitedInbox = checkInbox()
  • 1
    Wish there was a dictionary for developers
  • 3
    Always go for descriptive over short. Every ide on Earth has auto fill now, there's no excuse for using shorter less readable variables. Only exception is mathematical or physical quantities, ie pi, x, y, dv, etc.
  • 1
    boolean shouldVariablesBeLongAndDescriptiveToAvoidConfusionAndUseCamelCase = true;
  • 1
    There's a whole chapter on this in some c++ book.
  • 1
    Descriptive over Cryptic but Concise over Illustrative.

    Variable should give a *short* description of what they're meant for. Single characters and abbreviations are too short, sentences are too long.

    And ask yourself often, do I really need to store something in a variable, or can I use & return values directly and worry only about good *method* names?
  • 1
    var foo = "bar";
  • 0
    @TangChr Maybe I'm alone in this, but I really dislike the whole foo bar baz fart thing. Examples are so much easier to understand with realistic examples!
  • 0
    var a = superImportant.name;
  • 0
    Funny so I was talking to a smart colleague (that's a compliment where I work) about how all the variable names are in different styles.

    i.e. Testcase,testCase, Testcase

    Him: yea we have a style doc but no one bothers to read it
  • 0
    I'd love to read a book about that. Specially about defaults.

    I begin using variable like name, address, path, then I need a second name and things begin to mess up: name, nameSecondPerson (just a temp variable), address, Email (yeah, now I have capitals), work_address, dogsName and go on.

    At the end i need to use the autocomplete the variables names, I as have no idea anymore. Last, I have to stop and reorganize code, but if course I usually never get to the point to finishes organizing.
Add Comment