in Best Practices, Object-Oriented Development, PHP, PHP 5

Dealing with duplicated code

We’ve all seen it: we’re working along, and we come across code that just has a feel to it. It’s like déjà vu. You’ve seen this code before. You open another file – sure enough, there it is. The same code. Almost line for line.

In large code bases, it’s likely that there are dozens if not hundreds of similar behaviors that have to be completed more than once. And as code grows, is worked on by multiple developers, or just for the sake of speed, the same code gets copied or rewritten over and over again in different places.

But this creates a nightmare to maintain. Change one bit of the code in one place and you risk breaking all the other bits of duplicated code elsewhere. Lucky for you, there’s something you can do about duplicated code.

The beauty of abstraction

One way that we can defeat duplicate code is by abstracting the code out into a base class or object.

This strategy works well when the code meets the following criteria:

  • The code performs the same function in several different places; and
  • The various different places that the code resides are similar.

The idea behind object oriented programming is to reduce code duplication by encouraging reuse. And inheritance allows us to reuse code by moving common, core elements into base classes that are extended by objects that do similar jobs.

Of course, this doesn’t work every time. Sometimes, the duplicated code is in parts of the app that do very different things. But never fear: there’s something else we can do in that case.

Creating utility classes and libraries

When you run across duplicated code in classes or modules that have disparate jobs, it’s impossible to have these extend from a common base class. But that doesn’t mean we can’t consolidate the duplicated code in one place. That’s what utilities and libraries are for.

For example, if the same code to manipulate a set of strings is scattered throughout your application, this is a prime candidate for consolidating these functions into a single string manipulation library.

Another great example is database code. Sure, your database code may be scattered through modules that have different responsibilities, but that doesn’t mean you can’t take that code and place it in a single library that’s easy to maintain and use.

Learn to make great design decisions

On June 8th, I’ll be hosting the Mastering Object Oriented Design Seminar. This four-hour seminar will be laser-focused on helping you make great object oriented design decisions. Struggling with how to lay out your app? Wish you could make framework-level decisions? The Mastering Object Oriented Design Seminar is for you!

Registration opens Thursday, May 14th. The early bird cost is $179 – a discount of $50 off the full ticket price. However, I’ll be using a rolling early bird discount, meaning that for every day you wait, the price goes up! Also, due to technical limitations, there are only 45 seats available.

Want to get a sweet discount off the ticket price? Buy a copy of Mastering Object Oriented PHP by Thursday morning and you’ll receive $20 off the registration fee!