i always like to start with $stuff or $something. nice & descriptive. (:
do a lot of people use variable prefixes like $hr_something (hash ref), and $a_stuff (array ref)
to show the type? my officemate/code reviewer
has had me using these for a while- i don't know that it's helped
my programming so much, but it makes it easier for him to find
my bugs. it might be more of a Cish thing than a perlish thing.
I like the idea of prefixing variables. It sure can't hurt, especially since Perl doesn't do strong typing.
I used to do this with VBA code because a very solid manual on VBA recommended it
as a naming convention, until I showed the code to a consultant who was to build a
SQL 7 replacement for my Acccess application, who snickered and said that "real" programmers didn't do it that way.
But now I've seen his code, and I don't care what he says. Personally I think anything that helps get the bugs out is good.
Update: After reading tilly's comments, I'm reminded of the real reason I stopped using these prefixes in VBA, it was a pain in the kiester to go through and change all the instances of that variable name. In Perl I don't use anything like this because I tend to keep most of my blocks small enough that only a few pages of scrolling are needed to read the entire routine.
This is a Windows idea, known as Hungarian Notation. One of the holy wars of how to program. It is not very widely use outside of Windows programming, but it is used a lot there. Even at Microsoft it is not universally used, apparently the applications division likes it, and the OS division hates it.
In languages with static type-checking it makes little sense. Why? Well the compiler already did the work for you, and you have scattered your type decisions through your code. If later it is found that your byte needs to be an int, are you really going to rename everything? No. What will happen is that the type will get changed and people will then be living with a landmine because the variable names will now be documenting the wrong API.
In languages without static type-checking, it makes a bit more sense because (as you say) the language doesn't immediately do the type-checking for you. But not having type-checking is a design decision - you are being encouraged to let the language figure out how to handle your types. If you are designing things that care about types in a language that doesn't, then that is an impedance mismatch between you and the language. Either you need to figure out how to come up with type-agnostic designs, or else you need a language that suits your tastes better. But fighting the language design is always going to cause problems.
Personally I almost never use types in my naming schemes except as a way to produce a temporary variable which really has no better name. For instance $href will pop up when I need an anonymous hash for a short while. But anything longer will almost definitely get a real name. And if it deserves it, it gets its own class.
Incidentally I recommend Dominus's article on strong type-checking. It is quite interesting, if a bit off the current discussion.
I use that all the time. Always.
My naming convention goes like this.
- @aLine = array
- $paName = pointer/reference to array
%hCountry = hash
- $phLanguage = pointer/reference to hash
- $objLog = reference to object
The only really bad thing about this is the pointer/reference mixup, since there really are no actual pointers in Perl. This is a Bad Thing, but I'm reluctant to change on account of too much legacy code. I really should take some time and fix that though.
Now, the good thing with using this naming convention everywhere is primarily that all parameters to subs are implicitly documented when it comes to type. I find that very useful.
Hehe...the only person who understood my shirt was my AP Comp Sci teacher...and I have had the shirt for about two years :-). Kung foo baby, kung foo...