Skip to main content

I always struggle with naming. And I am not the only one. I know this from my experiences with code reviews and so on.

Here are some nice examples (mainly in golang and most of these are taken from https://google.github.io/styleguide/go/decisions#variable-names):

"Repetitive name" to "Better name"

Repetitive name -> better name

widget.NewWidget -> widget.New
widget.NewWidgetWithName -> widget.NewWithName
db.LoadFromDatabase -> db.Load
goatteleportutil.CountGoatsTeleported -> gtutil.CountGoatsTeleported or goatteleport.Count
myteampb.MyTeamMethodRequest -> mtpb.MyTeamMethodRequest or myteampb.MethodRequest
var numUsers int 	-> var users int //the variable type int already tells that users is a number, so no need to add the prefix`num`. Similar are true for the below examples
var nameString string 	-> var name string
var primaryProject *Project 	-> var primary *Project

More examples with package/lib

// In package "ads/targeting/revenue/reporting"
//bad
type AdsTargetingRevenueReport struct{}
//good
type Report struct{}

// In package "sqldb"
// bad:
type DBConnection struct{}
//good
type Connection struct{}

Short variable

I usually write long variable names. But, for the following cases a short variable name is a better choice:

  • for common types
    • r = io.reader
    • w = io.writer
  • inside a loop for i = 0; ...
  • When the scope of the variable is short, then use short variable name. If the scope of the variable is long, then use longer variable name (Uncle Bob)

Reference

Almost all the examples are taken from https://google.github.io/styleguide/go/decisions#variable-names

meaningfulcareerandlife

Junaed is a passionate Software Engineer who worked on Cloud Technology, Enterprise Software, Mobile Apps, Highly Scalable Software Systems, and more. He holds a Master’s Degree in Informatics from TU Munich, one of the best universities for computer science. During and after his academic journey, he started working as a software engineer in several countries and companies, including Airbus, IBM, and others. As an ever-evolving individual who likes to expand his knowledge and field of action, Junaed never ceases to pursue new learning opportunities. Driven by this mindset, he has acquired multiple certifications in Kubernetes, IBM cloud, AWS, and Spring framework - a popular Java framework. During his career, he has learned valuable lessons that he wishes to share with others, whether they have just started their career or need to advance it. That is why he is always on the hunt for ways to spread his knowledge on career growth and mistakes, software engineering best practices, and more subjects.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: