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“.
He has also authored the book, Building Web Apps with Spring 5 and Angular.
Latest posts by Ajitesh Kumar (see all)
- Difference between True Error & Sample Error - December 26, 2019
- Python – How to Create Dataframe using Numpy Array - December 15, 2019
- Hypergeometric Distribution Explained with 10+ Examples - December 14, 2019