There are multiple rules out there which help to achieve the clean code in your codebase. All of them are worth to familiarize with. But if I had to choose the most important one – I would say it will be SOLID principles. If you will write SOLID code, then probably you will implement many other rules, which are required to achieve it’s rules.
Another thing to know are the design patterns. You can find many online tutorials – below I will list some links, where you can find some interesting materials. There are many patterns to know, but again – if I had to point the most used it will be:
In the Laravel world, there is another popular pattern – Repository. But in my opinion, since there is an ORM inside of framework – there should be some condition when you should use it (instead of using it everywhere). I will address to that later in my next articles.
Why you should learn them? They will help you to resolve the common problems, but that’s only a part of it’s power. Another thing is the “standardization” of the code so that other programmers will recognize the pattern and will know what they can expect from it and how it should work.
Here you can find a great online library with design patterns examples and descriptions:
It’s worth to also watch this guy, which presents design patterns based on “Head First – Design Patterns” book. His explanation of the strategy pattern is just great, but please don’t forget to watch the other .
Must know that there are the PHP Standard Recommendations – PSR.
The ‘must know’ are PSR-1, PSR-2 and PSR-4. Sometimes it’s hard to remember all of them, so it’s worth to use some code formatting automation. Here you can find one of the most popular: php-cs-fixer, which will help you to properly format your code:
Another thing : “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” : John F. Woods, Code For The Maintainer
Also, check out that list – I will use some of these rules in my next articles: programming-principles
This was a short article, but has a lot of things to learn 🙂 In the next posts I will put some more “direct” examples how to write clean code – but most of them will be based on rules mentioned in this part.