Ibuildings Blogs
Thursday, February 14. 2008Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Hi,
In addition to your second point, one of the things that I do for clarity is assign complex(er) boolean to a variable and then use that in a control structure. The variable is named after the condition that has been tested. This makes it easier to see what was tested and what is going to happen with it.
Well so much for best practises with those encoding errors in point 2
Great article!
I love PHP! You can do so many things with it, it's crazy. However, there are a lot of folk out there that try to do things with it and have not got a clue how to code in the first place. Having recently taken on a project to write some modifications for an existing system, I am going through the pains of editing someone elses code. Why did they do that here? 4 SQL statements when 1 will do? Doc blocks, don't make me laugh!! Aaaaarrrrgggghhh! Learn to write neat and readable code before actually writing code people. I suppose that is what we get for making what was once a black art more accessible.
Thanks for replying!
Sometimes you do get to see some puzzling code with PHP. Then again, I saw some strange code with Javascript and Java too. As my college professor used to say, code is meant to be read first, executed second and written last. Though PHP is very loose in what it will accept, I agree with you that experience will teach you that putting in the extra time to make your code just a little bit more readable is definitely worth it in the long run.
Ad .2 ("Write control structures like you would say them"): I've noticed that some devs, most notably in Zend Framework, consistently write
false == $apples instead of: $apples == false I hate it, it feels completely unnatural, it confuses the hell out of me (even though it's quite simple and readable), but most of all: it makes me feel stupid 'cause i have no idea where this habit comes from. I figure it has to do with avoiding accidental assignments by omitting an '=' (as in: $apples = false where false = $apples probably won't parse), but i still don't like it. Ideas, opinions?
Good observation.
My view is that code is meant to be read, not written. You will probably read that if statement a lot more than you will modify it, so you should do what 'reads' better, which in this case is $apples===false. Also, practicing rule 5 will decrease the possibility of making a typo where you accidentally assign. Because then you have to forget two characters instead of one. Next time I talk to a member of the ZF team we'll discuss it
Hi Rick, your observation is correct and there's quite a good reason.
When you forget an '=' in a comparison, you will get a debugable error because the variable you try to compare to doesn't exist. You wont get this error if you place the variable in front of the comparison, because it will then just be assigned a value. $a = 1; // evaluates to true 1 = $a; // throws a php error Hope this solves the mystery |