Go Code Review: Function Names

This article outlines two mistakes in programming: ambiguous function names and an overwhelmed function body.

Jane Kozhevnikova
2 min readMay 14, 2020
Photo by Markus Spiske on Unsplash

› Code Sample

The components of this code sample are:

  • a URL,
  • a type Database, which defines the information about a database connection,
  • a type Orders, which defines the information about orders,
  • a method database.GetData(URL), which consists of an operation which receives orders, and a method database.Write(orders), which writes the orders in a database.

› Problems

A name getData tells nothing about what the routine does. First of all, the problem with the word get which is vague. You cannot guess what exactly happens in the routine. And what data do we get from a URL? The word data is also vague. However, a function should be named for the value it returns. For example, we can change database.getData(URL) to database.requestOrders(URL) to make it more clear of what exactly we do in the routine and its values.

Although, a name of the method database.requestOrders(URL) tells us that it requests orders, the method also writes the requested orders in the database. So, this routine does not have a single purpose. The method database.Write(orders) is abundant for the database.requestOrders(URL) method. Yet, you can rewrite database.requestOrders(URL) to requestOrdersAndWriteInDatabase(URL) which is long and silly.

› Corrections

The following changes simplifies reading and understanding the code:

database.GetData() 🠒 database.requestOrders(URL),
⁕ delete database.Write() from database.RequestOrders(URL),
database.RequestOrders(URL) 🠒 requestOrders(URL).

› Conclusion

Choose a short, concise, self-explanatory name for a routine. Also, it should perform only one operation. Use strong verbs followed by an object in function names. However, we do not need to use objects as part of a name in methods, because it belongs to an object. For example, database.Write(), not database.WriteInDatabase().

--

--

Jane Kozhevnikova
Jane Kozhevnikova

Written by Jane Kozhevnikova

Technical Writer in Software Industry

No responses yet