Function Names, do it right
Muhammad Asher Toqeer
Senior Java Developer | Backend | Spring | thebackendguy.com
In programming, naming identifiers (class, method, variable names) is one of the most crucial part of writing code. If done right it could make our code a lot more pleasant, not only for others but for our self too. There is no need to mention benefits of well written code, every body knows it, yet many (novice mostly) programmers often neglect this.
In this article we will be focusing on function or method names, However in later articles we would discuss how to name variables and classes:
Keep intellisense in mind
We live in modern world and IDEs now a days are a lot more intelligent. You might be thinking instead of telling how to write understandable code independent of IDE, I am encouraging you to depend upon IDE. Yes I am, and in real life IDEs are used for coding and develop real applications, not notepad or stuff. Intellisense is a common feature in about all modern IDEs. So, always keep in mind whatever name we are going to give our function, It would appear in intellisense and would be auto completed. So our name should be brief, as brief as possible. Name should only reflect "What", not "How" or "Why". There is no need of adding parameter names and types to be reflected in the name or the return type, Intellisense is going to show whole method signature. For example instead of using:
int integerSumOfTwoIntegers(int arg0, int arg1);
use:
int add(int arg0, int arg1);
Make It meaningful
Well, every one knows it so whats new? Yes every body knows it but often we neglect it and this is happening usually when we are experimenting something. We just write some random private functions, and just try to make it work. Once it gets working we switch towards other parts of code thinking we would change it later. And it remain unchanged, it usually reviles in code review or when we need to update, that ugly function is there, like a mine, ready to explode !
So avoid any foo() or f() or y() etc. There is no "will see it later". Do it now, do it right.
Keep It Simple
Always keep in mind if this is going complex, we are doing it wrong. For example if we just come up with a name like
void readFileContactsAndSendMessage(File file, String message);
If we look it closely, actually there are two tasks been performed in the same method, they can be split like
Contacts readFileContacts(File file); //method 1
void send(Message message); //method 2
//now call these methods accordingly
Simplicity is beauty.
Aware of Context
We should always be knowing that there is hierarchy behind your method call, for example we have a class named Calculator with functions calculateAddition(), calculateSubstraction(), calculateMultiplication(), calculateDivision(). Now a class named Calculation want to use Calculator. It would be something like
class Calculations{
// Other code
public T calculate(int arg0, int arg1){
Calculator calculator = new Calculator();
// perform addition
?calculator.calculateAddition(arg0, arg1);
// other code
}
?}
Apparently there is no problem, but if we know that addition is a function of calculator then there is no need of word "calculate" with our function names. We would be much better if we just use add(), subtract(), multiply(), divide() because object of calculator would always tell what it is, so a better solution would be
class Calculations{
// Other code
public T calculate(int arg0, int arg1){
Calculator calculator = new Calculator();
// perform addition
?calculator.add(arg0, arg1);
// other code
}
?}
Conclusion
There should be a thought behind every function we create. We should name it in a way that intellisense would work with us, not just work for us. Avoid creating random functions with strange names to just complete functionality, make proper signatures. Always keep things simple and always keep aware context so we can come up with better names.
Happy coding
Principal Software Engineer at Obenan | Ex-Nextbridge | Full Stack | MERN | MEAN | Micro-services | Docker
8 年Well said. (y)
Technical Expert | Enterprise Solution Architect | Technology Lead - Full Stack | .net
8 年The standards apply to almost all languages. Nice.
PSE @ NXB | PHP | JS | React | Laravel
8 年very good
8K+ | Senior Mobile App Developer (Java | Kotlin | Flutter) | Business Development Manager | Project Manager | Visiting Lecturer (Mobile Application Development)
8 年very helpful.. thanks for sharing