Naming a class/variable/method
December 30, 2007 Leave a comment
I needn’t emphasize the importance of choosing best name for class/variable/method. But how many managers/developers care in choosing best names. Most of them concentrate on completing assigned task and deliver working output, because they feel that naming is not going to add any value to their software.
A few years ago I read “Code Complete” book authored by Steve McConnel. He has mentioned that “Goodness or Badness of a class/variable/method are determined by its name”.
In an another instance, I had an argument with my architect to create a new class. My architect felt a new class is not required, whereas I argued to create a new class. Finally he asked me the name of the new class, which makes me to think a lot to create a new class.
Here are a few tips given by Steve McConnel in Code Complete book to choose best names.
1. Name fully and accurately.
2. The name should refer the domain problem not the programming problem.
3. The name should be long enough so that others needn’t puzzle it out.
4. Don’t use “is” infront of the boolean variable. (It is good for method)
5. Use positive boolean variable names like correct, found, done. Don’t use notFound, inCorrect, etc.
6. Avoid abbreviations. If you like to use follow the same throughout the application.
7. Short names should be well documented.
To me naming selection is directly indicating developers’ understanding on the system and domain. If one cannot choose right name, you can clearly observe that your developer doesn’t have enough understanding on the system or the requirement.