Good function naming

Naming is one of the most important and hardest things.

Why the method names are so important? Because proper function names lower cognitive load.

  • function name + parameters names should express what the method does

If you see

void assign (Person p1, Person p2)

do you know what the function does? Not. You have to look inside!:

void assign (Person p1, Person p2) {
   p1.hasParent(c2);
}

Analyzing every function’s content to understand what really a function does is a waste of time and effort.

But if you see that function declaration:

void assignParent (Person child, Person parent)

you know what the code should be.

void assignParent (Person child, Person parent) { 
  child.hasParent(parent);
}

The function name + parameter names should tell you what the method does. You don’t need or want to look inside the method and read/analyze line by line what is going on.

  • name should be descriptive

The method name should be descriptive, as long as necessary to understand what the function does. I prefer long function names that explain what is happening.

Example – which function name is better?:

void upperCaseName (Customer c) {
      //somewhere in the code
      c.setName(c.getName().toUpperCase());     
}

or:

void setCustomerNameAsUppercase (Customer customer) {
      //somewhere in the code
      customer.setName(customer.getName().toUpperCase());     
}
  • express what & why

If you see this:

void repairTheData (List<Link> links) {
   if(links.get(0).distance != 0)
       links.add(new Link(0, 0));
}

you have no idea what is going on. Why new zero-value link is added? But if you see this:

void addingZeroLinkAtBeginningOfListToBeStartingPoint (List<Link> links) {
   if(links.get(0).distance != 0)
       links.add(new Link(0, 0));
}
  • use the business language as much as possible

Business requirements are translated into code. However, the problem is that when looking at the code, we have no idea what the requirements are. Therefore, there is a need to use business terms in code/function names:

void addingZeroLinkAtBeginningOfRoadToSimulateTheStartingWaypoint (List<Link> links) {
   if(links.get(0).distance != 0)
       links.add(new Link(0, 0));
}
  • do not use general names such as parse, validate, etc.

You should not use a generic name for a function eg. parse, or validate because each function does something specific and this should be reflected in the function name.

eg. addingEmptyLinkAtBeginningOfListToSimulateTheStartingPoint

ERS example:

  • incorrect function name makes a chaos

Yes, you look at the name of the method and then you look at the content – it is contradictory. You start to wonder whether the method name is wrong or the code is wrong:

validateCustomer (Customer c) {
   c.name = setFirstLetterUppercase (c.name);
}

It creates a real chaos in your brain.

Summary

Proper naming requires training. You should practice and practice and really care about function names. The code will be much cleaner and easier to read.

Leave a Reply

Your email address will not be published. Required fields are marked *