Following .NET best practices is equally important for beginner and advanced programmers.
Only then you will be able to become a kick-ass code writer. It may sound big-headed but trust me. It’s not what I want to achieve by writing that article.
Following best practices of any programming language is a proven recipe for clean, high-quality .NET code. And it contains the following sections:
- General Technological Concepts
- Code Quality
- Performance
- Frameworks and tools
- Architecture
- Other useful tips
General Technological Concepts
Start with installing all the project infrastructure on your machine. Thanks to that, you won’t need any external components, such as remote databases, etc. It will make your work easier and faster.
Automate everything you can or suggest such improvements to your project leader. Be a real team player. Not just by automating, but also by showing your design, solutions or fixes to other members of your team. Moreover, don’t hesitate when someone asks for your help. Express your concerns and comments.
Code Quality
First of all, focus on component design and reusability. By that I mean you should design with extensibility in mind and use subclassing, virtual members, and callbacks.
What about the code itself? Always use full names instead of abbreviations and never write an empty catch block. Your code should be elegant and straightforward so remove commented out or dead code and improve your code’s readability. You can also do that by reading other people’s code.
When the build is ready, test it by writing and running true unit tests. Regarding external dependencies – don’t’ test them. Nevertheless, unit tests should be fast, independent, repeatable and self-validating.
Performance
As the Pareto principle says, 80% of the effects come from 20% of the causes. You can apply that law to the performance by rephrasing it – 80% of the execution time is usually spent on executing20% of the code. Use it to your advantage by optimising the most time-consuming areas.
If your application has poor performance, you can install and use a profiler. It will show you the exact reason of that. There are more ways to improve performance, i.e., running blocking tasks (like large file IO) in their threads. You should also avoid running massive calculations in the same thread as your GUI in desktop applications.
Caching is crucial for performance state but only if you do it wisely.
Frameworks and tools
You can find a great collection of tools, frameworks etc. on GitHub.
Here are some that I recommend:
- Log4Net – The Apache log4net library is a tool to help you output log statements to a variety of output targets. Papertrail supports aggregating messages from a native log4net appender, providing a live searchable log console for your .NET app.
- FluentMigrator – Fluent Migrations .NET framework
- QuartzNet – Quartz Enterprise .NET Scheduler
- NSubstitute – A friendly substitute for .NET mocking frameworks
- xUnit – A free, open source, community-focused unit testing tool for the .NET Framework
- Canopy – A free, open-source F# web automation and testing framework
- TopShelf – An easy service hosting framework for building Windows services using .NET
- Hangfire – An incredibly easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications
- Opserver – Stack Exchange’s Monitoring System
Architecture
If it’s possible, use design patterns. They are useful, proven solutions to repeatable project problems. Moreover, if you are developing web or desktop applications, use three-tier architecture. It divides user interface into three separate layers.
If you want to make your work easier, use MVC and MVVM frameworks respectively.
There is one more thing you can do to improve architecture – try to decouple layers from other layers as much as possible.
Learn more about .NET application development