|Perl: the Markov chain saw|
Re: Hungarian notation, kind ofby davies (Parson)
|on Jan 21, 2013 at 17:50 UTC||Need Help??|
One of the first things I found when first considering Hungarian was http://www.joelonsoftware.com/articles/wrong.html. I don't agree with everything Spolsky writes (especially on Excel - he should be sentenced to use it), but I like this piece and have used Hungarian widely since reading it. There are several points to note, though.
The first is the difference between "Apps Hungarian" and "Systems Hungarian". Actually, I find even SH useful in some circumstances. The commonest is when dealing with columns in Excel. These can be specified numerically or alphabetically, so I will always specify "ncol" or "scol". I also find SH useful in Perl as I'm not used to the relatively weak typing and find SH helps me remember my original intentions.
Second, consistency is very useful. Another reason I prefer Hungarian is that Excel uses it internally for constants (I wish it were consistent and used it for all data types, but you can't have everything. Why does Selection get an initial capital? Ask Spolsky). It also uses a capital letter to start all functions, so I use lower case Hungarian prefixes and capitals to start functions.
The effect of this is greater in VBA (Excel / VBA is where I spend most of my time) than in sensible languages. In a crash, there is no way of getting the return stack unless you organise it explicitly. I therefore wrap everything in error trapping and raise errors to enable me to see the return stack. So when I get a line like If Initialise Then Err.Raise knCall, , ksCall, it is obvious from the lack of a prefix that Initialise is a function returning a Boolean rather than a Boolean variable.
Another reason for including upper case characters in VBA (I don't know about other MS languages, but I believe that some have the same feature) is that the IDE automatically converts the case to the case of the original declaration. This means that anything that appears as 100% lower case is automatically a typo and is easily spotted.
I realise that I'm writing a lot about VBA here, but one of the points you raise is consistency. You can't be consistent with everything. Most of the Perl I write is to control Excel, and in that situation it makes sense to follow Excel's rules, even in Perl. If you read the Spolsky article I linked to, you will see him using upper case in various situations, so if you want to be consistent with him you will use CamelCase rather than underscores. But standards differ, so it helps to be flexible in your consistency so that you do not automatically use something that is inconsistent with someone else's standard.
You describe one thing I've never seen before, though, namely Hungarian suffixes. I (and I'm an accountant, not a programmer) have always seen them as prefixes. If you have any references for this use, I'd like to read the pros and cons. If not, be aware that you're likely to have to change to prefixes if coding in any sort of team.