Years ago I nearly failed in an exam just because my code wasn't easy to follow and understand. My lecturer was lost in the code but he knew that it was working as expected so he awarded me the lowest possible grade just to let me pass the exam. I learned my lesson! Your code may work fine but it may also be rubbish at same time. You get what I mean!
Everyone can have their opinion on how to code but at least we all should follow some kind of standards or best practises to make our code easy to understand, easy to follow, easy to refactor so on. The standards and the best practises differ from person to person so I'm going list what I consider is the best when it comes to coding.
Some obvious points below have flexibility so I don't push myself to the limits too hard. If you don't leave a space for flexibility then you won't enjoy what you're doing. Obviously we're writing a piece of code, not trying to rediscovering the World. Also some of the points are language or framework dependent.
- I follow PSR-2 Coding Style Guide.
- I use TDD (Test Driven Development) framework PHPUnit and PHPspec.
- I use BDD (Behavior Driven Development) frameworks like Behat.
- I version my code with Semantic Versioning.
- I try not to define more than 5 method parameters.
- I try not to have more than 15 methods in a class.
- I try not to have more than 25 lines of code in a method.
- I try to keep "public" methods as thin as possible and do most of the work in "private" methods.
- I avoid having God objects in my applications.
- I follow DRY, SOLID and Law of Demeter principles.
- I analyse source code and fix PSR coding standards issues with PHP-CS-Fixer or PHP_CodeSniffer.
- I use PHP Mess Detector to look for potential problems or bad smell within the source code.
- I use PHP Copy/Paste Detector.
- I use phploc to measure the size of my projects.
- I try not to introduce unnecessary functionality because You aren't gonna need it.
- I'm aware that Worse is better.
- I follow Agile software development methods.
- I always use GIT, apply git-flow and branching model techniques.
- I always consider refactoring my code.
- Before and while coding I tend to use ERD and UML modelling techniques.
- I don't like Cargo cult programming.
- I believe that all UI and UX people should understand Principle of Least Astonishment.
Interesting things to know