When programming, what naming conventions do you use for your variables? I don't mean when the name of the variable should be obvious; ie. sum, total, first, last. But when you name variables that don't really fit into a category/obvious structure, what sort of names do you use? Is it, myVar1 or test1 or variable1, etc...?
closed as not constructive by Walter, gnat, ChrisF♦ Aug 2 '12 at 17:03
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.
|
There always must be a purpose for a variable, otherwise you can leave it out and you don't need a name. So use it's name to identify the purpose for a variable. e.g. I sometimes introduce variables for the sole purpose of keeping a value that needs to be returned. I might call that variable returnValue e.g. sometimes it is a temporary for a different variable, I might use tempUserName hope this helps. |
|||||||||||||||||
|
|
For me, these are the basic rules any convention should be based on:
The following are some specific conventions that I consider important. If I had to join a team or project where the conventions already settled upon explicitly violate any of these, I'd started to rebel and make a fuzz about it.
Whether you use |
|||||||
|
|
I use snake_case for all my variables. I try to make them nice and specific and without unnecessary abbreviation. e.g. file_id rather than fid. If I am just testing something, I usually use the first letter of the type. e.g. If I'm in the Python interpreter, I'll use l for a list, s for string, etc. Otherwise I give it a descriptive name. I'm going to steal jensgram's answer for avoiding conflicts though, good idea. |
|||
|
|
|
Two scenarios I can think of:
|
|||||||
|
|
Every variable name should be "obvious" in the sense that it should convey the purpose of the variable in a way that's readable and understandable. Ask yourself "what does this variable represent in terms of the larger problem I'm trying to solve?" The answer to that question is the name of the variable. Don't use names like "myVar" or "variable1"; those names tell you nothing about what the variable represents. And for the love of God, do not perpetuate the abuse of Hungarian notation that encodes primitive type information into the variable name (iCounter, szName, fRoot, etc.) if you can help it. That's not what it's meant for, and it just clutters up code and makes names hard to read. |
|||
|
|
|
If i really have to create variable which i do not need (because of language semantics), I use Otherwise, as KeesDijk stated, every other variable has a purpose and should be named adequatly. If you have a convention to follow (one very common is to use i, j, k for loops, others can include names such as Personally I prefer not to use names like |
|||||
|
