Page cover

✏️Naming Conventions

The Importance of Naming in Coding and Software Design

Naming conventions are sets of rules to give names to different types of variables, functions, classes, and other entities in coding, aiming for clarity and maintainability.

Why it matters

  • Readability: Makes the code more understandable, reducing the learning curve for new team members.

  • Maintainability: Easier to debug and update.

  • Consistency: Across the codebase, ensuring uniformity.

  • Communication: Acts as self-documentation, making intent clear.


Best Practices

Note: While specifics might change based on the language, the core principles usually remain the same.

Variables

  • Use descriptive names.

    Bad: ua

    Good: userAge

  • Prefer camelCase for local variables.

  • Constants should be in UPPERCASE.

Functions and Methods

  • The name should describe action or intent.

    Bad: calc()

    Good: calculateTotalAmount()

  • Prefer verbs for function names as they perform actions.

Classes

  • Use PascalCase.

    Bad: user_info

    Good: UserAccount

  • The name should be a noun or noun phrase.

File and Folder Structures

  • Use kebab-case for file names.

    Bad: UserProfileCode.js

    Good: user-profile.js

  • Group related files into directories. For instance, all API routes could be in a routes directory.

  • Avoid overly generic names; utils or helpers folders should be clearly segmented.

Database Naming

  • Tables should be plural nouns.

    Bad: user_table

    Good: users

  • Avoid using reserved keywords.

  • Foreign keys should reference the parent table.

    Bad: referenceId

    Good: user_id


Language Agnostic Tips: Universal Conventions and Why They Matter

  • Self-explanatory Code: Your naming should be intuitive enough that a developer of any language can grasp the intent.

  • Consistency is Key: No matter the language, maintaining consistency in naming throughout the project aids in readability.

  • Avoid Abbreviations: Unless it's a universally recognized abbreviation, it's better to avoid them to maintain clarity.

  • Length isn't an Issue: Having a slightly longer, descriptive name is better than a short, ambiguous one.


Exercises & Examples

  • Exercise 1: Given a list of poorly named variables, rename them following best practices.

    Example: Original: a, b1, r2d2 Renamed: accumulator, baseValue, robotName

  • Exercise 2: Analyse a piece of code and identify areas where naming conventions are not followed. Suggest improvements.

  • Exercise 3: Look at a database schema and recommend changes for table names, column names, and relationships to be more descriptive and intuitive.


Incorporating these practices ensures that the code remains clear to both the author and any developers who might work on it later. Remember, a well-named function or variable can often eliminate the need for a comment, making your code cleaner and more self-explanatory.

Last updated