Intro
When it comes to writing code, having a clear and consistent naming convention and coding standard can greatly improve the readability, maintainability, and overall quality of your code. Naming conventions refer to the way in which variables, methods, and classes are named in your code, while coding standards are the set of rules and guidelines that govern how your code should be structured and formatted.
Why Naming Conventions are Important in C#
Naming conventions in C# are essential for several reasons. First, they make code more readable and understandable. When code is written in a consistent and uniform manner, it is easier to understand and maintain. Secondly, naming conventions help to avoid naming conflicts. Since different classes and variables can have the same name, it is important to use a naming convention that avoids conflicts and confusion. Finally, following naming conventions is essential for code portability. When code is written according to standard naming conventions, it is easier to port the code to different platforms and environments.
C# Naming Conventions
- Camel Case
Camel case is a widely used naming convention in C#. It involves starting the first word with a lowercase letter and then capitalizing the first letter of each subsequent word. For example, “firstName,” “lastName,” “dateOfBirth,” etc.
- Pascal Case
Pascal case is another popular naming convention in C#. It involves starting the first word with an uppercase letter and then capitalizing the first letter of each subsequent word. For example, “FirstName,” “LastName,” “DateOfBirth,” etc.
- Underscore
Underscore is a naming convention that involves separating words with an underscore character. For example, “first_name,” “last_name,” “date_of_birth,” etc.
- Hungarian Notation
Hungarian notation is a naming convention that involves using a prefix to indicate the data type of a variable. For example, “strFirstName” for a string variable or “intAge” for an integer variable.
Best Practices for Naming Conventions in C#
- Use meaningful and descriptive names.
It is important to use names that accurately describe the purpose and functionality of a variable, class, or method. This makes it easier for other developers to understand your code and for you to remember what your code does when you come back to it later.
- Be consistent.
It is important to be consistent with your naming conventions throughout your codebase. This means using the same convention for all variables, classes, and methods. Consistency makes it easier to read and maintain code.
- Use camel case for local variables and private fields.
Camel case is the preferred naming convention for local variables and private fields in C#.
- Use Pascal case for classes and public methods.
Pascal case is the preferred naming convention for classes and public methods in C#.
- Avoid using Hungarian notation.
Hungarian notation is considered outdated and is not recommended for use in modern C# development.
Examples of Naming Conventions in C#
- Camel case example:
int numberOfStudents; string firstName; float gpa;
- Pascal case example:
public class Student { public string FirstName { get; set; } public string LastName { get; set; } public float Gpa { get; set; } }
- Underscore example:
DateTime date_of_birth; string first_name; string last_name;
A little extra for people who want to have the “cleanest” code
There is a term for well-written code – “Clean code”. But what does it mean to write truly clean code? It is impossible to explain it in a few words. But there is a whole book about it!
“Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin is a highly acclaimed book that provides practical guidance on how to write high-quality, maintainable, and scalable software. It is a must-read for software developers, engineers, and executives who want to improve their software development processes and create software that can adapt to the changing needs of the business.
The book emphasizes the importance of writing code that is easy to read, understand, and modify, which is critical for software that needs to evolve and grow over time. The author presents a series of best practices, techniques, and principles for writing clean code, such as following SOLID design principles, using meaningful names, and breaking down complex code into smaller, manageable parts.
I will not rewrite this book but I want to give you a little overview of this book.
“Clean Code” provides several principles for naming variables and classes that are intended to make the code more readable, understandable, and maintainable. Here are some of the key points:
- Use descriptive and meaningful names: Names should describe what the variable or class represents and should be easily understood by anyone reading the code. For example, instead of using a variable name like “temp,” you might use “temperature.”
- Use pronounceable names: Names should be easy to say out loud, so that you can refer to them in conversation without having to spell them out. For example, instead of “numOfAccs,” you might use “numberOfAccounts.”
- Use names that are consistent with the domain: Names should reflect the language and terminology used in the domain of the problem being solved. For example, if you are working on a project for a bank, you might use “Account” instead of “Acc.”
- Avoid abbreviations: Abbreviated names can be confusing and difficult to understand. If an abbreviation is commonly used and well-known, it can be acceptable, but in general, it’s better to use the full name. For example, instead of “maxAmt,” you might use “maximumAmount.”
- Use names that reveal intent: Names should provide information about the purpose and intended use of the variable or class. For example, instead of “list,” you might use “customerList.”
By following these principles, you can help make your code more readable, understandable, and maintainable, which is essential for code that needs to evolve and grow over time.
Source code for the lecture
ClientActivity activity = new ClientActivity(); activity.CalculateAverageTimePerDay(5.7, 4); // hover to see the summary // Good example class ClientActivity { /// <summary> /// This is the good one! /// </summary> public double CalculateAverageTimePerDay(double timeSpent, int activeDays) { // here we calculate the average time per day for this client double result = timeSpent / activeDays; // here we return the result return result; } } // bad example class client_activity { static public double calculatetime(double time, int days) { Double res = time / days; return res; } }
If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.