Naming Things — A Key Part of Programming
pic credit: medium.com

Naming Things — A Key Part of Programming

Naming Things — A Key Part of Programming


Why Naming is Hard: Naming is an art that requires clarity and good descriptive skills. It doesn’t always come naturally, and many of us struggle with it. A well-chosen name makes code easier to read, maintain, and collaborate on.

One of the hardest and most underrated skills in software development is naming things. It’s easy to write code that works — but writing code that’s readable, maintainable, and self-explanatory takes real craftsmanship.


Best Practices for Naming:

  • Avoid Brevity: Short, vague names make code harder to understand.

// Bad
const d = fetchData();

// Good
const userData = fetchUserData();        

  • Don’t Use Vague Words: Avoid using generic names like util, common, misc, data, payload, tool, service, or engine. They lack intent and context.

// Bad
const processData = () => { /* ... */ };

// Good
const normalizeUserProfiles = () => { /* ... */ };        

  • Avoid Stuttering: Repeating the same word in different forms is redundant.

// Bad
const userUserData = getUserData();

// Good
const userData = getUserData();        

  • Add Meaningful Context: A name should describe what the variable or function represents or does.

// Bad
const value = 100;

// Good
const maxRetryLimit = 100;        

  • Focus on the "Why," Not the "How": Names should describe the purpose, not the implementation.

// Bad
const sortAscending = (arr) => arr.sort((a, b) => a - b);

// Good
const sortByDate = (dates) => dates.sort((a, b) => a - b);        

Software Naming Conventions: Adopt the specific guidelines of your project or organization for consistency. Common naming styles include:

  • camelCase: userProfileData
  • PascalCase: UserProfileData
  • snake_case: user_profile_data
  • kebab-case: user-profile-data
  • CONSTANT_CASE: MAX_RETRY_LIMIT


Bonus: Transform strings between different naming conventions easily with the change-case npm package.


Bad Code Example:

function handleData(data) {
    const result = data.map(item => process(item));
    return result;
}        

Improved Code:

function formatUserProfiles(profiles) {
    const formattedProfiles = profiles.map(profile => formatProfile(profile));
    return formattedProfiles;
}        

Good naming leads to cleaner code, better collaboration, and fewer bugs. Let’s make code easier for everyone! ???


What’s the best (or worst) variable name you’ve ever seen? Drop it in the comments! ????


#CleanCode #SoftwareEngineering #CodingBestPractices #JavaScript #ProgrammingTips #DevLife #WebDevelopment #TechLeadership

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

Neeraj Swarnkar的更多文章

社区洞察