PHP Code Smells and Best Practices

As application in PHP can be written in object oriented manner, most of the code smells found in the world of object-oriented programming also stay valid for PHP. Recently, I happened to do a code review of a PHP project and wanted to share some of the areas which one would want to pay attention to:

  • Naming methods using Camel Case: It is always helpful to write method names in camel case as it is easily readable. For example, instead of writing method names such as “searchurlAction”, one could write “searchUrlAction”. Software quality characteristic impacted in “Usability“.
  • Long Methods: One may want to avoid large method as these methods become difficult to change and read. It also makes code very fragile as a small change in the method can break functionality here and there if not tested properly. “Long method” is also considered as one of the most common code smell found while code reviews. The long methods are generally found to be less in cohesion and non-reusable. They do violate the “Single Responsibility Principle” which is one of the key SOLID object-oriented principles. Essentially, it is also found that long methods are higher in McCabe’s code complexity which adds to the fact of method becoming difficult in change, thereby scoring low in maintainability. Software quality characteristic impacted in “Maintainability“.
  • Long Classes: One gets tempted to put various different methods in one “Controller” class and end up creating a large class which is also considered to be one of the most common code smell and violate the “Single Responsibility Principle” of SOLID principles. Software quality characteristic impacted in “Maintainability“. People come with the argument that frameworks demand them to write all the code in controller classes including business logic. However, that is not true. One can create components around business logic (rules) and call these components from within “controller” classes.
  • Duplication: One would want to avoid duplicate code as much as possible as it adds to overhead of code maintainability.
  • Overall Code Complexity: Code complexity of a class or a method can be measured in terms of McCabe Cyclomatic Complexity. A very high value of code complexity depicts the low cohesiveness of the class and generally not considered to be a maintainable code. Software quality characteristic impacted in “Maintainability“.
Ajitesh Kumar
Follow me
Latest posts by Ajitesh Kumar (see all)

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Code Review, PHP, Software Quality. Tagged with .

Leave a Reply

Your email address will not be published. Required fields are marked *