Clean Code Basics: Why Naming Your Variables Properly Matters?!
Let's explore one of the clean code basic principle, which is naming your variables properly, is it matters ? Is it not ? That's what we'll see today!
Welcome back, brave coder đšâđ». Have you ever opened up someone else's code and immediately felt lost? Or maybe you've written code that you yourself didnât understand a week later, hahaha? This is where clean code principles come in, starting with one of the simplest yet most powerful practices: naming things properly. In this article, weâll explore why naming matters, see examples of poor versus good naming, and learn some best practices for naming variables, functions, and classes.
âThe Pain of Poor Naming
Imagine debugging code where variables are named a
, x
, or temp
. Itâs hard to understand what each value represents, especially when the code grows. Misleading names cause confusion and increase the risk of bugs đ. Naming things clearly is one of the best ways to communicate the purpose of each part of your code.
â
Clear Naming Conventions
Good naming conventions clarify the intent of the code. Here are some simple and practical tips, you can use to make your code looks cleaner:
Use descriptive names (e.g.,
discountRate
instead ofdr
)Avoid abbreviations unless widely understood
Follow consistent patterns like:
camelCase for variables â
var discouteRate
,PascalCase for classes â
class AppleJuice
,you can also use snack_case, if you are a python lover lol â
discoute_rate
đ§©ïžCode Example - Calculating a Discount
Letâs look at an example of refactoring a discount function to make it readable:
// Before: Hard to understand
function calc(a, b, c) {
return (a * b) - c;
}
// After: Clearly named variables
function calculateDiscount(price, discountRate, minAmount) {
return (price * discountRate) - minAmount;
}
// Applying the function
const price = 100;
const discountRate = 0.2;
const minAmount = 5;
console.log(calculateDiscount(price, discountRate, minAmount));
đConclusion
Todayâs article was very short and straight forward. To wrap it up, proper naming in code isnât just a ânice-to-haveâ, itâs essential for readability, maintainability, and team collaboration!
Next time youâre about to name a variable x
or temp
, take a moment to think about a name that truly describes its purpose. Future you (and your teammates) will thank you đ!
Thatâs it for today, see you soon!
PS: Also let me know what do you think of this kind of short content, do you like it? Or do you prefer the advanced long articles?
đ Don't forget to subscribe to my blog and YouTube channel to stay updated with all the latest content!