Monday, December 8, 2008

Good and Bad PHP Code

Today I read a blog about good and bad php code.

When I asked this question to myself, what is good php code, I surprisingly find out that my brain is blank! However, when I try to ask in the other way, what is bad php code, I can give tons of examples. Below is some of them.

0. Variable is not initiated.
1. Not using isset to check if a variable is initiated.
2. Not checking data type
3. Global variables everywhere
4. Static methods everywhere
5. Long functions (function should be short & simple enough that you even think unit test is not required)
6. Long parameters
7. Duplicate codes
8. Low efficient coding style
9. huge class
10. No memory management concept in mind (Yes, even PHP we need to think of that!)
11. Implement large project without MVC architecture.
12. Put business logic in presentation logic(Especially for smarty users)
13. Like to use too many if, else, switch to control the logic
14. Without security concept in mind (sql injection, string filter)
15. poor DB modelling and SQL statements, e.g. using too many Mysql Fucntions which could be done in PHP, using IN in sql Query (Although this is not PHP code)

No comments: