DEV Community

rconr007
rconr007

Posted on • Updated on

The Importance of naming in code...

Choosing appropriate names when coding is always a good idea. IMHO. Many think that using cryptic, (single, double, triple) letters for naming is appropriate. Unfortunately, I beg to differ. we are forgetting one thing: "We don't code for machines, we write for humans!"

We spend the majority of our time reading code which accounts for 60-70% of our coding time and the remainder writing it. If we write code to be understood at a later time. Then shouldn't we always choose meaningful names? Wouldn't you agree? I don't know about you but I write and forget practically the very next day if I don't need to touch the code for a while. This is my own way to keeping sane and not storing or perhaps, I can say it in a nicer way: not overloading my brain with extra information, not required for my everyday upkeep. ;-)

A simple example:

const a = [10, 5, 50, 6];
let t = 0;
for(i=0; i < a.length; i++){
   t += a[i];
}
console.log(t);
Enter fullscreen mode Exit fullscreen mode

As you read the example above you will be able to follow it very simply as you are only dealing with a couple of variables. But you won't be able to follow the intent (or reason) why this code block was created.

Let's take the same example and add meaningful names to our variables and put some additional thoughts into our coding habits.

const priceList = [10, 5, 50, 6];
let totalPurchasedPrice = 0;
for(index=0; index < priceList.length; index++){
   totalPurchasedPrice += priceList[index];
}
console.log({totalPurchasedPrice})
Enter fullscreen mode Exit fullscreen mode

I think you know where I am going with this. At a glance you don't have to think too much to know that someone is taking a list of prices, iterating over them and arriving at the Total Purchased Price.

You are probably going to say that the names are too long. Or that you don't need anyone else to read your code. I can argue that with minification the first issue will be taken care of. Specially with libraries and frameworks like ReactJs and Angular. But ultimately, I would like to leave you my intro statement: "We don't code for machines, we write for humans!" Save the sanity of your future self and those of your coding colleagues.

D-Agency

Top comments (2)

Collapse
 
jmdejager profile image
🐤🥇 Jasper de Jager

Recognizable!
It takes some time to find the good names but it is worth it. It makes it easier to complete your code but most of all someone will probably thank you for it when they open the code years later (and most of the times you'll thank yourself 😉)

Collapse
 
rconr007 profile image
rconr007

Thanks, that's exactly my sentiment.