This week we made a recording on the Café debug podcast about Design Patterns, good habits and good sense. The podcast is in Portuguese, but I dcided to write our conversation in English, of course just a summary.
Let's start with Clean Code. The book Clean Code for some people is considered the Bible of developers, because it teaches some techniques to make yours code cleaner and more inteligible.
But, why do we need to wirte code? Let´s see.
- Because we want to deliver results faster;
- The requirements were changed during development;
- The code was bad and a given situation´s refactor is not a good idea;
And what changes in a programmer´s life when you write clean code?
- Evolution as a developer: you make your code cleaner, to improve your skill;
- Produtivity increase: the team can have a produtivity increase and fast delivery;
- Clean, cohesive code: a code that everyone can understand where everyone talks the same "language" fallowing an standard;
- Reduce bugs: of course, the quantity of bugs redeced in your code the project and you have less time for correction.
Let's see some good practices that can be applied to clean code.
Your method, class or variables need to have meaningful names. It's not important if your code name is long, but the name needs to be specific according to its functionality.
You cannot put comments in your code, I think it depends on the situation. Normally we can put comments when we are talking about the code version, or business roles or something specific. So, avoid comments in code.
Try to make your code more elegant, write very well, think about someone that will read your code.
Unit testing is important, very important and you know this. Unit testing prevents future bugs in your project, and tests will teach you if you're learning.
But why don't we test it? Let's look at some reasons why some developers don't test their code.
- A small amount of people understand that unit testing is very important
- Don't know how to test your code
- Short deadlines
- The project is not prepared to implement unit tests
- The pressure from the team or manager
- Bad architecture and "code smell"
So I will leave some links about documentation of the unit test on the Microsoft platform .Net and the documentation of the framework Moq.
1- Best practices for unit testing with .NET Core e .NET Standard
2- Framework Moq no Net
4- Link to technology podcast Café debug
5 - Code smell (Martin Fowler)
6 - Refactoring