Are there any tutorials on how to name variables?

as you can probably tell from my previous posts, I have terrible naming conventions. do you know of any textbooks on how to name the material?

+4
source share
12 answers

All answers here are perfectly valid. The most important thing: to be consistent.

So here are my rules (C #):

  • camelCase ids - I personally find it a lot easier to read than underscores
  • Public properties begin with a capital letter
  • Something I should never touch on underscore - an example, a support field for a property should be touched on a property. If I had stresses elsewhere, I know that I am wrong
  • Hungarian applications where Corresponding are ints describing a string. Identifiers can be called rowSelected , rowNextUnread , et so on. This is different from Systems Hungarian, which will mark them as ints such as iSelected , iNextUnread . The Hungarian system does not add much if anything where Hungarian Apps provides information like no: it tells me to add rowItemsPerPage and colSelected is a meaningless operation, although it compiles just fine.
+4
source

I don’t think that there will be good tutorials, because there are no strict rules. Here are some suggestions:

  • Conforms to the standard: cycle variables i , j and k ; variable numbers of arguments go to *args and **kwargs ; use camelCase or underscored_names.

  • Be consistent.

  • Be brief. list_of_drugs_used_in_this_program much less clear than drugs . Similarly, you do not need to include the data type of the variable in the name: drugs_list is redundant.

  • Do not cross overboard with underscores. I never needed more than one. 2+ clicks it.

  • Never use metasynthetic variables ( foo , spam ...) in anything other than quick and dirty examples. method1 also missing.

But you can summarize all this with:

Do not be stupid.


Tee hee.

Variable naming conventions can often turn into a religious war, but Im completely sure when I declare the Name of the world's worst variable:

 $data 

Of course, his data! This is what the variables contain! That is all they can ever contain. It’s like youre packing your things to move to a new house, and on the side of the box you are writing in a large black marker, ā€œMatterā€.

http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html

+7
source
  • A bad agreement that follows completely is better than a combination of different good ā€œconventionsā€ (which are not agreements at all if they are not supported).
  • However, an agreement that does something less comprehensible than if it were ignored should be ignored.

These are the only two that I would call any rule. In addition to these conditional preferences, it is a matter of opinions quickly turning into tirades. The rest of this post is for sure, and should not be read like anything else.

  • For collections, use natural language plurals. In English, this means data, patterns, children, indices, criteria, formulas (and, indeed, tricks, geese, legs, men, women, teeth), not composed words, such as binding patterns, children’s, indices, criteria, formulas (and similar tricks, geese, legs, men, women, teeth, believe it or not, I really saw some of those used). The camel case and abbreviation cause damage to English, as it does not do more. Well, I never saw data, but I saw meta-plural data. Sweet Aradia, why?
  • However, use American English for names, even if you use a different dialect in English. Most coders with such dialects have learned to think of ā€œcolorā€ as a word for colors in the context of a computer under the age of 12, and this principle is applied more widely. If we can handle the ā€œcolorā€ (one of Webster’s worsening bites), we can deal with -ize and -ization (-ise and -isation are pseudo-French 18th C-affectations anyway, Americans are traditionalists On this).
  • Similarly, if you don’t know how to spell a word that you use as an integer or part of a name, look at it (google and see what Google says). Someone may be distracted for a long time by your spelling error, which spreads so liberally throughout the running code that it can be eliminated.
  • Hungarian is bad (in many modern languages, although in some it has its place), but the principle is good. If you can specify a constant, static, instance, local and parameter variable from each other in a split second, that’s good. If you can immediately say something about the intention, that’s good too.
  • In this regard, _ before public variables makes them non-CLR-compatible. This is a really good thing for private variables (if you make them public for a quick experiment and don't forget to capture visibility, the compiler will warn you).
  • Remember the Postel Act, "Be Conservative in What You Do, Be Liberal in What You Accept from Others." One example of this is to act as if you were using case-sensitive langauge, even if you are case-insensitive. Associated with it should be more famous in public names than private ones. Another is to pay more attention to the following conventions than to complain about those who do not.
+5
source

Not a tutorial ... more like a guide / best thing:
http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx

+3
source

I would recommend purchasing a copy of Robert C. Martin's "clean code." It is full of great suggestions, from naming conventions to how to write easy-to-understand functions and more. Definitely worth a read. I know this has influenced my coding style since reading it.

+3
source

hi for me you should always specify the most explicit names:

 string_to_hash = "blabla" hash(sring_to_hash) 

and follow the pep8 style manual . After that, the code should be easy to read.

+3
source

There are many different ideas about the specifics of naming conventions, but the general meaning can be summarized as:

Each variable name must be relevant to any data stored in the variable.

Your naming scheme must be consistent .

That way, the major no-no will be single-letter variables (some people use i and j for indexing loops, which is normal because every programmer knows what they are. However, I prefer 'idx' instead of 'i'). There are also names like "method1", it does not mean anything - it should indicate what the variable has.

Another (less common) convention is the "Hungarian" notation, in which the data type is prefixed with a variable name, such as "int i_idx". This is completely useless in modern object-oriented programming languages. Not to mention the flagrant violation of the DRY principle.

The second point, sequence, is just as important. camelCase, UpperCamelCase, whatever - just don't switch between them for no reason.

You will find that naming conventions vary from language to language and often, a company will have its own naming conventions.

Its a good investment to properly name your variables, because when you come to maintain your code much later, and you forget what it all means, it will pay dividends.

+3
source

You did not indicate what language you are looking for. Since your question is marked with .NET, here is the document that I follow when writing C # code: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx .

+2
source

Have you read Code Complete? He makes a complete treatise on this in the book. Definitely the best naming strategy I've seen in print ... And it's easy to find as many as 1000 programmers with a hat falling, which calls it one of the 5 best resources for programmers and program development.

Just my $ .05

+2
source

Can I make a shameless plug-in for the chapter ā€œNamesā€ in my book ā€œA Sensible Approach to Database Designā€? I specifically talk about names for things in databases, but most of the same considerations apply to variables in programs.

+1
source

It is unclear whether your question relates to Python naming conventions.

If so, for starters I would try to follow only these simple rules:

  • ClassName - ClassName for class names.
  • variable_name - lowercase and underscore for variables (I try to keep them in a nutshell maximum)
+1
source

All Articles