Are you experienced .NET developer? Or are you just starting out? Honestly, I don’t care. What I care about is if you follow the best practices from the beginning or not. 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.
Don’t feel offended. Be grateful instead. And print this bunch of advice I wrote below. It’s a proven recipe for clean, high-quality .NET code, and it contains the following sections:
1. General Technological Concepts
2. Code Quality
4. Frameworks and tools
6. 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.
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.
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 executing 20% 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
- 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
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.