MemberPress AI Academy memberpress.ai
← Prompt Engineering 101

Lesson 16 of 19

Advanced · Production-Grade Prompts

Industry use case - coding

Project25 min

Project: Refactor a function and write tests

You have a poorly written Python function. It works, but it has no type hints, no docstring, inconsistent naming, and no tests. Your job is to build a prompt that produces a clean, tested version — not just cleaner code, but code you would actually merge.

What this project tests

Code generation is one of the highest-stakes uses of Claude. A wrong answer in a legal clause gets flagged in review. A wrong answer in code may ship. This project tests whether you can write a prompt specific enough that the output requires minimal review.

The function to refactor

Use this as your starting point (or substitute one of your own):

def calc(items, d):
    t = 0
    for i in items:
        if i['qty'] > 0:
            p = i['price'] * i['qty']
            if d:
                p = p * (1 - d)
            t += p
    return t

Building the refactor prompt

Start with the role: "You are a senior Python engineer reviewing a pull request."

Add the task: "Refactor the following function to meet the requirements below."

List the requirements explicitly:

  • Add type hints to all parameters and the return value
  • Add a docstring following the Google docstring format
  • Rename variables to be descriptive (no single-letter names)
  • Add input validation: raise ValueError if items is empty, raise ValueError if discount is not between 0 and 1
  • Do not change the logic or the return value

Add the code in delimiters:

<code>
[function here]
</code>

Add the format constraint: "Return only the refactored function. No explanation."

Building the test prompt

After the refactor, run a second prompt: "Write three pytest unit tests for this function. Cover: the normal case (a list of items with a discount), the edge case (an empty list should raise ValueError), and the boundary case (a discount of exactly 0 and a discount of exactly 1)."

Checking the output

Copy the refactored function and the tests into a Python file and run pytest. If the tests pass, the prompt worked. If they fail, the error message tells you what went wrong — use it to refine the prompt.

If you get stuck

If the refactored function changes the logic, the refactor prompt needs a stronger constraint: "The function must produce the same output as the original for all valid inputs. Do not change the logic."

If the tests fail because the function raises an error the test did not expect, the test prompt needs to be more specific about the expected exception type and message.

Deliverable

The refactored function, the three tests, and the output of pytest showing all tests passing. If you cannot run Python locally, use Claude: paste the function and tests and ask Claude to simulate the test output.

Try these prompts

  • Refactor this Python function with type hints and docstrings.
  • Write 3 unit tests for a function that parses CSV dates.
  • Explain what this code does and identify potential bugs.