Asking ChatGPT to “write a function” works for snippets, breaks for real codebases. This workflow scales to actual files-and-tests-and-context work.
Who this is for
Developers who use ChatGPT as their main AI coding assistant (no Cursor / Claude Code), or who use it for languages and frameworks their main assistant struggles with.
When to reach for it
Greenfield prototypes, small utility scripts, learning a new library, debugging an isolated function, generating tests for existing code.
When this is NOT the right tool
Multi-file refactors, large legacy codebases, anything requiring directly editing files (use Cursor or Claude Code instead).
Step by step
- Open one chat per task, not per project. Long mixed-topic chats degrade fast.
- Paste the existing code first, then the goal. Order matters — ChatGPT anchors to whatever it sees first.
- State constraints explicitly: language, version, framework, what libs you can / cannot use, and the test command.
- Ask for a plan before code: “List the files you would touch and why, before writing any code.”
- Generate code in small chunks (one function, one component), not whole files. Easier to review and re-prompt.
- For each chunk, ask: “What edge cases does this not handle?” Then ask it to add them.
- Always run the code locally before accepting it. If it fails, paste the exact error back and ask for the minimal fix.
Recommended workflow
Adding a date-parsing utility: paste existing utils file → state “Add a parseLooseDate(str) that handles formats X/Y/Z, return Date or null” → ask for plan → generate function → ask for edge cases → add tests → run.
Common mistakes
- Pasting a 2000-line file and saying “find the bug”. ChatGPT will pattern-match, not actually reason.
- Skipping the plan step and going straight to code, then re-prompting endlessly to fix what was misdesigned.
- Not stating the exact framework version — getting React class components when you use hooks, or Python 2 syntax in a 3 codebase.
- Trusting unit tests it wrote — they often hallucinate APIs. Run them.
Advanced tips
- For new libraries, give ChatGPT a paste of the actual library README or docs page. Way fewer hallucinated APIs.
- When debugging, include the FULL error message and stack trace, not a paraphrase.
- Set a Custom Instruction:
When writing code, default to {your language/style}. State assumptions before writing.Saves you re-typing every chat.
Copy-ready prompt
Language: {lang} {version}
Framework: {framework} {version}
File context: {paste file contents here}
Goal: {one sentence}
Constraints: {libs allowed, perf, style}
First, list the changes you would make and why. Wait for me to confirm before writing code.
FAQ
- Should I use ChatGPT or Claude for coding?: For chat-based coding, both are strong. Claude tends to be better at long context. For agentic file editing, use Cursor or Claude Code.
- Why does it keep hallucinating APIs?: It does not see your actual installed package versions. Either paste the relevant docs, or pin the version in your prompt.
Related
Tags: #ChatGPT #Tutorial #AI coding #Workflow