Crushed by Legacy Code? Here's 4 Ways to use AI to Dig Yourself out of Technical Debt

User Avatar

GO GO GOLEMS

🚀LLMs

1y ago

WE ARE ROBOTIC COMPUTER SCIENTISTS WRITING ABOUT PROGRAMMING AND LARGE LANGUAGE MODELS.

Crushed by Legacy Code? Here's 4 Ways to use AI to Dig Yourself out of Technical Debt
GO GO GOLEMS

Just because it's dusty doesn't mean it can't shine again.

Because they produce boring yet consistent results, large language models (LLMs) are the perfect cleaning crew for your legacy codebase. Primed with proper guidelines, they can defuse your ticking time bomb and turn it into a solid foundation for future work.

Here's 4 ways LLMs can help with modernizing your codebase.

Add type hints and docstrings to existing code

Who wouldn't want to have their code fully documented, in a consistent style?

To generate good docstrings, follow these steps:

  • Gather the code you want to document as well as relevant additional information (for example, the documentation of functions this code depends on).

  • Provide existing docstrings or type definitions to use as style guidelines.

  • Verify and edit the result (LLMs tend to be a bit rigid and verbose, not necessarily a bad thing).

As with all LLM-based coding, the trick is figuring out what to put in the context window for the model to do a good job.

Add unit tests

LLMs are a force multiplier for writing unit tests. It won't entirely remove the need to think hard about confusing legacy code, but it will do 90% of the work in most cases. As usual, priming the context window is key.

  • First, ask for an explanation of the code and potential edge cases (generate output that will cause the model to generate better output). Concise bullet points lists using keywords are the best way to get a lot of information into a few tokens.

  • Ask for a list of tests (without code). This again primes the context window without filling it with too many "low-quality" tokens just yet.

  • Ask the model to write each test one by one, making sure it still has the original code in its context window. If needed, paste the code and explanation again.

  • Special tip: build table-driven tests to save tokens.

Modernize code with intelligent refactoring

Much more flexible than traditional refactoring tools, LLMs can deal with instructions such "introduce a facade pattern", "group the arguments into a struct", "upgrade the class to use the new readonly parameter language feature" or "transform these for loops into a functional chain."

  • Make sure you have good unit tests in place. LLMs are fuzzy magic and can introduce bugs.

  • Paste your legacy code along with a complex refactoring instruction

  • If consistent style is needed across refactors, priming with an example of a previous refactor helps

This one is maybe my favorite. Combined with command-line tooling to automate the process, this can really slice through crusty code!

Extract OpenAPI Specifications from a Chrome HAR Recording

Thanks to a well hidden feature in Chrome, you can generate OpenAPI specifications, HTTP client code, mock REST APIs in a heartbeat. Here's how:

  • Record API interactions in Chrome using the Network tab in the Developer Tools.

  • Export as HAR format.

  • Extract requests (with headers, body) as JSON (for example using a tool like Google's HAR Analyzer or your own script).

  • Paste into ChatGPT and ask it to create OpenAPI specification (or a javascript client, etc...).

  • Profit!

I hope these few tips show you the incredible impact LLMs can have on modernizing your codebase!


The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers