Lost my mind in PHP

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!

5 thoughts on “Lost my mind in PHP

  1. That’s an awesome tale. Rewriting is even more satisfying than refactoring.

    I love the inspirational PHP poster, too.

  2. Liz,

    It struck me how much your coding experience is like getting a job. Here’s why:

    1. We start out with all these “tags” that we use to describe ourselves. Collectively and in context, the tags don’t always paint the picture we’re trying to convey. It doesn’t do (get us) the job.

    2. We’re not always clear on how an earlier piece of code (say, a resume) ever saw the light of day. Once reviewed, that version is tossed in favor of a rewrite.

    3. The rewrite hopefully has decent formatting, stated functions, clarity and logic, and makes sense (relevant to the job you’re applying for).

    It’s only when we get to this stage that our “code” works (or, well, gets us an interview).

    In the same way that your forums rocked, job-finding communities also rock. Yeah, we know that applying for a job with no connections is a bit like a cold call from a sales person. That’s why our network of friends and sites like LinkedIn can be so helpful. If someone meets you at the door, it’s more likely that others inside will be happy to meet you.

    Finally, as for snobs, we’ve all been there too. A good number of people were fortunate to have gone to top tier schools and have impressive credentials. Many of them make for difficult competition in the job market. Thankfully, all the great ideas and all of the excellent work being done in the world is not the exclusive domain of the top line candidates.

    Perhaps it’s a matter of writing our “code” in a way that maximizes our greatest personal resources? If so, there is still hope and joy in the world!

    judi, Digital ID coach

  3. I can say as a I use php a lot that I am always doing this with my scripts. i also do c# programming and I often do the same thing. I can usually understand short bits of my own code but when it gets longer its harder to read. its also annoying when trying to get other people to read it.

    I need to learn to name variables properly by habit instead of redoing them later. I am usually good with formatting but I should also take the time to leave comments.

  4. Well, at least you gave your vars a decent name – tag. When I just need to get the work done, I usually just write $x, $i, $ly, and so on. The main problem is, that since I started organizing my work in a more OO and MVC fashion, the code gets out of context for sake of reusability, and the result is that I have functions like doStuff($a) and encryptWithCowmilk($cow) [no kidding, I was feeling bored].

    One thing I found VERY useful is the refactoring features present in many IDEs (like VS.NET), so one can just use $x and $ppp, then rename them $tagDatabaseId and $tagPriority.

    JB

  5. I work on some code for a product that I helped write 12 years ago, and some of the code is the same as it was 12 years ago. Other parts are 10 years old, 8 years, 4 years. I really enjoy taking a look back how how I named stuff.

    I find I’m doing that now in web development, especially with PHP. First it was never_using_underscores. Then FirstCaps. Then upperLower. Now I’m worrying more about being descriptive, than how I’m doing it. I feel so much wiser.

    But I can only imagine how silly it will all look in 10 years.

    By the way, if I were to ever win some sort of Magic Contest, a chunk of it would go to Seattle Children’s Hospital via Child’s Play.

    So says I, Tom Music.

Leave a Reply

Your email address will not be published. Required fields are marked *