DEV Community

Cover image for Clean Code 101: Variable Naming Conventions - Part 1

Clean Code 101: Variable Naming Conventions - Part 1

MohitSinghChauhan on July 23, 2023

What's the best way to measure how clear and understandable code is? Simple - count the "WTFs/minute". When reading confusing, messy code, you ca...
Collapse
 
giovannimazzuoccolo profile image
Giovanni Mazzuoccolo • Edited
const address = 'One Infinite Loop, Cupertino 95014'; 
const cityZipCodeRegex = /^,([^, \]+) ([^, \]+)(?: ([^,\s]{5}))?$/; 

const [, city, zipCode] = address.match(cityZipCodeRegex) || [];

This code doesn't work. The regexp is not correct - Unmatched ')' - and the array destructuring got the wrong words.

This is the correct one

const address = "One Infinite Loop, Cupertino 95014";
const cityZipCodeRegex = /^([^,]+),\s*([^,]+)\s+(\d{5})$/;

const [, , city, zipCode] = address.match(cityZipCodeRegex) || [];

Fell free to check it on this codesandbox

Collapse
 
hayley_grace profile image
Hayley Grace

Well written article :)

Collapse
 
mohitsinghchauhan profile image
MohitSinghChauhan • Edited

Thank you for your kind words.

Collapse
 
chiragagg5k profile image
Chirag Aggarwal

The only thing harder for programmers to do than getting a life is thinking of variable names

Collapse
 
mohitsinghchauhan profile image
MohitSinghChauhan

Lol! So true.

Collapse
 
venkatlohithdasari profile image
Venkat Lohith Dasari • Edited

Amazing Article buddy!! Its really useful for someone like me who is working on open source projects, it helps Construct project and codebase in such a way that other can understand it! Thanks

Collapse
 
onlinemsr profile image
Raja MSR

One point that I liked from the post is the use of meaningful and pronounceable names for variables. I think this makes the code more readable and understandable.

Collapse
 
abdrcao profile image
Antonio Belo

Great article! Thanks

Collapse
 
mohitsinghchauhan profile image
MohitSinghChauhan

Thanks, Stay Tuned, more to come.

Collapse
 
tensorprogramming profile image
Tensor-Programming

Excellent article, though I'd like to insert a caveat regarding the 'clean code' idea.

Despite reading Uncle Bob's acclaimed book and your insightful piece my experience tends to be a bit different. The term 'clean' in the context of code can be quite ambiguous and interpreted differently. Standard definitions for clean code can often be somewhat self-contradictory. For example, some may view the use of singletons as enhancing comprehension, yet they may not necessarily aid maintainability or performance over time. Engineering is a matter of striking a balance, and as a team, we need to grasp and discuss these trade-offs, keeping the project requirements in mind.

Let's look at the spread operator and iterators in JavaScript as a case in point. Generally, it's viewed as 'cleaner' to use these tools. However, if you're working with large data sets, you may encounter performance issues due to memory allocation and how the garbage collector operates.

In this context, the concept of 'clean' code clashes with the reality of the language. The ideal clean code would strike a balance between readability and performance. However, in this situation, even if readability is improved (subject to individual interpretation), performance significantly deteriorates.

This underscores why principles like clean code should be viewed as flexible guidelines rather than rigid rules. No matter how straightforward the idiom might appear, there's always an outlier that challenges it.

Collapse
 
laansday profile image
Tony Do

Youโ€™re inspired from the actual book Clean Code right? I can tell from your very first sentence

Collapse
 
arocha profile image
Nefario313

Uncle Bob have more interesting ideas about variables names. Short name vs long names and when to use them.