Function Names, do it right

Function Names, do it right

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

Rana M. Nabeel

Principal Software Engineer at Obenan | Ex-Nextbridge | Full Stack | MERN | MEAN | Micro-services | Docker

8 年

Well said. (y)

回复
Hassan Gulzar

Technical Expert | Enterprise Solution Architect | Technology Lead - Full Stack | .net

8 年

The standards apply to almost all languages. Nice.

回复
Muhammad Sulman

PSE @ NXB | PHP | JS | React | Laravel

8 年

very good

回复
Muhammad Zeeshan Shahid

8K+ | Senior Mobile App Developer (Java | Kotlin | Flutter) | Business Development Manager | Project Manager | Visiting Lecturer (Mobile Application Development)

8 年

very helpful.. thanks for sharing

回复

要查看或添加评论,请登录

Muhammad Asher Toqeer的更多文章

  • Modern Java as Data-Oriented Language

    Modern Java as Data-Oriented Language

    Java, at its core, is an Object Oriented Language. But, Object Oriented Programming alone can't solve all of the…

  • Spring Framework Brief Overview

    Spring Framework Brief Overview

    History Spring is a framework for Java Enterprise Application Development. It started in 2003, at that time Java offers…

  • Java Modules Introduction

    Java Modules Introduction

    Classes are the basic unit of a program in Java. Packages are used to manage classes and modulesare used to manage…

  • Handling Mistakes as a Developer

    Handling Mistakes as a Developer

    This article is based on "A Pragmatic Philosophy" from the book: "The Pragmatic Programmer". Mistakes are Inevitable.

  • Java 8 Functional Programming Simplified

    Java 8 Functional Programming Simplified

    This article will explain Java 8 functional programming related concepts, i.e Lambda expressions, Functional…

  • 3 Years being a Software Engineer, Here are some things I have learned

    3 Years being a Software Engineer, Here are some things I have learned

    Life is a journey, so as your carrier and like every other journey, your carrier teaches you a lot of things. Here are…

    2 条评论
  • MVC and MVCS : Software Engineering

    MVC and MVCS : Software Engineering

    In previous article Architectural vs Design Patterns: Software Engineering we discussed in detail about what…

    2 条评论
  • Architectural vs Design Patterns: Software Engineering

    Architectural vs Design Patterns: Software Engineering

    Software design patterns are some proven ways to solve a reoccurring problem faced by programmers. These are general…

    2 条评论
  • Variable Names, Some tips

    Variable Names, Some tips

    Variables can be your best friend, or your worst enemy and that depends on You. If you are not careful and just…

    2 条评论
  • Meaningful Class Names

    Meaningful Class Names

    This article is about naming classes, mainly it is about "how to meaningfully name your classes" remember in…

    3 条评论

社区洞察

其他会员也浏览了