Tuesday, August 29, 2006

A guide to good naming

A question that I often get is "what should I name it?"

The best advice I can give is to take off your 'developer' hat and put on your 'business' hat. The most meaningful names reflect the purpose of the artifact. I have found that most often the purpose is best defined in business terms.

For example, we have a component that notifies a user of a new task. We could call this componet "SendEmail" or "SMTPOut" but it is much more meaningful to call it something like "SendTaskNotification".

It's almost never appropriate to name things based on the technology used. My favourite example of this is to name your XmlDocument variable "xmldoc". Could this name be any less descriptive? Surely, if we are going through all the trouble to instantiate/use this variable it must have some purpose. When naming we need to highlight this purpose and ideally put it in terms of the task at hand without focusing on the technical. It would be much more descriptive to call this variable "PurchaseOrder" or something like that (if that it what it was being used for). It is obvious from the code that it's an XmlDocument so we don't need to name it that.

A big win for good naming is the massive reduction in the need to write comments to describe what the variable is for. Good name swill take you a long way to self-commenting code.

1 comment:

Anonymous said...

This is very true, in the past my naming conventions have been garbage to say the least. Alittle attention to this and my code has become much easier to read for myself (and I assume others). Just takes some effort to switch but soon its habit.