The last few days I went a little nuts thinking about PHP and rewriting a very horrible messy script. I’d write something, make it work, write a little more, break the first bit, go back to look at the first bit and find that I couldn’t understand my own code that I’d written the day before. I ended up throwing it out completely, waking up the next morning and writing the entire thing in a nice, neat, correct way. Within a day and a half, it worked, and is readable. The most important things that improved this sad blob of pointy-looking things and regular expressions were:
- decent formatting, without slacking off! Equal sign in vim, you’re my pal.
- abstract things out into functions. Do it twice? think about making it a function.
- But don’t always, if it’s going to make a simple thing confusing. Keep it clear!
- Name the functions logically.
- Really think about naming variables so that the code makes sense when you read it.
I struggled with understanding what the hell I was doing in the first script because, I swear, everything was named $tag and $another_tag and $taglist and $tagfeed and $tag->tag until I was lost in a maze of taggy little passages, all alike. How very, very embarrassing! If you ever see that code, please burn it!
Now all the variables have very logical names so that everything makes sense. Doing that made me understand what I was actually trying to do — much better than I had understood it before. Everything became clear and fell into place.
The php.net pages are truly awesome. I did struggle for silly amounts of time trying to figure out what the hell to do with strings. Like do I want egrep, preg_match, substr, strstr, or WHAT? (I usually end up with preg_match since I know perl regular expressions reasonably well.) But I appreciated php.net/function pages very much. The explanations make sense, there are examples, the lists of related functions often lead me to stuff I want to know, and the comments by other users *completely rock*.
Meanwhile, I read PHP Sucks, But It Doesn’t Matter on codinghorror.com, and the entire crazy comment thread that is half computer science “real programmer” snobs, half even realer programmers rolling their eyes, and half people who know they are the 3rd half and who are Microsoft types (in other words, weird aliens from other universe). All I can say if any of these guys calls me a “script kiddie” I will enjoy kicking their teeth in!
Bike? I don’t need your bike! I can kick your ass with these here training wheels! Snobs.
My thingamajig now reads tags in from de.licio.us, parses them, decides what kind of thing they are, pulls out different kinds of posts, and builds lists of post titles by subject grouping and source in a somewhat complicated way. Then it writes all those lists out to a jillion little static files which will be cached… everything will be so much faster and more polite to the delicious servers this way. Joy!Related posts: