Subscribe to Martyr2's Programming Underground        RSS Feed
-----

Best Practice: Naming Conventions

Icon 2 Comments
i = 0, j = k + t % rr ok? Do we even know what the heck that is saying? This is the kind of stuff programmers have to deal with when editing someone else's code. What do these things mean? As programmers we should strive to make code better than we found it. A keyword here is "Naming convention". We will explore what it means to have one and what good does it do for us.... next on the Programming Underground!

<MC Hammer version of theme music... complete with muscle pants>

In the book Coding Complete by Steve McConnell, he talks briefly about naming conventions and what they are. It is the process of naming variables, classes, methods and functions in a standard consistent way. One of the more commonly used naming conventions is Hungarian notation where names are specified with the type of object it is and then the name. For instance... if we have an object representing a recordset, the variable may be named rsPatientRecords where the "rs" stands for the type of object, a recordset, and then the name specifying what type of recordset. Other examples include strName for a name string and intRecordTotal for an integer representing the total number of records.

Some programmers like this naming convention and others do not. It is a style issue and while out in the industry you may run into it from time to time. Heck, your company may even enforce this naming convention on all the code you write. By enforcing such a convention, companies can create a more consistent feel and standardization to all code written by the multiple teams. It gets everyone on the same page.

Here on DIC I spend a great deal of time actually deciphering what the programmer means by declaring a variable. By understanding what is meant by the name, I can then get to the problem and provide a solution. It is a readability factor when other programmers (or even yourself) sees the code later and quickly understand what is being done. Here is a great example...

i = s / 3.0;

To tackle this problem above I have to spend time figuring out what "i" and what "s" represent. Seasoned programmers recognize "i" as short for "index" in a loop and that is usually the first thing that comes to mind, especially when it is in a loop. It may or may not be an index. In our example it isn't.

Now here is the same example but with a naming convention that makes things more readable...

dblAvg = dblSumOfNumbers / 3.0;

Ahhh we see that we are calculating an average based on the sum of some numbers divided by 3.0. We can even extrapolate that dblSumOfNumbers is the sum of 3 numbers since we are dividing by three. We also know that average is a double and so is the sum of the three numbers.

Now I am not saying Hungarian notation is the "BEST" of naming conventions... there are many more, but it is usually one taught in universities and in some books for beginners. It is usually pretty easy to pick up and use and is understood by a lot of people in the industry.

"So Martyr, what does this do for me as a programmer? I know what those letters meant, I wrote them!" Yeah you may know them now, but are you sure you will know them a few months or over a year from now? Perhaps you have written 100 other programs since this one and in each your variable "s" represented something else in each one of them. Maybe you are not going to be the next person to see this code. Perhaps you moved up in the world and onto another company that pays you a quarter million dollars a year (yeah right!). That programmer will then have to decipher what "s" means and that costs time... time is money! Plus it introduces the possibility of not fully understanding the problem (if your code was perhaps bugged... yeah like that ever happens!) and then the programmer may include an error of his own.

So he main reasons for having a naming convention...

1) Improves readability
2) Reduces the chance that someone makes an error because they don't fully understand the problem.
3) Reduces development time, costs, and complexity by making it very easy to understand what is going on.
4) Helps in refreshing your memory in the future of what you did and how you did it.

Other Naming conventions include Positional conventions where you summarize large names and sentences into small (typically less than 8 or 9 character) variables. These summarization may include the name of the project, the process, and the sequence like "MarAdd2" representing "Martyr's application, addition function number 2".

This entry is not to say one is better than the other but that programmers should do everything in their power to make their code readable and maintainable so that other programmers find it easy to update and modify. If you are unsure your company or class has a naming convention in place, you can ask which to follow or have them create one. As long as the convention is consistent, understood by everyone, and makes things more readable, then the convention is good.

In the long run everyone wins. The employer, you, and the programmers following your legendary DIC coding legacy!

:)

If you want more blog entries like this, check out the official blog over on The Coders Lexicon. There you will find more code, more guides and more resources for programmers of all skill levels!

2 Comments On This Entry

Page 1 of 1

PennyBoki 

14 September 2007 - 07:35 AM
Nice tips, Martyr. I rarely use naming conventions, whenever I need a new variable I just create one and see the name is just different than the others.
Bad practice I know. I only use naming conventions when I'm programming in VC++. I see now that they are pretty much important, so I'll try do better from now on, and just wanted to ask, do you know any international standards about naming conventions, say in Java? Does SUN have some naming conventions?
Links would be great, thx :)
0

Martyr2 

14 September 2007 - 09:11 AM
I don't know of any set standard for naming. Partly because, as mentioned in the article, companies can leverage their own naming convention to fit their company's goals. I really wish they would put a naming convention together and standardize it. I think it would make things a little more uniform across the industry. From my experience though I have not seen such a standard and I have worked for numerous tech companies as a programmer and application developer on a multitude of different platforms.

They do have a naming standard for the language itself, so that programmers can understand java terminology and syntax (like quickly identifying that a name is a method by the way it is capitalized), but it is for syntax only and not variable naming and programmer style. Like it doesn't tell you that the standard is to follow Hungarian notation or anything. You can find it at the link below...

Java conventions (for the language's syntax)

One great book that I highly recommend for this area of study is a book I read a year or two ago and cited in the blog entry... Code Complete 2 by Steve McConnell. The text itself dates to around 2000 or 2001 I believe but most of the book's theories are still very much alive and well. He covers naming conventions and various standards that programmers should follow for better readability and program clarity. It has a ton of examples cover Java, C++ and VB. Good book if you are interested in the theory behind languages.

Hope that proves helpful and thanks for the comment. I am glad to see my blog helps people out. Even if it is only in a little way. :)
0
Page 1 of 1

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Recent Entries

Recent Comments

Search My Blog

6 user(s) viewing

6 Guests
0 member(s)
0 anonymous member(s)