C# Naming Convention
- Give your variables some meaningful names instead of keeping it mysterious.
Mysterious names examples are:
Either it’s difficult to understand the meaning or relate it with some functionality.
So let’s try to give these variables, properties and methods self-explanatory names:
If you look at the variable names here, you will see that it’s a bit lengthy, demands you to type more but now you can easily remember them and can trace its different usage across your application whenever you see these names.
That is why I am presenting the naming conventions that you can follow in C# code.
2. Don’t name your methods which is too lengthy such as:
3. Don’t use Hungarian notation in C#. Back in the days, developers following pre-fixing or suffixing data type in variable names so they don’t have to remember data type and it was popular in C++ but now latest IDEs are useful, we can hover the mouse over variable and IDE will show its days type.
So instead of having following variable names:
Change it to:
4. Avoid vague and ambiguous names.
This method name doesn’t specify clearly whether it’s selecting multiple items or is it processing items that were multi selected. I would have to look at it’s implementation to understand its purpose.
5. Noisy names : Don’t add extra words to variable names than required.
Instead make it simple as follows:
.NET follows two naming conventions :
Class, property and method names must follow Pascal case and local variables, parameters, private fields must follow camelCase. Fields must also be prefixed with an underscore.
6. Poor method signature:
Try to avoid method signature that has parameter names that seem out of context and return data of different context altogether. See following code example for reference, GetCustomer is supposed to return a Customer but returns an Orange and it takes an integer airplane when in fact airplane sounds like an object.
7. Method parameters:
If your methods have repeated parameters and their count is more than 3 then think of encapsulating them in an object.
8. Out parameters:
Try to encapsulate returned data and out parameter in an object.
9. Variable declaration inside method:
Declare variable where its most likely to be used instead of always declaring it on top.