AI-Assisted Engineering

Prompt Engineering for Developers: Programming With Natural Language

A week after ChatGPT's launch, developers are using it to explain code, write functions, draft tests and debug errors. Getting good results depends on how you ask. This practical guide covers prompt patterns that work for programming tasks, and the habits that keep AI-generated code safe to use.

By EthicalHat Research · · 3 min read
Infographic: A prompt template for programming tasks

A week ago, OpenAI released ChatGPT. Developers were among the first to discover what it can do: explain an unfamiliar codebase, write a regular expression, convert a script from Python to Go, suggest why a stack trace happened, or draft a set of unit tests. Screenshots of ChatGPT writing working code have been everywhere.

Developers are also discovering that the quality of the answer depends heavily on the quality of the question. Prompting is becoming a programming skill.

Why prompts matter

A language model produces the most plausible continuation of what you give it. If the request is vague, the answer fills gaps with the most common assumptions, which may not be yours. If the request includes your context, constraints and expectations, the answer can be remarkably specific.

Prompt patterns that work

1. Give context

State the language, framework, versions and relevant existing code. "Write a function to validate an email" gives a generic answer. "In TypeScript for Node 18, using our existing Result<T, E> type below" gives one that fits.

2. State constraints

Performance limits, allowed dependencies, style conventions, security requirements and what not to do.

3. Show examples

Inputs and expected outputs, including edge cases, communicate intent better than prose.

4. Specify the output

"Return only the function." "Include pytest tests." "Explain the approach in three bullet points first."

A complete example:

text
Context: Python 3.10 service using FastAPI and SQLAlchemy 1.4.
Task: Write a function `get_active_subscriptions(session, customer_id)` that returns
the customer's subscriptions where status == "active" and end_date is null or in the future.

Constraints:
- Use SQLAlchemy ORM queries, no raw SQL strings
- Must be efficient for customers with thousands of subscriptions (single query)
- Type hints and a docstring

Examples:
- Subscription ending yesterday -> excluded
- Subscription with end_date None -> included

Output: the function, then pytest tests using an in-memory SQLite session.

5. Iterate in conversation

Treat the first answer as a draft:

  • "What edge cases does this miss?"
  • "Rewrite it without the external dependency."
  • "Add input validation and explain each check."
  • "This fails with the error below. What's wrong?"

6. Ask for critique

Ask the model to review its own output, or your code:

text
Review the function above for bugs, security issues and performance problems.
List each issue with the line it applies to and a suggested fix. Do not rewrite
the whole function.

It will not catch everything, but it often catches something.

7. Use it to learn, not just to produce

"Explain what this regex does, piece by piece" or "Why would someone use a defaultdict here?" are some of the most valuable prompts, because they build your own understanding.

Habits that keep it safe

flowchart LR
  P[Prompt with context<br/>no secrets] --> D[Draft code]
  D --> U[Understand it<br/>ask for explanation]
  U --> T[Test it<br/>your tests + edge cases]
  T --> R[Review & scan<br/>like any PR]
  R --> M[Merge]
  • Never paste secrets, credentials or customer data into public AI tools. Be careful with proprietary code, and follow your organisation's policy.
  • Do not trust, verify. ChatGPT's knowledge ends in 2021, it can invent APIs that do not exist, and it makes confident mistakes. Run the code, test it and read it.
  • Understand before you commit. If you cannot explain the code in review, you should not merge it.
  • Watch security. Ask explicitly about injection, validation and authorisation, and still scan the result.
  • Check licences when the model produces large, distinctive blocks of code.

What changes for developers

Natural language is becoming another way to program: not a replacement for code, but a faster way to produce drafts, explore options and learn. The developers who benefit most will be those who combine clear communication (precise specifications and good examples) with the engineering discipline to verify what comes back.

The bottom line

ChatGPT and tools like it reward the same skills that make good engineers: clarity about the problem, attention to edge cases and healthy scepticism. Write prompts like you would brief a capable colleague, iterate on the answers, and treat every result as a draft that must earn its way into your codebase.

Our secure SDLC team has adopted these practices across projects, and our security training now includes safe use of AI assistants for developers.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.