Sunday, June 06, 2004

Hungarian Notation in Perl

Perl has some measure of Hungarian notation running at the language level itself. In Perl, all variables start with a symbol, which is a kind of Hungarian notation. @names is an array of names, while %persons is a hash of persons. Hence, looking at the variable itself, you can tell, whether the variable is an array or a hash. So far so good! But I find that confusion comes in when we consider scalars. A variable that starts with a $, can either be a string, a number or a reference.

Consider the following statement:

$$items_collection[0] = 10;

This is an error, if items_collection is a reference to a hash, but the user may only find out the error, when he sees Perl complaining at the run-time: “Not an ARRAY reference”.

If, Hungarian-notation was conformed to, the above statement would have been written as:

$$rh_items_collection[0] = 10;

One look at the statement is enough to realize that it is wrong. It should instead be:

$$rh_items_collection{0} = 10;

It’s always good to catch the bugs as soon as you can.

Though we regard strings and numbers as very different things, Perl uses them interchangeably, which can lead to confusion. If we use two to three letters in the variable name, to give some information about the data-type held by the variable, its data-types can be readily inferred.

$i_Total – holds an integer
$f_Avg – holds a float
$raf_Expenses – holds a reference to an array of floats

When scripts get big and complex, we can avoid unintentional type-errors by using Hungarian notation. By aiding comprehension, this would also make the code more maintainable.

No comments: