Tips to write better C# Code- PART2

Tips to write better C# Code- PART2
Best tips for code developers:
The code developers can boost up their creativity by following these rules:

1. Class size should be kept small. Large class size proves to be totally confusing for the readers. Mostly the large class size becomes a violating factor in case of Single Responsible Principle.

2. An obsolete comment needs to be avoided. It is because it belongs to some old and irrelevant class which does not prove to be fruitful for the code developers. Instead it tends to migrate away from the described codes creating too much irrelevant factors in the code.


3. Another must remember tip is to follow individual and short code classes. Avoid commenting on the long codes which are of no use but wastage of time. You can definitely increase the readability level of your code if you intend to adopt the ways of avoiding meaningless comments in any code.

//ensure that we are not exporting
//deleted products
if(product.IsDeleted && !product.IsExported )
{
   ExportProducts = false;
}

// This is a for loop that prints the 1 million times
for (int i = 0; i < 1000000; i++)
{
    Console.WriteLine(i);

}














4. Try to comprehend your lines. It is because sophisticated lines in a code become difficult to understand. The professional code developers prefer using twenty to thirty lines in any code. But this is the maximum range. If you want somehow simple code then even one to ten lines would be sufficient. The Exact Method can be applied to keep your code’s length proper. A Visual Studio ReShaper can also help you comprehend your code. 




5. You need to avoid using too many parameters in the code. Instead of using bundles of parameters, you can make use of a class.





       //avoid
public void Checkout(string shippingName, string shippingCity,
       string shippingSate, string shippingZip, string billingName,
       string billingCity, string billingSate, string billingZip)
{

//DO
public void Checkout(ShippingAddress shippingAddress,BillingAddress billingAddress)
{
}

















6. I have personally observed a lot of times that complex expressions have some hidden meanings. So we have to avoid them because they can seriously damage our code information.

if(product.Price>500 && !product.IsDeleted &&
            !product.IsFeatured && product.IsExported)
{
   // do something

}











7. Warnings should be taken as errors—serious errors.


8. I would also like to recommend you to minimize the number of returns in each outline/number of routines.
//avoid
if(product.Price>15)
{
   return false;
}
else if(product.IsDeleted)
{
   return false;
}
else if(!product.IsFeatured)
{
   return false;
}
else if()
{
   //.....
}

return true;
//DO
var isValid = true;
if(product.Price>15)
{
   isValid= false;
}
else if(product.IsDeleted)
{
   isValid= false;
}
else if(!product.IsFeatured)
{
   isValid= false;
}

return isValid;
Concluding the whole tutorial I would like to say that every organization has its own standards for implementing its code within the projects. But one should always keep in mind that whatsoever the code may be and whatsoever its standards are, it should be written in a way which is both readable and informative.

No comments:

Post a Comment