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

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. 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.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago