I have read a tweet about PHP programming today that made me furious.
“Handling complex logic in PHP is like doing the LSATs after shock therapy”
What is wrong with this statement? It moves responsibility from coder to the coding language and that is completely wrong in this case.
Firstly, PHP has syntax similar to C, which is used to code programs much more complex than most of websites. Almost all control statements are similar or same. Although there are differences, I doubt anyone would argue that there is a significant lack of logic control statements in PHP than in C. Although PHP lacks specific PC programming libraries, it has very little to do with implementing logic of application.
So, what might cause such statements?
The top reason for problems handling logic is bad quality of the code (software quality measures how well software is designed (quality of design), and how well the software conforms to that design (quality of conformance)). PHP, similarly to C, is a very flexible language. And you need to and think work to produce good, quality code.
Thus, what is code quality characteristics related to implementing logic in PHP?
- Separation of concerns. You should not handle logic, input, output, database, exceptions in single big file. Implement these as functions, or use libraries and frameworks. There is no excuse to not use a PHP framework nowadays, except when modifying old application source. And yes, I made such mistakes myself in the past.
- Avoid logic shortcuts, like die(), exit() and break (in cycles, not in switch) statements if you can. They make code much harder to comprehend in the long run. Perfect function or method has a single return statement as well.
- Use right model of computation to process information gathered. Logic will look nasty in all the languages as long as you have no foundation how to handle it. And the worst is, these programming models are already here for decades, like FSMs. Simply, they are used in software programming, but not so in web programming.
- Comments, documentation, sane names of variables and no hacking. One of the reasons for bad code is “quick fixes”, when quick, undocumented fixes are applied to program code that no one understands later on. Even worse, they are applied to the system/framework core without any documenting sometimes. That might leave you stuck with un-updated version of some framework for long time.
- It is better to comply with framework programming model than implement couple different models in single system. If you are using hook and plugin based framework as wordpress, stick to it, and know its features, internal logic and limitations.
If you will know these 5 things, all you will have to curse is your code or framework/cms you are using. But not the PHP.
2 Comments
Ahad Bokhari · September 4, 2009 at 7:55 am
We’ve all made our mistakes, but we learn from them and learn how to use the language to our benefits, not detriment. You are right tho alot of people just keep on using the quick fix time and time again till everything breaks…At least they could comment on the code they write…
Separation, exaclty matey – i know you are a php guy but you should try Rails man, the syntax is quite easy to grasp and the MVC is a joy working with. Check these posts out for more insight:
http://andyjeffries.co.uk/articles/4-reasons-why-ruby-syntax-is-better-than-phps-
http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html
Check the comments on the latter one, great read! Always good to have different perspectives mate…
Giedrius · September 4, 2009 at 9:23 am
I have checked Rails. I am not so sure I am ready to switch from PHP to rails now, but I like its implementation. Thats why I like “stricter” modular php frameworks like Cake.