How to Deal with Badly Written Code

published: August 31, 2018 — last modified: October 23, 2025

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.

Strategies

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.

Wrapper

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.

https://gist.github.com/LuckyResistor/57af62aeeee54990b0c0be2ee3dde660

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.

https://gist.github.com/LuckyResistor/6705f75d46bf037655d9103b750fc694

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.

Conclusion

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!

More Posts

Write Less Code using the "auto" Keyword

Posted on 2019-07-12— C++, Improve your Code, Learn

I walk through practical ways I use C++11's auto keyword to reduce repetitive declarations, simplify range-based for loops, lambdas and register access, and make Arduino and embedded code easier to refactor. Read the full post for examples and tips you can apply to your projects.

Read this post

PCBite Kit 2.0 from Sensepeek Arrived

Posted on 2018-02-26— Common

I just received the PCBite Kit 2.0 from Sensepeek and explored its mirror board, magnetic pogo-style probes, and PCB holder. I found the gold needle tips and crown forks helpful for accessing tiny pads without test points. If you work on soldering or circuit analysis, read on for my hands-on impressions.

Read this post

Candlelight Effect

Posted on 2019-11-25— Projects

I built a compact Neopixel-based candlelight decoration with a DS3231 RTC and share the schematic, firmware, and build tips here. It's best placed behind a screen or in a jar. Read the full post for parts, assembly photos, and configuration notes to recreate it yourself.

Read this post

Logic Gates Puzzle 101

Posted on 2023-02-02— Puzzle

I created a logic-gate puzzle featuring a gated D latch: more artwork than optimized circuitry. I invite you to try decoding the hidden message—there are hints and links to gate and flip-flop basics in the post. I hope you enjoy the challenge; read the full article if you'd like to solve it.

Read this post

Flat Storage Boxes Set for 3D-Print

Posted on 2020-11-24— Projects

I've finished testing a flat storage box set for 3D printing and adjusted the label profile so they stack neatly with regular boxes. I designed them to save drawer space for rarely used parts. Read the full post for stacking details, label geometry, print settings, and download links if you'd like to try them.

Read this post

12 Time Wasting Issues in Autodesk EAGLE

Posted on 2017-03-06— Common

I use Autodesk EAGLE regularly and, while I appreciate parts of it, I ran into 12 recurring issues that waste time and hinder workflow. In this article I explain each problem and suggest improvements. If you use EAGLE, please read on — I hope it helps.

Read this post