Best Practices

This is a quick attempt to try and provide some guidelines, suggestions, recommendations for Coding Conventions.

link - docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions 

1) No Namespaces
Do not use "using", prefix everything in full.
Always fully qualify the names from any namespace
Organize namespaces with a clearly defined structure


2) Fewer files means more consistency


3) Always include curly brackets
Do not use the ternary operation ?
Use brackets for single line If statements
Always vertically align curly brackets.


4) Declare the variables outside the Try block and initialise them inside the Try-block


5) Add local variables inside the try-block, not above


6) Always use this prefix inside classes


7) Use PascalCasing for class names and properties
Class Names should be nouns, noun phrases or adjective phrases and should not contain an underscore
Member Names should be verbs or verb phrases


8) Use camelCasing for local variables and method arguments.
Unlike VBA do not use hungarian notation which is prefixing your variables with a data type abbreviation.
Avoid any types of abbreviations because these will be inconsistent between developers.


9) Do not use upper case (or Screaming Caps) for constants or readonly variables


10) Declare all member variables at the top of a class, with static variables at the very top.

public class Account 
{
    public static string BankName;
    public string Number {get; set;}

11) Use predefined type names instead of system type names like Int16, Single, UInt64, etc

string firstName;    // not String firstName; 
int lastIndex; // not Int32 lastIndex;
bool isSaved; // not Boolean isSaved;

12) Use implicit type var for local variable declarations.

var customers = new Dictionary(); 

13) Always prefix interfaces with the letter I.
Interface names are noun (phrases) or adjectives.


14) Do not provide a default constructor for a structure ??


15) Use ex as the variable for a general exception


16) Windows Forms Controls
Do not change any of the properties in the designer.
Always change the properties in the code for more visibility.


17) Add suffixes
Add the suffix "_Delegate" to any delegates
Add the suffix "_EventArgs" to any classes that extend System.EventArgs
Add the suffix "Exception" to any types that inherit from System.Exception
Add the suffix "Stream" to any types that inherit from System.IO.Stream
Add the suffix "en" to any enumeration types


18) when naming private or internal fields, and prefix them with _


19) Write only one statement per line.


20) Write only one declaration per line.


21) Use singular names for enumerations
do not explicitly specify a type of an enum or values of enums

public enum Color 
{
   Red,

22) If continuation lines are not indented automatically, indent them one tab stop (four spaces).


23) Add at least one blank line between method definitions and property definitions.


24) Use parentheses to make clauses in an expression apparent, as shown in the following code.


25) Do not add too many comments
Place the comment on a separate line, not at the end of a line of code.
Begin comment text with an uppercase letter.
End comment text with a period.
Insert one space between the comment delimiter (//) and the comment text, as shown in the following example.
Don't create formatted blocks of asterisks around comments.
Ensure all public members have the necessary XML comments providing appropriate descriptions about their behavior.





© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext