Improve code quality in .Net applications

Writing source code for any requirement is always a head-scratching activity. But when it comes to improving the quality of code, it seems to be a different world.

Though maintaining code quality looks like overhead at the beginning of the project, but as code grows and the development team expands, the initial effort on focusing on setup quality gateways looks fruitful.

Few tools and techniques are summarised here which should become an integral part of each .Net project.


Improve Code Quality in .Net

Find unused code:

ReSharper is one of the best code analysis tools that can be integrated with Visual Studio

To find unused code in your solution right-click on the solution > Select "Find Code Issues" > One of the results is "Unused Symbols". This will show you classes, methods, etc. that aren't used.

Reference - https://stackoverflow.com/questions/245963/find-unused-code

Discover dead code using Visual Studio Code Analysis:

Visual Studio provides this feature out-of-the-box. Right-click on the solution > Analyze > Run Code Analysis on Solution.

Reference - https://blogs.msdn.microsoft.com/habibh/2009/07/31/discover-dead-code-in-your-application-using-code-analysis/

Static code analysis tool:

StyleCop and FxCop are the most popular tools that can be included in the project to maintain coding standards defined by the organisation. 

They are available as a NuGet package and gets auto-configured when added. These tools follow basic coding standards defined by Microsoft but can be customised further as per the need.

It can be configured to fail the build if any of its code quality checks fail. It can also be configured to fail the build if any warnings are identified. This ensures the code delivered is of optimum quality.

Enable Gated check-in using VSTS Build Definition:

Gated check-in builds ensures that the checked-in code is always free from compilation errors. This can be enabled in Visual Studio using TVFS projects using VSTS Build Definition, though this feature is still not available in Git team projects.

Reference - https://www.codeproject.com/Tips/1168853/Enabling-Gated-Check-in-using-Visual-Studio-Team-S

Code coverage analysis:

Code coverage analysis is available out-of-the-box in Visual Studio. This analysis indicates the percentage of unit tests written for the project.

The attribute [ExcludeFromCodeCoverage] can be applied to the class files that need to be excluded from code coverage. Make sure not to apply this attribute on the application code, as it will produce over-inflated coverage metrics.

Class files created automatically by adding service references must be excluded from code coverage. The CodeCoverage.runsettings file can be used to accomplish this.

Include automation scripts:

When development is in progress, there are several instances where fixing one issue or developing a new story would unexpectedly break existing functionality. 

QA team can only focus on testing the specific feature or user story in a sprint. But as the system scales, it becomes practically impossible for the QA team to perform integration testing in each sprint.

Automation scripts are proven to be a boon in such cases which can be executed either manually or scheduled after each deployment. This can also be added into the Continuous Integration pipeline to automatically run after each deployment.

Selenium scripts are recommended for automated testing in .Net applications. Selenium is powerful enough to execute scripts on the server even in absence of any browser. This is made possible by adding a specific browser driver as a NuGet package. Some browsers like chrome can be run in headless mode to execute the scripts faster as this will run chrome in the background.

Though surprising it is not recommended to use Internet Explorer anymore but focuses your application to fully functional on Chrome or Firefox.

Use mocking framework:

Mocking frameworks like NSubstitute or MOQ helps to write unit tests by mocking the reference layer. This means while writing unit tests for the web layer you can mock the business layer; similarly while writing unit tests for the business layer you can mock the service layer.

Design patterns:

Few design patterns are a must-have for any development. They include dependency injection, factory pattern, service pattern and repository pattern.

Dependency injection with Unity framework can be easily integrated with a NuGet package. It is highly recommended to use the DI pattern as it also helps in writing effective unit tests with a mocking framework.

Exclude NuGet packages from code check-in:

TFS or VSTS can be configured to download the NuGet package from nuget.org during the Build process.

Whenever a NuGet package is added to any project, it not only download its assemblies but also adds an entry into packages.config file. Hence it is the best approach to eliminate any NuGet package from check-in into source control and just check-in respective packages.config file.


Build quality checks:

Add task "Build Quality Checks" in the VSTS Build process. Build can be configured to break based on quality metrics like a number of warnings or code coverage.

You can also specify a code coverage threshold that is the minimum code coverage value in percentage terms.

Modify email files arriving into SMTP server

When you send any email it arrives in the Pickup folder of the SMTP server in the form of a .eml file. SMTP server continuously monitors the Pickup folder for any incoming file.

As soon as any email file is received in Pickup, it is picked to check its integrity. If the format of .eml is incorrect then it is sent to the BadFiles folder, otherwise, it is sent to the Queue folder from where SMTP forwards email to specified recipients.

If there a requirement to modify recipients, subject or content of the email after it arrives in the SMTP server, then it can be achieved using a different mechanism.

SMTP server rules:
You can create rules in the SMTP server to modify email before forwarding it to recipients.

Create macros:
Macros are very powerful scripts that can read the content of .eml files. They can be written in VB Script (.vbs file) which can read each incoming .eml file within the Pickup folder and process as needed.

Socket programming:
Write a code based on socket programming or HTTP pipeline that picks the inflow of content to the SMTP server.