Click here to Skip to main content
15,881,712 members
Articles / All Topics

Robert’s Rules of Coders: #3 Use Good, Meaningful Names

Rate me:
Please Sign up or sign in to vote.
4.47/5 (18 votes)
6 Aug 2015CPOL4 min read 23.2K   8   17
Robert’s Rules of Coders: #3 Use Good, Meaningful Names

Code is much easier for humans to read when the variable and function names reflect what they mean and do. In ancient times, like the 1960s, some programming languages did not support long variable names, but today almost every language supports long names with no penalties. Although some exceptions may be tolerated for very common usages such as the variables “I”, “j”, and “k” for loops, you should strive to use meaningful names in all cases. Would anyone honestly say that the first block of code below is easier to understand than the second?

Code with and without good names

Code with and without good names

Good, meaningful names not only make your code more readable, but they may also remove the need for you to add comments or documentation to the code.

When we first began writing JavaScript in the 1990s, we used short variable names because we wanted to reduce the size of the files to make them faster. Today, you should probably not do this because the data is compressed by default by technologies like gzip, and Internet speeds are faster, and if you are writing enough code that the size of your JavaScript is a factor you should be using a minifier tool to make the JavaScript smaller for you.

Aside from long names, what makes a variable or function name good?

  • Good names reflect the value stored in the variable or explain what the function does. “Counter1″ is better than “c”, but “ActionCounter” is even better. Giving variables relevant names reduces the time it takes people in the future looking at the code to figure out what it does. And that includes the future you. This applies to function names also. A function named “UpdateActivityCount” is better than “CountUpdater”.
  • Try not to use negative logic in variable and function names. It takes our brains a little longer to deduce the meaning of negative logic statements.
    • IsValid is usually a better name than IsNotValid
    • DoesValueExceedMaximum is usually better than DoesValueNotExceedMaximum
  • Follow the conventions of the language you are coding in. If the language uses CamelCase, you should use CamelCase. If the language uses Hungarian Notation (txtFirstName, intItemCount), you should use Hungarian notation. If the language uses all upper case for the names of CONSTANTS, you should use all upper case for CONSTANTS too.
  • Don’t reuse variable names for different purposes.

    Example of Reusing a Variable

    Example of Reusing a Variable
  • The function name should describe what it does not how it does it. This allows you to change how the code in the function works without needing to change the function name. This helps the function remain a black box.
    • Name the function GetAllCustomers, not CallDatabaseUsingADOtoGetDataFromCMCUSTTable.
  • Avoid names that might match keywords. Naming a variable “Name” or “Date” might compile safely at first, but it might fail after an update to the language is installed, or if you include a new library in the project. Also, it may not be obvious to other developers that the variable name is one that you created instead of one that is built in.
  • Follow a consistent pattern
    • If your Foo object has a Retrieve method to get data from the database, then your Bar object should also have a Retrieve method to get data from the database, not a Get method. Using the same method and property names across objects when the methods serve the same functions reduces the learning curve for new developers.
    • You should also strive to use consistent noun and verb pairings. Naming two functions, CalculateLastSaturday and CalculateLastSunday is usually easier for programmers to remember than having one function named CalculateLastSaturday and the other named FigureOutFinalSunday.

Finally, remember to rename your variables if the meaning of the value they store changes and rename your functions if the things the function does changes. We are writing code for humans, not for computers.

Are there any more good rules that should be added to this list?

Image 3 Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Kraft Software LLC
United States United States
Rob Kraft is an independent software developer for Kraft Software LLC. He has been a software developer since the mid 80s and has a Master's Degree in Project Management. Rob lives near Kansas City, Missouri.

Comments and Discussions

 
QuestionUsing an upper case first letter for type names Pin
Gerd Wagner28-Sep-15 10:48
professionalGerd Wagner28-Sep-15 10:48 
QuestionVoted 5, but isn't that an error for Totalprice Pin
Kirk 1038982128-Sep-15 7:57
Kirk 1038982128-Sep-15 7:57 
AnswerRe: Voted 5, but isn't that an error for Totalprice Pin
Rob Kraft29-Sep-15 14:33
professionalRob Kraft29-Sep-15 14:33 
QuestionHungarian C# Pin
Gerd Wagner28-Sep-15 3:13
professionalGerd Wagner28-Sep-15 3:13 
AnswerRe: Hungarian C# Pin
Rob Kraft28-Sep-15 4:17
professionalRob Kraft28-Sep-15 4:17 
AnswerRe: Hungarian C# Pin
Thomas Chizek28-Sep-15 7:39
Thomas Chizek28-Sep-15 7:39 
SuggestionRe: Hungarian C# Pin
David A. Gray28-Sep-15 9:32
David A. Gray28-Sep-15 9:32 
GeneralRe: Hungarian C# Pin
Thomas Chizek28-Sep-15 12:38
Thomas Chizek28-Sep-15 12:38 
GeneralRe: Hungarian C# Pin
David A. Gray28-Sep-15 14:46
David A. Gray28-Sep-15 14:46 
GeneralRe: Hungarian C# Pin
Thomas Chizek29-Sep-15 0:58
Thomas Chizek29-Sep-15 0:58 
Sigh, look I have over 30 years of real world experience as well. I did read your article, I do understand your conventions, agree with them, and use something similar to them. What I am objecting to and this is a general objection that you are catching the brunt of is the generalization of this type of naming standard as "Hungarian". There was a very specific naming convention that had/has that meaning, I (and it sounds like you) lived through the time period when it was all the rage. Calling these variants Hungarian makes the younger programmers confused as to what the convention actually is and why people who lived through it dislike it so much.

I didn't explain this very well, and I should probably write an article explaining my dislike of this kind of generalization. While my opinion will not have an impact on the overall industry this particular thread caught my eye on a day when I was feeling particularly old and grumpy about people that I thought hadn't lived through what I consider "Hungarian Hell" making comments. So I shot off my first two before rereading your article - for that I apologize, however from my read of your article all you did is repackage the prefix ugliness of Hungarian which is one of the parts of it that I truly dislike.. What I don't apologize for is my dislike of everyone calling these naming conventions Hungarian, in my opinion you should find a different name or not jump into articles about not using Hungarian.
GeneralRe: Hungarian C# Pin
David A. Gray29-Sep-15 10:21
David A. Gray29-Sep-15 10:21 
AnswerRe: Hungarian C# Pin
Kirk 1038982128-Sep-15 7:54
Kirk 1038982128-Sep-15 7:54 
GeneralMy vote of 3 Pin
KarstenK16-Sep-15 6:42
mveKarstenK16-Sep-15 6:42 
GeneralRe: My vote of 3 Pin
David A. Gray28-Sep-15 9:10
David A. Gray28-Sep-15 9:10 
GeneralMy Vote 5 Pin
Dharmesh .S. Patil13-Sep-15 19:35
professionalDharmesh .S. Patil13-Sep-15 19:35 
QuestionExcellent Article Pin
Tom Granados10-Aug-15 1:28
Tom Granados10-Aug-15 1:28 
AnswerRe: Excellent Article Pin
David A. Gray28-Sep-15 9:35
David A. Gray28-Sep-15 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.