BP273: Name your variables, methods, classes properly

One of the best practices in .NET Core and C# is to name your variables, methods, and classes properly. This may seem like a trivial thing, but it can have a significant impact on the readability and maintainability of your code. Proper naming conventions make it easier for other developers to understand your code and can help prevent bugs and errors.

When naming variables, it's important to use descriptive names that accurately reflect the purpose of the variable. Avoid using single-letter variable names or abbreviations that are not widely understood. For example, instead of using "i" as a variable name, use "index" or "counter" to make the purpose of the variable clear. Similarly, instead of using "str" as a variable name for a string, use a name that describes the content of the string, such as "firstName" or "errorMessage".

When naming methods and classes, use PascalCase to make the names more readable. PascalCase means that the first letter of each word in the name is capitalized, and there are no underscores or spaces between words. For example, a class that represents a customer might be named "Customer" and a method that retrieves customer data might be named "GetCustomerData". This makes it easier to read and understand the code, especially when working with larger projects that have many classes and methods.


// Example of poorly named variables
int i = 0; // What does "i" represent?
string str = "Hello"; // What kind of string is this?

// Example of well-named variables
int counter = 0; // Describes the purpose of the variable
string firstName = "John"; // Describes the content of the string

// Example of poorly named class and method
class C { // Not descriptive
    void m() { // Not descriptive
        // Method body
    }
}

// Example of well-named class and method
class Customer { // Describes the purpose of the class
    void GetCustomerData() { // Describes the purpose of the method
        // Method body
    }
}

Comments

No Comments Yet.
Be the first to tell us what you think.

Download Better Coder application to your phone and get unlimited access to the collection of enterprise best practices.

Get it on Google Play