5 Tips to Prevent Code Rot

March 20, 2018

Building a future-proof application is hard. Web technologies and browsers evolve fast, and with evolution comes challenges. Software rot is nothing new and maintaining software is hard. Here are a few advices to prevent your apps from becoming unstable and unusable over time.


1. Choose frameworks and libraries wisely

While you might be inclined to use a trendy new framework that got released a week ago, don't do it. Instead, choose dependencies that have a solid history.

Thanks to Open Source and Github, it is fairly to understand which frameworks and libraries have been around for a while and are actively maintained.

2. Setup automated testing & CI

It's 2018! Unit testing is a must. Not only it will help you identify problems early but it will also give you the confidence to perform refactoring. Thanks to unit testing, you will be able to simplify and re-organize your code while knowing your component / module still works correctly.

“Code without tests is bad code. It doesn't matter how well written it is; it doesn't matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don't know if our code is getting better or worse.” ― Michael C. Feathers

Without the fear of breaking things elsewhere, most developers will naturally want to improve or maintain their code over time.

3. Refactor the code

Most commons goals of code refactoring includes:

  • Reduce complexity (simplify, remove duplicates, etc)
  • Improve performance and stability
  • Improve readability of the code

Get rid of the components of your project are no longer necessary. Technology and browsers evolves fast, and supporting old behaviors costs time and money.

4. Update your dependencies

Knowing when should dependencies be updated is tough. Why should you update your dependencies if you application works? Sometimes, it's simply required to prevent your code from breaking. For instance, one of your dependency has been updated because a browser API specifications has changed.

You should also update your dependencies to take advantage of the new features or better performance that the latest version offers. Thanks to dependencies manager (Yarn, NPM, Composer) it's extremely easy to update a dependency. And with automated testing and continuous integration, you will be the first to know if you screwed anything up.

Update early, updated often! If you wait too long, chances are it will be a hassle to update all your dependencies because they will be many breaking changes. Instead, you should be aware whenever once of your dependency gets updated.

If you're using Yarn/NPM, you want to take a look at https://greenkeeper.io/

5. Document your code

Technical documentation is extremely valuable, especially when several developers are collaborating on the same project (extremely common on Open Source projects).

The common saying in the software industry “Good code is self-documenting” is a bit of a cliché. Adding documentation directly in the code (as comments) will help anyone who will use your source code but don't want to read through it.

To write documentation comments, ensure that you choose a popular standard like JSDoc for JavaScript, or PHPDoc for PHP.


Conclusion

“Code rot is like weeds in the garden – the earlier the problem is dealt with, the easier it is to overcome. The longer you leave it, the harder it is to fix.” ― Henrik Warne

Software maintenance is hard but it is required to prevent code rot. To improve code integrity, regular maintenance is a must. The longer you wait, the harder it will be to fix.

But thanks to dependency managers, continous integration (CI), and tools like DependenCI or Greenkeeper, preventing code rot has become much easier.