1. DRY: Don’t repeat yourself.
DRY is usually the easiest principle to understand, but it is quite harder to apply. It means that when finding similar code in two or more places, we should abstract them into a new method and change the previous code fragments so they will now call the new method with the appropriate parameters.
DRY is maybe the most universal coding principle, I have never found a developer who would argue that repeating code is good, but, I have found developers that forget about this principle when coding.
2. Write short methods.
There are three very good reasons for writing short methods. In this way, Your code will be:
- easier to read.
- easier to reuse (short methods are likely to produce loose coupling).
- easier to test.
3. Code to an interface, not to an implementation.
This is a classic one, coding to an interface will free us from the implementation details, we just define a contract and rely on calling the defined operations on the contract, expecting that the actual implementation will be passed to our code or decided at runtime.
4. Use good names for your classes, methods, variables and everything else.
There is nothing nicer than using some other developer code and not having to read its documentation because the names of the classes and the methods are telling us everything, so, make everyone’s life easier and take this approach, expend always a few seconds before naming any element in your code. Please, do it.
5. Assign the right responsibility to each class.
One class, one responsibility. But not any responsibility, the right responsibility: so if we have the class ‘Customer’, we won’t assign to it the responsibility to create a new sales action, we will just assign it the responsibility to handle all the data related with a customer.
6. (Too many) Comments are evil.
Most of developers taught that comments are good, and actually it’s better to have a comment in an obscure piece of code than just having the code by itself. Even better than having a comment for an obscure piece of code is to not to have that code at all, just refactor it until is a nice and readable piece of code.
7. Respect the best practices of the design pattern you choose.
Be sure to respect the common best practices. In example, if you choose the MVC design pattern:
- never write SQL queries in the views.
- write skinny controllers and ‘fat’ models.
- the entire application logic must be in the controllers.
8. Test, test, test.
The most tests you have, the better, they are our safety net for all the changes we will have to perform in the code in the future.
9. Refactor often and sooner.
Software development is a continuous discovery process, in order to keep up to date with good code that matches the new/changing requirements is essential to refactor the code as we go. As this is a risky task there are 2 main preconditions to avoid entering new bugs into the system.
10. Have code reviews.
We all make mistakes, and there’s nothing better than asking some other person to have a quick and informal review in our code to find them. In order to make the reviews, it’s better not to wait until the code is completed, ask for reviews whenever some important part of the code has been completed (or when a few days have passed from the previous review).
