Naming conventions

Author

Niko Laurila, Miika Reijonen

Published

February 24, 2025

Modified

August 26, 2025

Naming Conventions

Definition

Naming conventions are agreed-upon rules and styles that guide how you name variables, functions/methods, classes, files, and other entities in a codebase. They typically cover:

  • Case style (camelCase, PascalCase, snake_case, etc.)
  • File and folder naming
  • Prefixes/suffixes (e.g., I for interfaces, Async for asynchronous methods)
  • Abbreviations (deciding whether to use them, and how)

“There are only two hard things in Computer Science: cache invalidation and naming things.”
— Phil Karlton


Why It Matters

  1. Readability
    Clear, consistent names help developers (including future you) immediately understand the purpose of a variable, method, or file.

  2. Maintainability
    When everyone follows the same naming rules, the code becomes more uniform, reducing cognitive load and confusion.

  3. Discoverability
    Intuitive names make it easy to search the codebase for relevant classes, methods, or files—key for larger projects.

  4. Reduced Errors
    Ambiguous or misleading names can cause logic mistakes or confusion about a function’s parameters. Good names make the intent explicit.

  5. Team Collaboration
    Consistent naming conventions foster smoother communication and code review processes among team members.


Examples & Best Practices

Below are examples using a C#-style approach, but the principles apply to many languages. The exact style may differ based on your language and organizational preferences.

1. Variables and Fields

  • Use lowerCamelCase for local variables and private fields (in languages that prefer that convention):

    int userCount = 0;
    string firstName = "Alice";
  • Avoid ambiguous or non-descriptive names like cnt or str. Instead:

    // Bad
    int cnt;
    
    // Good
    int itemCount;
  • Constants often use PascalCase or ALL_CAPS (depends on style guide). For C#, PascalCase is common:

    private const int DefaultPageSize = 20;

2. Methods and Functions

  • Use PascalCase for public methods:

    public void CalculateTotal() { ... }
  • Method names should convey an action or intention:

    // Bad
    public void Data() { ... }
    
    // Good
    public void LoadUserData() { ... }
  • If the language or framework encourages it, consider adding suffixes for asynchronous methods, like Async in C#:

    public async Task SaveChangesAsync() { ... }

3. Classes and Interfaces

  • Classes: Use PascalCase that conveys their responsibility or domain concept:

    public class OrderService { ... }
  • Interfaces: Many C# coding standards recommend prefixing with I:

    public interface IOrderRepository { ... }
  • Avoid “Manager” or “Helper” if it doesn’t clarify the class’s true role. Instead, be specific:

    // Less clear
    public class OrderManager { ... }
    
    // More explicit
    public class OrderProcessor { ... }

4. Namespaces and Packages

  • Namespaces (C#) or Packages (Java) often follow company/domain-specific structure plus project layers:

    namespace MyCompany.MyApp.Infrastructure.Persistence
    {
        // classes related to data persistence
    }

5. Files and Folders

  • Name files after the class or primary concept within. For example, OrderService.cs for a OrderService class.
  • Folders often mirror namespaces or domain concepts (e.g., Services/OrderService.cs).

Guidelines for Effective Naming

  1. Be Consistent
    • Choose a style guide (e.g., Microsoft’s for C#, Google’s for Java/C++/Python, PEP 8 for Python) and apply it throughout the project.
  2. Use Meaningful Names
    • Reflect domain logic or the code’s purpose: CalculateInvoiceTotal() is much clearer than CalcInv().
  3. Avoid Redundancy
    • Don’t repeat information already provided by context:

      // Redundant: class already indicates it's a controller
      public class UserControllerController { ... }
      
      // Better
      public class UserController { ... }
  4. Keep it Simple & Clear
    • Names should be as short as possible while still being descriptive (KISS principle).
  5. Refactor
    • If a name no longer fits the code’s purpose, rename it to stay accurate and avoid confusion.

Conclusion

Using clear, consistent naming conventions makes your codebase more readable, maintainable, and collaborative. Whether you follow a widely-used standard (like PEP 8 for Python or Microsoft’s guidelines for C#) or create your own team style guide, the key is consistency. By choosing meaningful, descriptive names and sticking to the agreed-upon conventions, you reduce ambiguity, streamline code reviews, and help everyone (including your future self) work more effectively.

Back to top