Naming Variables and Functions

We all come across situation where we have to look at a code that it’s written by someone else and try to figure out what is that variable does or what is this function suppose to do.

This is where good naming is important. it saves us time and significantly reduce error. I know naming variables and functions can be tricky. In this post, I share two characteristics that can be a foundation or starting point for good naming

Intent

before you declare a variable or write up a function think about what is the purpose of the variable or function. The name should self-evident so in case of function it should be in verb form such as GetIncident() Or UpdateIncident() and in variable should be noun like IncidentNumber

Contract

Think about how is your variable is going to be used and how it is going to interact with other variables and functions. The name should indicate data type, use case, general behaviour. here are a few examples

  • IsIncident – I except the variable to be Boolean variable and going to be used to check whether a ticket is incident or otherwise
  • SubjectHireDate– I except the variable to be Date variable and going to be used to store subject person hire date
  • IncidentNum I except the variable to be string and going to be used to store incident number
  • AllProblems – I except the variable to be Array variable and going to be used to store all Problem records

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s