Best Practices

This is our definitive list of guidelines, suggestions, recommendations, etc

link - 

Numbers 1 - 10 are aimed at Beginner Programmers.
Numbers 11 - 20 are aimed at Intermediate Programmers.
Numbers 21 - 30 are aimed at Advanced Programmers.

1) Very simple project structure, no sub folders
Fewer files means more consistency

2) Write only one statement or declaration per line.

3) Always indent your code to make it more readable.
You can use spaces to indent your code, although tabs are recommended.
Indenting your code consistently makes the code easier to understand and the program flow more obvious.
Always vertically align curly brackets.

4) Try to declare your variables at the top rather than close to where they are being used.
Declaring some variables just before they are used can make refactoring the code easier but it means you have to look in multiple places.
Remember to keep your methods to less than 30 lines.

5) Avoid putting your if statements on a single line
Do not use the ternary operation ?

if (bvalue == true) 

6) Use camelCasing for local variables and method arguments.
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.
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
Use upper case for constants

7) Use predefined type names instead of system type names like Int16, Single, UInt64, etc
Use the alias names

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

8) Use meaningful method names, property names, variable names, etc.
Only use the same method name when it is overloaded
Using the same name with a different access modifier will work but is extremely confusing.

9) 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.

10) Avoid using namespaces always be explicit

using System.Collections.Generic 

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

Intermediate Best Practices

Numbers 11 - 20 are aimed at Intermediate Programmers.

11) Declare all member variables at the top of a class, with static variables at the very top.
Group all the variables with the same data type together

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

12) Always specify the access modifier, never rely on the defaults
private for member
internal for class

13) Add prefixes
Any procedures or functions that are declared inside classes should be prefixed
This makes it a lot easier to identify and find your code in the intellisense

public void Method_DoSomething(); 

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

15) Any public class members should only be accessible through Properties

16) Always use "this" when accessing class member variables (esp private variables)
Prefix private member variables (or internal fields) with an underscore so they are not confused with the method parameter

private string _myfield; 

17) Use ex as the variable for a general exception

catch (System.Exception ex)

Never embed or nest try-catch statements

18) Declare the variables outside the try-block and initialise them inside
Add local variables inside the try-block, not above

19) 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

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

public enum Color 

Advanced Best Practices

Numbers 21 - 30 are aimed at Advanced Programmers.

21) Use Tabs to indent your code

22) Only use the implicit type var for variables when it makes sense

var customers = new System.Collections.Generic.Dictionary(); 

23) Avoid using the conditional ternary operator
It makes your code very hard to read and depending on your changes you might have to remove it altogether at a later date.

myCombo.SelectedItem = string.IsNullorEmpty(sVar) ? "one" : "two"; 

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

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

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

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

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

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