The Code Abides logo The Code Abides
Illustration of a developer writing prompts on a glowing screen surrounded by AI code patterns in warm amber light

Prompt like a Developer: How to get better code from ChatGPT

If you want great code from ChatGPT, you have to talk to it like a developer. That means clear goals, the right context, realistic constraints, and a tidy feedback loop. When you do this, ChatGPT stops feeling like a toy and starts feeling like a reliable partner.

This guide shows you simple, repeatable ways to write prompts that ship real features. You will see why certain prompts work, how to structure them, and exactly what to say in common situations like debugging, refactoring, test writing, and performance work.

Why prompts matter more than you think

Large models do not read minds. They make the best guess based on the text you send. Vague asks lead to vague code. Sharp asks lead to sharp code. Treat your prompt like a mini spec. The quality of that mini spec sets the ceiling for the quality of the result.

The anatomy of a high quality coding prompt

Use this structure and you will notice an immediate boost. 1. Role and context Tell ChatGPT who it is helping and where the code will live. Mention framework, language, versions, and style guides if you have them. 2. Goal One sentence that states the outcome in plain language. Think user story or acceptance criteria. 3. Constraints State limits up front. Mention browsers, memory, performance, security, or package rules. 4. Source material Paste only the parts that matter. If you paste a file, say which lines are relevant. 5. Output format Tell it exactly what to return. Code only, code and comments, a diff, or a plan first. 6. Review loop Ask for a short summary of what changed and why. That makes commits and PRs easy.

A reusable master prompt

Copy this and adapt it for any task.

Act as a senior engineer helping me ship a feature.
Project stack and context: React with Astro and Tailwind, Node, Vercel.
Goal: add a responsive hero section with a single call to action that links to the blog.
Constraints: do not add new packages, support Safari and Chrome, pass Lighthouse on mobile with a score above ninety.
Source: I will paste the current component.
Output: propose a short plan, then provide the updated component. Include comments that explain key choices. End with a one sentence summary for the commit message.

Bad prompt vs good prompt

Bad

Make the page responsive

Why it fails: no stack, no goal, no constraints, no output format.

Good

You are helping with a React and Tailwind landing page in an Astro project. Goal: make the hero responsive from three hundred seventy five pixels to large desktop. Keep one call to action that links to the blog. Do not add packages. Keep CSS small. Output the updated component only with comments. Add a one sentence summary for the commit.

Why it works: clear stack, clear outcome, hard limits, clear output.

Prompts for common jobs

Debug a failing function

Use this when you can reproduce the bug.

I have a repeatable bug. The function below throws when the input array contains null. Explain the most likely root cause, show how to reproduce with a small example, then fix it. Keep the function signature the same. Add a short test plan at the end.
Code:
[paste only the function, not the entire file]

Why it works: asks for explanation, reproduction, fix, and a test plan. You learn and you ship.

Refactor for clarity

Use this when the code works but feels heavy.

Refactor this module for readability and small functions. Keep behavior the same. Prefer early returns, descriptive names, and short comments that explain intent. Provide a one paragraph summary of the changes.
Code:
[paste module]

Write tests before features

Use this when you are about to add behavior.

Create a table of test cases for the function I describe next. Cover normal cases and edge cases. Then write Jest tests based on that table. After the tests, propose the minimal code to make them pass.
Function: calculates BMI from height and weight, returns a category string, rejects invalid input.

Add performance awareness

Use this when a function feels slow.

Review the function for time and space costs in realistic cases. Suggest the smallest safe improvement that keeps behavior the same. Explain the tradeoff in one paragraph. Provide a micro benchmark plan I can run locally.

Generate documentation that people will read

Use this when you want a human friendly summary.

Write a short readme section for this component. Include purpose, props, failure modes, and one example of use. Keep it under two hundred words and write for a new teammate.

How to give the right context

ChatGPT does not see your file tree unless you show it. Give it just enough to anchor the request. 1. Name the stack Framework, language, version, hosting. Example: React with Astro and Tailwind on Vercel. 2. Name the file and path Example: src/components/Hero.astro. 3. Paste only what matters If the bug is in one function, do not send the whole project. If imports matter, include them. 4. Describe the shape around the code Example: this component receives props title and ctaUrl from the parent layout.

The constraint checklist

Constraints are your steering wheel. They reduce drift and keep the solution practical. 1. No new dependencies 2. Keep bundle growth under a chosen size 3. Browser support list 4. Accessibility expectations, such as keyboard and screen reader flow 5. Security notes, such as never echo secrets to logs 6. Style rules, such as follow the existing class naming pattern 7. Performance target, such as render under one hundred milliseconds for a given case

The secret power of step by step prompts

When a task is complex, split it and drive. Think plan then build.

Bad

Build a full blog system

Better

Step one: propose a plan for a minimal blog with Astro, a blog index page, a single post page, and related posts based on tags.
Step two: provide the index page.
Step three: provide the single post page.
Step four: add a helper that computes related posts.
Stop after each step and wait for review.

This pattern keeps quality high and lets you make course corrections early.

How to handle long files

If the file is long, send a slice and give a pointer.

The bug is in Hero.astro, lines forty to one hundred ten. The rest is fine. Here is that slice. Please explain the bug and propose a fix that keeps the public API the same.

This saves tokens and directs attention where it belongs.

Turn comments into code

Write comments that describe intent, then ask ChatGPT to fill the blanks.

Replace each comment with working code. Leave all other lines unchanged.
// validate input
// fetch data
// map to view model
// return result

This is fast and keeps you in control.

Build a feedback loop

Treat each answer as a draft. Give tiny nudges.

Good nudge

Keep the function names and props exactly as in the current code. Remove any new dependencies. Try again.

Good nudge

The solution works in Chrome but fails in Safari fourteen. Use a different API that has wider support.

Short, specific feedback beats long essays every time.

Prompt templates you can save

Debug explainer

Explain why the code throws, show a minimal reproduction, then fix it. Keep the signature the same. End with a short test plan and a one sentence summary for the commit.

Refactor plan

Refactor for clarity without changing behavior. Prefer early returns and small functions. Add comments that explain intent. Provide a short summary and a diff style view of the changes.

Test first

Propose a table of cases. Write Jest tests from that table. Then write the minimal code to pass the tests with comments. End with a command I can run to execute only these tests.

Code review

Review the module as a senior engineer. List the three most important risks. Propose one safe improvement per risk. Keep the review under two hundred words.

Performance pass

Point out the hot path. Suggest one change that improves time or memory while keeping behavior the same. Provide a small benchmark plan and an example input.

Common mistakes and how to fix them

  1. You ask for everything at once Fix: ask for a plan first, then build in steps.
  2. You paste the whole project Fix: paste only the part that matters and describe the rest in one sentence.
  3. You forget constraints Fix: set rules at the top. No new packages. Browser list. Bundle size.
  4. You accept the first answer Fix: request a revision with one sentence. You will be amazed by the jump in quality.
  5. You skip a final summary Fix: always ask for a commit message or two line summary. Future you will be grateful.

SEO friendly questions this post answers

How to write better prompts for coding with ChatGPT. How to get ChatGPT to write usable code. How to debug with ChatGPT. How to prompt ChatGPT for tests. How to improve code quality with ChatGPT.

Final thoughts

Great prompts are not magic. They are clear thinking written down. When you give ChatGPT a role, a goal, constraints, and a short feedback loop, you turn it into a patient partner that helps you ship with confidence. Practice with the templates above, adjust them to your stack, and keep a small prompt library in your repo. Week by week you will spend less time wrestling with syntax and more time directing the work that matters.

Related Posts