Peer Review: Improving The Business Logic

This entry is part of an ongoing series involving the review of a code sample and it’s refactoring. For the original code sample, see here.

So far, we’ve done quite a bit of work on our Twitter class, making it better. There’s still work to be done, though, especially improving the logic.

The Twitter class we have now has a number of logical flaws in it that we need to address. Additionally, there are some logical flaws that we started with that I want to highlight, even though we’ve already fixed them. Let’s get started with those.
Continue reading

Peer Review: Looking At Abstraction – Redux

This entry is part of an ongoing series involving the review of a code sample and it’s refactoring. For the original code sample, see here.

Editor’s Note: The response of the community to this series has been great, and I’ve been given a large number of suggestions. I’ve incorporated some of those suggestions into the code and into this article. Thanks to Jeff Carouth, Greg Beaver and Daniel O’Connor for their help and suggestions.

This entry will focus on our use of the database, and specifically on the already_tweeted() method. This method has a number of problems, and while we’re focusing on the implementation of the database, it’s important to note that we will also need to address some of the logic (which will be the next part of the series).

In our last entry, we focused on abstracting the HTTP request out to a seperate class. Lots of people wrote comments with suggestions of HTTP handlers, including pecl_http, the PEAR HTTP class, HTTP_Request2 and the PEAR Log class for logging. These are all great suggestions, and all will help abstract out the class without causing us to have to write our own implementation of common problems (the Not Invented Here (N-I-H) syndrome).

In focusing on the already_tweeted method, one thing becomes immediately apparent: it is private. This suggestion, provided by Greg Beaver relates to our first discussion of coding standards and we will change the class to a protected class for extendability later on.
Continue reading

Stop Sacrificing Readability For Efficiency!

Much was made last week over the topic of micro optimization in PHP. Most of these argued that micro optimization was a bad idea, from the perspective of unit testing, the idea that “premature optimization is the root of all evil”, the fact that it takes too much time, and that it violates the rules of development and optimizaton.

There’s another reason that micro optimization can be a bad choice: it makes code absolutely impossible to read!

The original article by Alex Netkachov makes arguments about calling static methods, using magic methods, getting the time with $_SERVER[‘REQUEST_TIME’], output buffering, and loops. But what this article completely misses is that many of these tips would render code completely useless if you had to maintain it.
Continue reading

Bug-Free: Using Function Traces To Better Understand Code (Part 3 of N)

Have you ever been handed some code and told, “make it work”? If you haven’t, chances are good you will some day. It’s often a daunting task, especially since, as one programmer told me, “comments come when the second developer has to make changes.” While this is likely bad coding practice, it’s happened to me more than once.

Lucky for us, XDebug has a built-in tool that helps us evaluate code and figure out some of what is going on without spending all week reading the documentation or the code itself. It’s called “function profiling.”
Continue reading

Bug-Free: Using XDebug To Improve Development (Part 2 of N)

There are two really great experiences in my PHP life: the day I learned how to use PHP, and the day I installed XDebug. Ok, perhaps that’s an overstatement, but XDebug is one of the best tools I’ve ever used. I think every developer should use it, and for the next part of this series we’re going to talk about its features.

XDebug allows you to do a number of things: first, it provides styling to your stack traces and stack traces every error. Second, it allows for profiling. Third, it provides some variable output tools that are just necessary when working with PHP.

XDebug is an extension for PHP, and before you do anything you should probably download and install it from the XDebug website. Don’t worry…I’ll wait…
Continue reading