Whatâ€™s in the name?
Contributed by Khurshidali Shaikh on 25 Mar 2009
This may be true when naming humans and many other things but when developing software names used inside the code are very important. While developing software, what names you give to entities (classes, methods, variables, files, etc) inside your code have a big impact on how easy it is to deal with the program during development and later during maintenance, by yourself and other team members.
We mostly use a personâ€™s name to associate a label to a face.Â A personâ€™s name may have a meaning but it does not tell anything about the person. As opposed to this, names used inside software program are expected to be meaningful and serve as an indicator of what the entities represent or are expected to do.
- An object of instance Customer should be better named as a customer rather than object
- A method which is computing a total may be better named computeTotal or getTotal rather than some thing as strange as doTheStuff.
- An customerId argument passed to a method should be better named as customerId rather than just id so that when someone looks at the method signature the argument meaning is clear
Looking at some one elseâ€™s code, or even looking at oneâ€™s own code after a long time, is a pain. In addition to that if the proper names are not used it becomes irritating and difficult to work with.
Names used in a program should almost always fall into 2 categories
- Noun: These are used to represent some concept that is modeled in the system.
e.g Customer, Order, Bank Account, etc
- Verb: These are used generally to name functions, methods, procedures, actions.
e.g register(), calculateTotal(), copy(), delete()
There are some exceptions like Collection.size(), StringUtils.defaultString(), etc but its still better to stick to the convention of using verbs to describe methods, functions, procedures.
Names should be explanatory but at the same time overly long names should be avoided
Correct names should be used from the start and not left as a TODO exercise.Â Correcting names in a program later on is a difficult task as there may be dependenciesÂ involved and this also needs further testing to be done. Also once a software is running fine no one is interested in doing this unless its a paid effort or is mandated by someone.
Last but not least, names must not have spelling mistakes.
So next time you are coding think before naming anything and not use the first name that comes to your mind.
Visit us at Neevtech.com to know more about our offerings.
Tags: coding, linkedin, naming conventions, software