Clarity or Optimization which should come first for naming a variable or function?

Monayem Islam
2 min readDec 22, 2020

You are a novice programmer and think that variables and functions name couldn’t meet the standard. Every beginner makes some silly mistakes in variable and function names which I also did. In this article, I am going to share with you some tips that helped me to level up my coding skill and enhance my confidence. Before start I want to recall the boy scout rule which is too much relatable with our best coding practices.

Leave the campground cleaner than you found it.

Ignore Nonsensical variable and function names

If you think you are naming the variables and functions only for machines then it doesn’t matter what you are giving its name. But If you think your code should be human readable then you have to be conscious about variable and function names. For example,

Code 1:

double h = 7.9;
double w = 5.8;
public double a(double h, double w)
{
return h * w;
}

Code 2:

double height = 7.9;
double width = 5.8;
public double areaRectangle(double length, double width)
{
return length * width;
}

Code 1 will work perfectly but it’s quite difficult to understand what’s happening here. Without putting comment it’s quite impossible to understand the meaning of h and w.

double h = 7.9;
//h stands for Rectangle height
double w = 5.8;
//w stands for Rectangle width

But a variable requiring comment means the variable name doesn’t reveal its purpose.

On the other hand if you look at Code 2, easily anyone can say that it’s calculating the area of a rectangle where the height and width of the rectangle is assigned. This is the benefit of good naming convention. There are some delimiting conventions for multi word variables such as Snakecase, camelCase. Personally I prefer camelCase.

Now you are thinking that descriptive variable or function names should increase the size of code but it’s ignorable because a descriptive name helps other programmers to understand your code. Even you couldn’t understand your code after a year without descriptive names.

Tim Ottinger says that

One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.

--

--

Monayem Islam

Passionate about programming and working towards B.Sc. in Information and Communication Technology at Islamic University, Bangladesh.