Beyond CustomModules: Naming Conventions for Clear, Maintainable Code

In the world of programming, naming is not just a formality; it’s the bridge where logic merges with language. Choosing the right names for variables, functions, classes, or modules in languages like C# isn’t just a matter of syntax—it’s the key to crafting code that communicates its purpose with clarity and elegance.

Programming is much more than creating instructions for a computer. Coding is where logic meets language, and as with any written language, there are principles that should be followed. Whether it’s a variable, a function, a class, or a module, the name you assign to it carries the weight of understanding for fellow developers and even for your future self. It has been said that programming is 20 percent coding and 80 percent figuring out what to name things. This seemingly simple act profoundly shapes the clarity, readability, and maintainability of our code. Proper naming can transform lines of code into eloquent expressions of logic and purpose. Each language has its own preferred naming paradigms chosen by the authors. For this post, I’m going to focus on C#.

a close up of a computer screen with a lot of code on it
Photo by Markus Spiske on Unsplash

Modules/Classes/Structs/Enums

1. Be Descriptive

The module name should clearly describe the functionality it provides. A developer should be able to understand what the module does without having to look into its code. And by all means, never call it a CustomModule.

2. Use PascalCase

Always use PascalCase (first letter of each word capitalized) for module names. Consistency is essential for readability.

3. Avoid Abbreviations

While brevity is important, avoid overly cryptic abbreviations that might confuse other developers. Use meaningful words that convey the module’s purpose.

4. Noun for Classes and Objects

Class names and object instances should generally be named using nouns or noun phrases that represent the entities they represent—for example, Customer’, Order’.

5. Be Concise

Try to keep the module name concise while being descriptive. Long module names can become unwieldy and more challenging to read. Aim for a balance between clarity and brevity.

6. Be Consistent

Establish a naming convention for your project and stick to it. Consistency in naming modules, variables, and other elements across your codebase enhances readability and maintainability.

7. Consider Namespace

Choose module names that fit logically within the namespace hierarchy. This helps in organizing and structuring your codebase.

8. Avoid Naming Conflicts

Ensure that module names are unique within the scope of your project to avoid naming conflicts. This becomes especially important when working on larger projects or collaborating with other developers. Oh, and don’t call your module CustomModule.

9. Use Singular or Plural Form

Choose between singular and plural form based on whether the class represents a single object or a collection of objects. For example, use User’ for a class representing a single user and Users for a class representing multiple users.

Variables/Fields/Properties

1. PascalCase

Again, PascalCase is the preferred naming for properties. For example, ‘UserSettings’, ‘HasChildren’.

2. camelCase

Local variables are typically named using camelCase, where the first letter of the first word is lowercase, and subsequent words are capitalized. For example: ‘totalCount’, ‘customerName’.

3. ALL_CAPS

Constants are often named using uppercase letters with underscores separating words. For example, ‘MAX_VALUE’, ‘DEFAULT_PATH’. Note: this is a heated debate among developers who argue that PascalCase is the way.

4. Descriptive Names

Choose descriptive and meaningful names so that someone reading your code can understand the purpose of the variable without needing to refer to additional documentation. Avoid using generic names like ‘temp’, ‘data’, or single-letter names like ‘x’.

5. Avoid Abbreviations

While keeping names concise is important, abbreviations and acronyms can sometimes make code less understandable. Use abbreviations sparingly and only when they are well-known and widely recognized.

6. Use Meaningful Prefixes and Suffixes

In some cases, it’s helpful to use prefixes or suffixes to indicate the purpose or type of a variable or method. For example: ‘isComplete’ , ‘hasData’.

7. Avoid Hungarian Notation

Avoid using Hungarian notation (prefixing variable names with type indicators) as it’s no longer considered a best practice in modern C# development.

8. Consistency

Consistency in naming is crucial. Use the same naming conventions throughout your codebase to maintain readability and reduce confusion. You can use whatever you want, but make sure it remains consistent.

Methods/Functions

1. Verb-Noun Pairing

Methods typically perform actions, so their names should reflect what they do. Use a verb or a verb phrase to describe the action followed by a noun representing the object acted upon. ‘GetDocuments()’

2. PascalCase

Always use PascalCase (first letter of each word capitalized) for module names.

3. Be Descriptive

Choose method names that clearly convey the purpose of the method. A developer should understand the method’s functionality just by looking at its name. For Example, ‘FetchDataFromDatabase()’, ‘CreateUserFromContact()’

4. Avoid Acronyms and Abbreviations

While brevity is important, avoid using abbreviations or acronyms that are not widely known. Code should be understandable by anyone, not just the original developer.

Proper naming conventions cannot be overstated in the realm of programming. Remember, clear and meaningful names significantly enhance the readability and maintainability of your code. By adhering to these guidelines (remember they are just guidelines), you create a codebase that is not only functional but also understandable. A thoughtfully named variable, method, or module allows you to spend less time writing documentation and more time coding.

About the Author

Tim Stauffer

Completely self-taught and a Jack of all trades, Tim’s the man when it comes to making things happen with websites and software. Given enough time, he can figure anything out. It makes him feel all warm and fuzzy inside when he makes something and others use it to make their lives better. We like his big heart. Tim enjoys “experimenting with food,” and is just a bit addicted to World War II movies.

Subscribe to Our Blog

Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.