Ad
Latest Review
Screenshot of the opening screen of Super Mario Bros. Wonder
November 5, 2023
Latest App Update
App icon for wwriteLite
August 1, 2023

Dev Topic: Hungarian Notation

I have spent the last few weeks thinking about programming, specifically variables, and how to name them. There are many different approaches to naming variables. One of the ones that is bandied about, and question quite often, is the Hungarian Notation. Hungarian Notation is the idea of putting the type in the name of the variable. An example of this might be "str" for string, or "int" for integer, or "bln" for boolean.

Conversely, there are those who never put the type within a variable name. Examples of these variables names might be "firstName", "zipCode", or "countryCode". These three examples are somewhat basic and most people can infer what type of information is going to be stored in these variables. However, there are times when it may become less obvious. For example, the variable "userID". What type of data is stored in this variable? It could be an integer, to tie a user to a database row, or it could represent a username, like "bob123".

The debate between the naming conventions used leads to this question "When is it appropriate to use each type of naming convention?" Well that depends on many factors. For me, the biggest is which programming language am I using. When I am using Objective-C, the type of variable must be declared.

Objective-C

NSString *firstName; NSString *lastName; NSInteger *userID; BOOL isReady;

With the three variable names above, it would be quite obvious what type the variables represent. However, when I use PHP or Javascript, it may become less obvious. In PHP, you can specify what type, but by default all variables are just that, variable. No keyword or type information is assigned. They can contain any information and be of any type. See the code below.

PHP

$firstName = ""; $lastName = ""; $userID = ""; $isReady = "";

As a person looking at the code, you cannot explicitly know what type of information is expected. You can infer but you do not know with certainty. If you assign the wrong type of data to the wrong variable, it can completely break an application. The same is for Javascript, where the only way to declare a variable is using the keyword of "var".

Javascript

var firstName; var lastName; var userID; var isReady;

With PHP, and Javascript, Hungarian Notation becomes quite handy. When using Hungarian Notation, your code may look like this:

$strFirstName = ""; $strLastName = ""; $intUserID = ""; $blnIsReady = "";

Javascript

var strFirstName; var strLastName; var intUserID; var isReady;

As you can see, you can more readily discern what type of information is being expected. Now that one situation when to use each type of notation is answered, the next question becomes, "Why have different naming conventions?"

The answer to this one is quite simple, readability. With the proliferation of open source projects, there are many individuals who try to understand how an application or program actually functions. To make it easier on them, it is best to use some sort of convention, when it is appropriate. Besides the benefit of other individuals, as a developer it also makes it easier for you to read your own code later on.

As an example, my largest web, which has about 175 database tables, has a total of 107525 lines of code, between PHP, HTML, CSS, and Javascript files and all of this spans almost 1500 files. A good chunk of this code was written almost six years ago now. If I were to look back at some of the original code and did not use Hungarian Notation, then I would most likely have to look back at certain things and re-construct what I had written. While this almost certain to happen regardless of how long ago a piece of code was written. Nonetheless, it does take away from current productivity and forward momentum.

I am not advocating that everybody use Hungarian Notation, but merely to think and plan ahead for when you have to come back and look at code. Additionally, if you are looking to open source your code, be sure that it is readable to your audience. This means, somebody besides yourself. Also, do not forget comments, even if they are basic so that everybody can get some idea of what is the intention of a certain bit of code is meant to accomplish.

Tags: