Naming Variables Effectively for Clean Code Practices

Naming Variables Effectively for Clean Code Practices

Naming variables effectively is a crucial aspect of writing clean code. Writing clean, understandable, and maintainable code is a skill that is crucial for every developer to master.


Use Intention-Revealing Names

The name of a variable should be sufficient enough to understand its purpose. Don't use comments to explain why you're using a variable. If the name isn't clear enough, spend some time giving it a better name instead of adding comments. Avoid using generic letters like x, y, a, or b as variable names unless there's a good reason (loop variables are an exception to this rule).

Bad Example

$n = 10; // no of students        

Good Example

$noOfStudents = 10;
$verifiedUsers = 30;        

These names are so much better. They tell you what is being measured and the unit of that measurement.


Manage Consistency

Maintain consistency in your naming conventions throughout the codebase. If you use a specific format or style, stick to it. Whether it's Camel case, Pascal case, Snake case, or another convention, maintaining a standardized approach fosters a cohesive and professional codebase.

Bad Example

$currentDate = "04-01-2024"; //camelCase
$PrevoisExamDate = "12-11-2023"; //PascalCase
$next_exam_date = "02-03-2024"; //snake_case        


Good Example

$currentDate = "04-01-2024"; 
$previousExamDate = "12-11-2023"; 
$nextExamDate = "02-03-2024"; 

OR

$current_date = "04-01-2024"; 
$previous_exam_date = "12-11-2023"; 
$next_exam_date = "02-03-2024"; 
        

To uphold consistency in your codebase's naming conventions, it's essential to adhere to a specific format or style consistently.


Avoid Noise Words

Noise words are those that don't add any extra information about the variable. They're redundant and should be taken out. Some popular noise words are Data, Value, Info, Manager, Variable, Table, String, Object, Helper, Handler, etc

If your class is named AccountInfo, you can just remove the Info and make it Account. Remember, the goal is to have variable names that are concise and convey a clear understanding of their purpose without unnecessary words.


Use Pronounceable Names

Opting for Pronounceable Names Enhances Code Readability and Facilitates Discussions. If you can't pronounce a name, you can't discuss it without sounding silly.

Bad Example

$ddmmyy = "04-01-2024";        

Good Example

$currentDate = "04-01-2024";        


Avoid Disinformation

Exercise caution with words that carry specific meanings. Avoid using terms like accountsList unless the variable's type is genuinely a List. The use of such words might cause misunderstandings. Even if the variable is indeed a list, opting for a simpler and clearer name like accounts is recommended.

Bad Example

$accountList = [];        

Good Example

$accounts = [];        


Use Searchable Names

Steer clear of magic numbers in your code. Instead, use searchable, named constants. Avoid employing single-letter names for constants, as they may be scattered across numerous locations, making them harder to search.

Bad Example

if (strlen("A Random String") > 10) {
   // Do something
}        


Good Example

if (strlen("A Random String") > < MAX_STR_LENGTH) {
    // Do something
}        




要查看或添加评论,请登录

Mominur Rahman的更多文章

社区洞察

其他会员也浏览了