December 31

Naming Variables – my camelCase story

One of the many arts or etiquette of coding can be attributed to your choice of naming convention for your variables.

I say art because it’s partially aesthetics. The way you name your variable can make your line look very beautiful or very ugly. I say etiquette because it’s really just professional and respectful best practice to name your variable in a way where it’s easier for another person to follow the flow of your code.

My variables typically look like the following:

i_item_name_str

Examining this, there are typically at least 3 parts to my variables. the [i] here represents that this is an input variable. In this case, it is a parameter being processed from within a function (thus, input). So right away, the beginning of the variable can tell where this variable came from (i.e. is it an input? an output? or just a non-derived variable?) The [item_name] represents what this variable is, which in this case is the name of the item being passed. The [str] helps identify what type of variable this is, so right away I know this is a string (or varchar) field.

Now, we all know there are certain standardized means of naming variables, especially for certain languages. One of the most common methods is camel casing. That is, instead of i_item_name_str, it may look something like itemName. While this is absolutely fine and visually sexy, I’ve come across some unanticipated obstacles in my career that deemed the camel casing methodology a risky one, and one that I am hesitant to use.

As a solutions architect, I work with many different layers or technologies. A project may consist of working with a combination of MySQL and PHP. While you may be able to get away with camel casing in PHP, there was an instance where I had used it throughout my procedure names and variables in MySQL. I then, at a later time, had to migrate to another server, after which my applications all broke. When I did some diagnostics on it, I came to realize it was because the migration lowercased everything and ultimately made everything unusable since the camel casing was no longer in tact! Now that I look back, I’m almost positive there could have been a setting in the server configuration that I could have used to enable case sensitivity. But now that I’m traumatized, I’d rather never take that risk ever again and never put myself in a position to do any unnecessary work. At the end of the day, i_item_name_str is almost just as legible as itemName, but less prone to fail across platforms and migrations. That is, until we come across one that prohibits the use of underscores. Oh boy.

So tell me, what is your single most used variable naming convention and why?