A Guide to Naming Things in Code

A Guide to Naming Things in Code


Hey fellow coders!

Ever found yourself staring at a mess of abbreviations, numerals, or worse – generic placeholder names like temp1 or xyz – in a chunk of code and wondering, “What was I thinking?!”. If you’ve ever caught yourself in a naming nightmare, you're certainly not alone.

Welcome to this guide on the art of naming things in code. Whether you are a beginner easing into the world of programming or a seasoned developer looking to revisit some fundamental best practices, stick around for a deep-dive into turning your messy code into easily readable, maintainable code.

The Importance of Good Naming Conventions

Before diving into the nitty-gritty of how to name, let’s talk about why it’s crucial. Did you know that a staggering 70% of software’s cost over its lifetime is maintenance (TCO), and poor naming conventions significantly contribute to that cumbersome expenditure? Names are the most visible part of your work to your teammates and your future self. Hence, getting them right can be a game-changer.

According to a study conducted by Stroustrup (yes, the C++ guy), meaningful and verbose names can reduce cognitive load on developers, promoting faster comprehension.

 

Bad Practices

As crucial as knowing what to do is understanding what to avoid. Let’s discuss some of the most common pitfalls in naming conventions.

  1. Single Character Names : Single character names (like x, y, z) should only be used in very short contexts such as small loop counters, and even then, meaningful names are better.

let x; // What does x represent?

let age; // Clearly indicating purpose        

2. Abbreviations : Abbreviations and shortened forms can create confusion about what a variable, function, or class represents. Some widely accepted abbreviations are fine (like min, max), but many others can be unclear.

let qty; // Quantities...

let quantity; // Being concise but clear        

 

3. Don’t Put Types in Names : Including the type of the variable in its name can add redundancy and make refactoring stressful. Your code should be flexible enough to accommodate type changes without needing to rename variables.

let numberAge; // Redundant and rigid

let age; // Context preserves clarity        

 

Good Practices:

Lets discuss some good practices that makes our code more readable.

 

  1. Use Units in the Name If your variable represents something measurable, include units in the name to provide context:

let duration = 120; // 120 what? seconds? minutes?

let durationInSeconds = 120; // Clear and informative.        

2. Use Descriptive Names Clear, descriptive names that define the topic or functionality will make your code more readable:

let tmp; // What does this stand for?

let userSessionData; // Far more informative.        

3. Consistency Uniformity in naming conventions across the codebase is key. Define a standard and stick with it uniformly across your project:

// Before - mixed case styles

let first_name;

let lastName;

 

// After - consistent in camelCase

let firstName;

let lastName;        

 


Use Tools and Conventions

Leverage tools like linters (e.g., ESLint, Pylint) and follow language-specific naming conventions to standardize your practice. Additionally, take advantage of Integrated Development Environment (IDE) features like Visual Studio's AI rename suggestions to get intelligent recommendations for renaming variables and functions based on your code context.


Adopting good naming conventions is not just a best practice but a commitment to making your code understandable, maintainable, and bug-free. Integrating these guidelines seamlessly into your coding routines might just make your future self very grateful!

Happy Coding!

Thank you Suhaan Tonse for helping with research.

To view or add a comment, sign in

Others also viewed

Explore content categories