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#.
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.
Always use PascalCase (first letter of each word capitalized) for module names. Consistency is essential for readability.
While brevity is important, avoid overly cryptic abbreviations that might confuse other developers. Use meaningful words that convey the module’s purpose.
Class names and object instances should generally be named using nouns or noun phrases that represent the entities they represent—for example, ‘Customer’, ‘Order’.
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.
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.
Choose module names that fit logically within the namespace hierarchy. This helps in organizing and structuring your codebase.
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.
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.
Again, PascalCase is the preferred naming for properties. For example, ‘UserSettings’, ‘HasChildren’.
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’.
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.
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’.
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.
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’.
Avoid using Hungarian notation (prefixing variable names with type indicators) as it’s no longer considered a best practice in modern C# development.
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 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()’
Always use PascalCase (first letter of each word capitalized) for module names.
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()’
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.
We love to make cool things with cool people. Have a project you’d like to collaborate on? Let’s chat!
Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.