3 Ways to Improve Existing Software

There appear to be plenty of articles about the best ways to start new software development projects. Everything from choosing a language, framework, revision control system, or design pattern has been covered in lucid detail hundreds of times over.

But what about those of us who find ourselves starting with existing software? I spend a lot of my time working with code that was written some time in the past and is in various states of repair. Some of it was written by others (what were they thinking?); some of it I wrote (what was I thinking?).  So, what can we do to improve the quality of our software?

Though far from being a complete list, the following three items have helped me a lot along the way.

Write automated tests

Tests are a developer’s best friend. Well, maybe a close second to caffeine, but they are an important foundation of writing good software. Test automation is well-documented and simple in most languages, and is well worth the effort.

It saves time. You are going to test the changes you make anyway, whether you do it manually or through some form of automation. Setting up the automated tests once will let you repeat them quickly whenever you desire.

You’ll be more likely to catch problems caused by future development. Almost like Einstein’s “spooky action at a distance”, making a change in one part of an application can sometimes evoke a reaction in a part you thought was completely unrelated. This is especially true when working in unfamiliar code. Having the tests automated will make it easier to run a quick check of all parts of the application, which you may not take the time to do otherwise.

Refactor the ugly

If well-written code is like poetry, then ugly, poorly-written code is like amateur graffiti. “Quick, let’s see how much crap we can paint up on that wall!” Nobody likes graffiti — especially not amateur graffiti. It makes people wince in passing and it makes building owners downright grumpy.

As the owner of your building, your code, you owe it to yourself to make it pretty. Everyone else who has to see it will thank you. Additionally, it will help you write better code in the future and make the existing code easier to maintain.

Especially target code that is confusing, uncommented, or just too cleaver for its own good. Consider using automated tools to format and check your code: perltidy and Perl::Critic are excellent tools for Perl. Refactor methods into chunks small enough that they are easy to wrap your brain around. I could go on, but this should be enough to get started.

Increase code reuse

If you’ve written the same thing in more than one place, you might be wasting your time. Three times and I’ll call it a sure thing. Repeated code can also lead to a maintenance nightmare — ever miss updating one of the five places you define rules for usernames? I see two ways to do it.

First, see if there is a library or module that already does what you’re trying to do. You can effectively pare down the code you have to support, and conventional wisdom says the more people who use a chunk of code the fewer bugs it is likely to have. It will also get you outside your own codebase and help you keep in touch with what is possible. I love CPAN.

Second, reuse your own code. Create your own sharable libraries, add tests, and market them to your team. Be a catalyst for improvement and share what you’re learning along the way.

It’s not difficult to get started, and trust me, you’re not alone.

CPAN Nuggets: Debugging Tools

Just thought I’d take a moment to share a couple of CPAN modules I love using for development and debugging my Perl web applications. They are not restricted to web application development, so give them a try!

DBIx::Profile

This module is a godsend when trying to figure out what is going on in DBI land. I typically include it in my Apache config: PerlModule DBIx::Profile, restart the server, and watch the log while I hit it. It shows every SQL statement as it is being run, along with the time it took for each statement to run and how many times it called each of the prepare, execute, and fetch operations. Priceless!

Carp::Always

This little gem was brought to my attention a couple of weeks ago by Andy Lester when he made it out to our Grand Rapids Perl Mongers meeting (thanks, Andy!). I had seen a slick one-liner previously that, by including in your code, would force a stack trace if anything died:

use Carp; $SIG{__DIE__} = &Carp::confess;

Carp::Always makes it even easier — just use it and you’re done. Score one for another quick, easy tool from CPAN!

Design Changes

Truth is I’ll probably always be playing around with the styles on here.  I just wasn’t satisfied with the original design I came up with — I wanted something simpler that put as much focus on the content as possible.  It doesn’t have a lot of color and I moved all of the sidebar content to the bottom to remove distractions.  Personally, I like it, but I wouldn’t mind some feedback.  My one big concern is that the links to the categories, search, etc. are too far out of the way… we’ll see.

So, enjoy.  And let me know what you think in the comments!

Andy Lester @ Grand Rapids Perl Mongers

Andy Lester will be making the trip up from Chicago this month for our Grand Rapids Perl Mongers meeting. He will be presenting, “Things you may not know about Perl”, so join us if you can.

Note the time is different than usual: it is going to be Friday evening from 5:30 - 6:30. I guarantee you won’t want to miss it!

Noteworthy link: High Scalability

High Scalability website

If you’ve ever been curious about what lies beneath the biggest sites on the web you owe it to yourself to check out the Real Life Architectures section. I was surprised how many of them listed PHP in their technology stack.