What are PHP Peekaboo Bugs? What causes them and how to fix them?

September 24, 2013
Posted in Advanced PHP

peekaboo-bugs

Peekaboo bugs are the worst sort of bugs, because they just pop-up randomly and are really hard to track down.

When a PHP peekaboo bug appears, I get very nervous because I know that I might be spending hours trying to track it down. I also get nervous because the peekaboo bug itself, tells me something is rotten with the codebase to begin with.

What causes a Peekaboo bug to appear?

In a nutshell: messy code.

Messy code typically means code riddled with ultra long monster-methods … or as some people refer to them: god methods. If you have a method/function with 200 lines of code and 3 or more nested conditionals – you are begging for a peekaboo to appear!

Squashing the PHP Peekaboo Bug

The first thing to do when peekaboos show up, is to localize (as best you can) where the source of this error is. You may only be able to narrow this down to the class, but that doesn’t matter … what you are looking for is the aforementioned monster-methods or god methods.

Once you’ve identified these, it time to start simplifying/refactoring them and do what Fowler calls ‘extract method’. This is the process of breaking down these massive monstrosities of nerd madness into smaller more manageable chunks.

… So for example, you might take out a few if and if else statements from a method and place them into their own method that the original monster-method can call. Besides making the code much easier to debug and update, you will probably squash your peekaboo bug in the process.

A final tip:

You should always be looking to refactor code AS YOU BUILD your app. Think of it as opportunities when you are coding and you run across a messy chunk of code that can be easily simplified.

Thanks,

Stefan Mischook
killerPHP.com

Comments

Comments are closed.

To Top