Posts Tagged naming conventions
What’s in the name?
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.
e.g.
- 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
e.g. registerUserSendMailAndSubscribeToNewsletter()
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.