Course Content
Prompt Engineering Mastery
6 sections · 32 lessons
How can prompts be used to generate code in different programming languages?
What you need to know
A code request with gaps gets the model's defaults: an old library version, a different error-handling style, a dependency your project does not allow. Filling those gaps is most of the work.
What to specify
| Item | Example |
|---|---|
| Language and version | Python 3.12, TypeScript 5 with strict |
| Frameworks and libraries | "FastAPI, Pydantic v2, standard library only otherwise" |
| Signature | def gst_breakup(amount: Decimal, rate: Decimal, inter_state: bool) -> dict |
| Behaviour and edge cases | Rounding rule, negative amounts, zero rate |
| Errors | "Raise ValueError with a clear message" |
| Conventions | Naming, logging, docstring style |
| Output | "Code block only, then 5 pytest cases" |
Tests as the specification
Giving tests first is the most reliable way to say what "correct" means, especially across languages. The model writes code until the tests would pass; you then actually run them.
Translating between languages
Ask for idiomatic code, not a line-by-line copy: Python's exceptions become Go's error returns, Java's streams become Python comprehensions. Keep the same tests in both languages to prove the behaviour matches.
What changed by 2026
Coding assistants and agents now read your repository, run the tests and fix failures themselves. Prompting shifts from "write this function" to giving good context: a repository instructions file with conventions and commands (many tools read files such as AGENTS.md), clear acceptance criteria, and tests. "Set temperature to 0 for code" is old advice — many current reasoning models do not accept temperature; use a higher effort setting for hard code instead.
A real-life example
A billing team needs a GST split function. The first prompt:
Write a function to calculate GST.They get a Python function using float, a single 18% rate, and no split between CGST/SGST and IGST. The improved prompt:
Write a Python 3.12 function: gst_breakup(amount: Decimal, rate: Decimal, inter_state: bool) -> dict- Intra-state: split tax equally into "cgst" and "sgst". Inter-state: all tax goes to "igst".- Use Decimal throughout; round each component to 2 places, ROUND_HALF_UP.- Raise ValueError for a negative amount or a rate not in ALLOWED_RATES.- Standard library only. Return the code, then pytest cases for: 1000 at 18% intra-state -> cgst 90.00, sgst 90.00 1000 at 18% inter-state -> igst 180.00 negative amount -> ValueErrorThe result uses Decimal, splits correctly and passes all generated tests. The team then asks for "the same function in TypeScript using integer paise, with the same test cases in Vitest" for the checkout page, and both versions pass identical cases.
Follow-up questions to expect
- "How do you check generated code is safe?" — Run tests and static analysis, review for injection, secrets and unsafe deserialisation, and check any new dependency exists and is maintained.
- "What is a hallucinated package?" — The model imports a library that does not exist; attackers sometimes register such names. Verify every dependency before installing.
- "How do you get code that fits the project?" — Give examples of existing code and a repository conventions file, or use an agent that reads the codebase.