Tips to write better C# Code- PART1

Tips to write better C# Code C# Tutorials Code
In this tutorial, we will teach you about the best tips for writing better C#/.NET codes. Although there are various similar tutorials but none of them proved to be fruitful for solving the errors/problems. But I am sure this tutorial would be of great help to my viewers/readers.

Coding standards have always been a point of hot conversation among the developers and creators. But it seems that no one cares about following the exact coding standards. I think most of the developers are not familiar with the fact that the coding standards are what which can help us achieving the consistent outcomes in our projects.



Name your codes:

This is really an important fact that we need to name each and every code we write. I know it is one of the toughest jobs we can ever perform as a developer and software creator. But if we realize how much significant and beneficial it can be, then I am sure all of us would follow the exact characteristics, classes and methodologies for naming the codes, without even bothering ourselves to think of the possible arising hurdles. The meaning of a code gets clear if it is properly named.

Here is an example of how we can properly name our code(s):
public void DoFizzBuzz()
{
   for (int number = 1; number <= 100; number++)
   {
       var output = GetFizzBuzzOutput(number);      
       Console.WriteLine(output);
   }
}

private static string GetFizzBuzzOutput(int number)
{
   string output = string.Empty;
   if (number%3 == 0)
   {
       output = "Fizz";
   }
   if (number%5 == 0)
   {
       output += "Buzz";
   }
   if(string.IsNullOrEmpty(output))
   {
       output = number.ToString();
   }
   return output;

}























How to judge a better code?

A better code can be judged with the property that it is readable. As long as you are able to read your own code clearly, it would be regarded as the better code.
Pascal Case:
We can use the Pascal case for identifying 3 or more than 3 characters.
Camel Case:

Another characteristic of a better code is that it should be helpful to the people looking for the solution. Let’s say one of your friends wants to fix/resolve an error. In an urge to get the solution, he comes across your code. As long as he is able to get benefited from your code(s), it would be regarded as a better code.
Make sure that the code is not confusing. Modifications are an ultimate part of any project. You can’t say that whatever code you have written, cannot be changed. It can be whenever amendments were required. Isn’t it wonderful if you code is more viewed and liked after a little modification? I think it is. So don’t consider it to be a final work-piece as there is always a room of improvement.

Improving the Code’s Readability:

Before you take steps of preparing your own code(s), you have to increase your knowledge. This can only happen if you make yourself habitual of learning and observing what other people figure-out. Try your level best to understand and get inspired from others’ work and then come up with a more sophisticated and unique code.

Writing a code is not possible without practice and experience. This is not a cup of tea which you can prepare easily. It needs to keep into consideration some standards. A lot of coding tools are also available which can help you write your unique codes. A few of them include FxCop, StyleCop and JetBraitns ReShaper. All these tools are especially popular in writing and analyzing various C# source codes. Another advantage of these tools is that you can implement them for writing statistical code and .NET code.

Coding Conversations:

Coding Conversations are the tools/guidelines which are recommended for specific programming languages. For each code, a specific style, practice and methodology is used to write its programming language. There is no chance that one code’s language would be as per used in another code’s language. However, similarities can arise.
The conventions with different properties, variables, methods and class names are considered to be more reliable. It is because these conventions are accompanied with their specific values. Building one’s own convention is a matter of great skills. Only professional and highly experienced people can build / develop their conventions.
To make the tutorial more effective and informative, I am giving some examples of C# coding standards, conventions’ names and a few most effective practices to boost up the creative abilities of my readers. 

Here are a few situations of how to write Camel case more professionally in different identifiers.


Tips to write better C# Code- PART1 C# Tutorials

Some Naming Conventions’ Examples:


I am really not willing to mention all the naming conventions in detail. The best suitable and practical I have found to be for myself are given below.
1. C# Coding Conventions
2. C# Coding Guidelines
3. C# Coding Standards and Best Programming Practices
4. C# Coding Standards and Naming Conventions

Pascal Case must be used:
public class Product
{
    public void GetActiveProducts()
    {
      //...
    }
    public void CalculateProductAdditinalCost()
    {
        //...
    }

}













Camel case must be used:
public class ProductCategory
{
  public void Save(ProductCategory productCategory)
   {
       // ...
   }

}  
Abbreviations need to be avoided:
// Correct
ProductCategory productCategory;

// Avoid
ProductCategory prodCat;

In case of identifiers, Underscores should not be used:

// Correct
ProductCategory productCategory;

// Avoid
ProductCategory product_Category;
Letter I needs to be prefixed with the interfaces: 
public interface IAddress
{

}







Static variables should be declared at the top of the class: 
public class Product
{
    public static string BrandName;

    public string Name{get; set;}
    public DateTime DateAvailable {get; set;}

    public Product()
    {
        // ...
    }

}














Singular names for enums should not be used: 
public enum Direction
{
    North,
    East,
    South,
    West

}










The enum names should not be suffixed with Enum: 
//Avoid
public enum DirectionEnum
{
    North,
    East,
    South,
    West

}












Next Part : Tips to write better C# Code- PART 2

If you need any Help feel free to leave a comment (or) contact us.

No comments:

Post a Comment