Best coding practices


Learning


Best coding practices

Best practices to write C# code:

To make the best coding practice, below are the few points which we should always follow:

1) Variable names should begin from small letter and should follow Camel-Casing structure.

    For example: count, totalAmount, todaysDate and so on.

----------------------------------------------------------------------------------------------------------------------------------

2) Every function name must have a meaningful name and must contain function description as comment before initializing the function.

    For example:

    // Function to get record by passing id 
    public Record GetRecordById(int Id) {

    }

----------------------------------------------------------------------------------------------------------------------------------

3) Using proper space before using any operator.

    For example:

    if (i == 0) {

        // Statement comes here...

    }

----------------------------------------------------------------------------------------------------------------------------------

4) Do not hard code any value within variables. The only things that should be hard coded are constants.

     For example:

    string someVariable = “Hard coded value”;
 ----------------------------------------------------------------------------------------------------------------------------------

5) Use string.Empty instead of using double quotes (“”)
     Good : 

     if (someVariable == string.Empty)
     {

        // Statement comes here...

     }

    Bad : 

     if (someVariable == “”)
     {

        // Statement comes here...

     }