How to Deal with Badly Written Code

Sadly there is a ton of badly written code out in the wild. Hardware related code, seem to suffer more in this regards. I imagine, many developer in this segment are unwillingly to invest time in quality or are just inexperienced.

Even if you are dedicated in reliable and high quality code, you will probably run into the situation, where you have to use a library with really low standards.


There are a number of strategies to deal with this code:

  1. Rewrite the library.
  2. Refactor the code.
  3. Wrap the library.
  4. Wrap the interface.

Rewrite the Library

This is actually the best you can do and it is also the most time-consuming approach. Yet, write the code from scratch will give you several benefits:

  • You will perfectly understand how the original library and the accessed hardware works. Actually you have to understand it, otherwise you are unable to rewrite the code.
  • The new code will be modern, fast, reliable and in your coding style.
  • If you open source the new code, you will give people an alternative and a better example how to implement something the right way.
  • You can also selectively remove unwanted/bloated parts from the original code, which can reduce the overall binary size of the final project.
  • It will also give you the option to implement a proper error handling in the new code.

If you have the time and motivation to rewrite the code, do it!

Refactor the Code

Changing the code, without changing the functionality is called code refactoring. This is a good strategy, a compromise, between rewriting and wrapping. Usually you will just go, line by line, through the original code, modernise it and cleaning it up.

  • First you replace macros (constants, header guards) with modern C++ equivalents.
  • Next you reformat the whole code and make it readable.
  • You add missing API documentation.
  • Now you rename methods and variables in the code in a proper and speaking style.
  • You split large methods into smaller ones and make everything modular and nice.
  • etc.

At the end, you will have a library which is modern, readable and therefore easier to handle and use.

Wrap the Library

If you can not bear how something looks, just wrap it nicely. This is a very effective strategy for code. Because the compiler will resolve the wrapper code, there is no disadvantage for this approach.

Often there are macro definitions or other declarations (functions, operators, etc.) in the global namespace which creating troubles in you code. Your wrapper solves all this issues and provide a better API to your code.


The illustration above illustrates the basic principle. Only the wrapper code includes the API of the bad code and has to deal with the problems of it. The rest of your application will access the wrapper, and therefore using proper code.

In the hypothetical example above, you can see a simple wrapper for a single function. The header LevelMeter.hย is clean, proper C++ code. In the implementation of your wrapper, you deal will the dark side and all quirks of the badly written library.

This will not prevent linker problems if there are variables with the same name. Because you properly place your code in namespaces and avoid global variables, you are on the safe side. ๐Ÿ˜‰

Wrap the Interface

As a quick fix it is often helpful just to wrap the interface, speak include file.

This is a good solution for complex headers causing problems. Instead of including the original header, you just include the header wrapper. Your wrapper will clean-up (undef) macros after including the bad header file.

Wrapping the interface is only helpful for problems with macro definitions. Strange operators, function or class definitions will still influence your code.


Despair is no strategy. ๐Ÿ˜‰ Take one of the actions above to solve your problems. Never let badly written code influence your own sense of quality and style while writing code. Be a role model! Always show how well written code shall look like.

If you have questions, miss some information or just have any feedback, feel free to add a comment below.

Have fun!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.