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.

My list

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.

  1. I follow PSR-2 Coding Style Guide.

  2. I use TDD (Test Driven Development) framework PHPUnit and PHPspec.

  3. I use BDD (Behavior Driven Development) frameworks like Behat.

  4. I version my code with Semantic Versioning.

  5. I try not to define more than 5 method parameters.

  6. I try not to have more than 15 methods in a class.

  7. I try not to have more than 25 lines of code in a method.

  8. I try to keep "public" methods as thin as possible and do most of the work in "private" methods.

  9. I avoid having God objects in my applications.

  10. I follow DRY, SOLID and Law of Demeter principles.

  11. I analyse source code and fix PSR coding standards issues with PHP-CS-Fixer or PHP_CodeSniffer.

  12. I use PHP Mess Detector to look for potential problems or bad smell within the source code.

  13. I use PHP Copy/Paste Detector.

  14. I use phploc to measure the size of my projects.

  15. I try not to introduce unnecessary functionality because You aren't gonna need it.

  16. I'm aware that Worse is better.

  17. I follow Agile software development methods.

  18. I always use GIT, apply git-flow and branching model techniques.

  19. I always consider refactoring my code.

  20. Before and while coding I tend to use ERD and UML modelling techniques.

  21. I don't like Cargo cult programming.

  22. I believe that all UI and UX people should understand Principle of Least Astonishment.

Interesting things to know