Introduction: Keeping Code Working Over Time

Imagine you have a piece of code that does a specific job for you — maybe it parses some data, generates a report, or automates a task. Over time, you will likely need to update this code. New requirements might come up, bugs may appear, or you might want to improve how the code works. This is a regular part of software development, and it’s called code maintenance.

Large Language Models (LLMs) can help you maintain and develop your code more efficiently. In this lesson, I will show you three practical strategies for using LLMs to update and improve existing code. Each strategy has its strengths and limitations, and I will walk you through how to use them with clear examples.

Strategy 1: Full Code Replacement

The first and most direct strategy is to provide the LLM with your entire code and ask it to return a fully updated version. This simple approach works well for small scripts or for making straightforward changes.

Let’s look at an example. Imagine you have a code that parses your data, and you want to update the generated report data. Here is a prompt:

Explanation:

  • You start by pasting your code so the LLM has all the context.
  • You clearly state what you want: a better-structured report, specifically as a list of bullet points.
  • You ask for the full updated code, not just a snippet.

This method is fast and can work well for small to medium-sized scripts. However, the LLM’s consistency and accuracy can drop as your code gets larger. Remember, LLMs are limited to how much code they can process at once, and they might miss details or introduce new issues in bigger files.

Strategy 2: Building Context with the LLM

When your code is too large or complex for the LLM to handle all at once, you can use the generated knowledge approach to help the LLM build context before making changes.

Step 1: Ask the LLM to explain your code.

Explanation:

  • By asking the LLM to explain your code, you help it “understand” what the code does.
  • The LLM will summarize the logic, which can also help you spot any issues or areas for improvement.

Step 2: Now, ask for the changes you want.

Explanation:

  • After the LLM has explained the code, it has a better context for making changes.
  • This two-step process can lead to more accurate and relevant updates, especially for larger codebases.

However, even with this approach, you can still hit the LLM’s context window limit if your application is very large. In those cases, you may need to break your code into smaller parts or use the following strategy.

Strategy 3: Guided Code Updates

If your codebase is too large to update all at once, you can ask the LLM to guide you through the step-by-step update process.

Here’s how you can do it:

Explanation:

  • Instead of asking for the full updated code, you ask the LLM to break down the process into steps.
  • The LLM will list the changes you need to make, provide code snippets for each step, and explain why each change is required.
  • This approach is especially useful for large projects or when you want to understand the reasoning behind each change.
  • It also helps you learn and build your own coding skills since you’ll be making the changes yourself with the LLM’s guidance.
Summary And What’s Next

In this lesson, you learned three strategies for maintaining and developing existing code with the help of LLMs:

  • Full Code Replacement: Best for small scripts or simple changes.
  • Building Context: Useful for larger codebases, where you help the LLM understand your code before making changes.
  • Guided Code Updates: Ideal for very large projects or when you want to learn and understand each change.

Each method has its place, and you can choose the one that fits your situation best. In the next set of practice exercises, you’ll get hands-on experience using these strategies to update and improve code with the help of an LLM.

Farewell and Next Steps

Congratulations on reaching the last unit of the "Prompt Engineering for Software Development" course path! You’ve explored a range of prompting approaches, advanced techniques, and real-world problem-solving with LLMs. You’ve also learned how to use LLMs as coding partners to tackle everyday development tasks and maintain your codebase efficiently.

As you move forward, remember that prompt engineering is a skill you’ll keep refining as LLMs evolve and new tools emerge. Keep experimenting with different strategies, stay curious, and don’t hesitate to revisit these lessons as you encounter new challenges in your work.

Thank you for joining this journey. Wishing you continued success as you build, maintain, and innovate with the help of LLMs!

Just one last practice session waits for you to complete it. Let's go for it!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal