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.