Performance Code in C#: Part 2

Nilesh Yendhe
5 min readMay 23, 2021

Some of the best practices we should follow while doing software development.

Photo by Nicholas Priest on Unsplash

In the Performance Code in C#: Part 1 article, we have mainly covered how to check the performance of the code.

Now, in this article, we will check some of the best practices we can follow while doing software development.

Code Reviews

  • Remember a very famous quote: “There was knowledge everywhere. Go get it from anywhere you can.” — Rancho (3 Idiots Movie)
  • Code review does share the knowledge within the team.
  • Actually, it does not matter at which level we work, whether it is a senior developer or a trainee developer, we should ask our colleague to review the code.
  • Even if we know our code is bug-free or it will perform well in the production environment, code review gives more insights like how 10 times we can write our code in a very different / better way because every other developer will look at that same code in his very own way.
  • It gives more power to teamwork, how we communicate in a better way with our team members, how we take suggestions from reviewers, how we respond to their review comments.
  • It improves our code quality and communication day by day basis.

Comments in the Code

  • I have added this point right after Code Reviews because they are so interconnected.
  • Proper commenting in the code shows how mature a developer you are.
  • We should not write comments for every line but write wherever it is necessary.
  • Comments text should be limited, but with effective words and it must define the purpose of the code block.
  • Very important, Comments POWER: Suppose, we want to debug the code and resolve the issue asap, but if we spend some time reading the comments or code documentation which will save hours of debugging efforts of ours by knowing where to check and fix the code.
  • It helps the code reviewers to understand the functionality, so it improves the code review process and it’ll get complete in a very fast time.
  • Proper commenting will also help developers who have less experience.

Exception

  • COST of EXCEPTIONS is VERY HIGH.
  • It is not good practice to throw exceptions in normal situations.
  • It is very simple/cheap to add the try/catch block, but actually, it is very effective when we throw the exception. Because the application needs to clear the resources after that.
  • Please check the below-mentioned sample code example, we can parse the int value in two ways either int.Parse or int.TyrParse, the code snippet says function ParseInt will throw the format exception saying “Input string was not in a correct format.” if we get a non-numeric input parameter, whereas the ProperWayToParseInt function will not throw the exception.

Logging the exception

  • If we get an exception, we don’t have to just throw it as it is to the customer’s face, record it for our debugging purpose, and send a custom error message at the front-end. (Customer Experience Improvement)
  • If it is a critical exception then set the alert where we need to take the action on an immediate basis.

Declare variables wherever necessary.

  • When we are declaring variables in our code, we are actually telling the computer to allocate some space/memory to my variables.
  • Memory is playing a very important role in application performance.
  • So, we need to be very cautious for what purpose we are declaring variables and at what location where actually it is necessary.
  • If the scope of the variable is limited for the method then we should only declare them inside methods rather than making them private.

Avoid HardCoded values

  • We should not encourage this culture to hardcode the values in the application itself, it leads to security vulnerabilities also.
  • And suppose if we want to change one config value, after changing the value in the application, we need to build and deploy the complete application which is very risky when there is high traffic on our application.
  • I’ll suggest maintaining the config in the external infrastructure (Suggestion: Cache Database) and access it from there.

Async / Await

  • It is a very powerful feature when it comes to the performance optimization of the application.
  • When we know functions that we are executing one after another and they are not dependent on each other, we can execute the same code in the async way which saves a lot of time.
  • Please check the below-mentioned sample code, the code block says FirstMethodAsync and SecondMethodAsync are independent methods, they will execute parallelly and they will take it’s own to complete it.

Handled unmanaged resources

  • Resources that have been created by our code are getting cleaned up by garbage collection which happens automatically, we don’t have to worry about that. But the resources like file handling, network I/O connection which we need to handle explicitly.
  • When we have a disposable type, there is a very strong in-built functionality in the C# called “using”. It internally calls Dispose when the code flow leaves the current scope of the application.
  • When we use this functionality it frees the memory when they are finished with the specific object.
  • Please check the below-mentioned sample code example.

Don’t put too much pressure on the Garbage Collector.

  • Garbage collection is the process that determines which object is currently not used and removes them to free up some space in memory.
  • It means, it does a VERY GOOD job. BUT.
  • When our application spends more time garbage collecting, it spends less time executing code, and it means it directly hurts the application performance. (So too much work by the GC, application performance will get impact)

There are so many other pointers we have to mention in the best practices, I tried to cover some of the major pointers.

If you have any suggestions apart from these, feel free to leave a response.

I hope this helps some people understand a little more about the best practices which we can follow while doing the software development.

Thank you so much… !!!

Enjoy coding…

--

--