These code generation prompts help you write functions, classes, scripts, boilerplate, regular expressions, data transformations and small tools with ChatGPT, Claude, Gemini or Copilot. They ask for clear requirements, edge cases and tests, so the code is usable rather than a rough guess.
20 prompts on this page. Last updated: 4 December 2026. Written and tested by Saad Raza.
How to use these prompts
- Click Copy on any prompt.
- Paste it into ChatGPT, Claude, Gemini, Copilot or Perplexity.
- Replace everything in [square brackets] with your own details. The more specific you are, the better the answer.
- Read and edit the result before you use it. Add your own experience and check every fact, name and number.
Give the AI your language, version, framework and coding conventions, and paste the relevant code rather than describing it. Review, run and test every generated change before merging it, and never paste secrets, keys or private customer data into an AI tool.
Writing code
1. Write a function from a specification
Write a [language] function called [name] that [does what]. Inputs: [types]. Output: [type]. Handle these edge cases: [list]. Follow [style guide], add a docstring and include unit tests.
2. Write a class or module
Write a [language] class for [purpose] with these methods: [list]. Use dependency injection for [external services], type hints and clear error handling.
3. Write a script to automate a task
Write a [Python or Bash] script that [task, e.g. renames files, processes CSVs or calls an API]. Accept arguments from the command line, log progress, handle errors and include usage instructions.
4. Generate boilerplate
Generate the boilerplate for a new [framework] project for [purpose] with [features], including folder structure, configuration, linting and a sample test.
5. Write a regular expression
Write a regex that matches [pattern] and does not match [counter-examples]. Explain each part and give test cases.
6. Transform data
Write [language] code to convert this input data into this output format: [paste input sample] to [paste output sample]. Handle missing and malformed values.
7. Write a CLI tool
Write a command-line tool in [language] that [does what], with subcommands [list], help text and exit codes.
8. Write code from pseudocode
Convert this pseudocode into idiomatic [language]: [paste].
9. Translate code between languages
Translate this [source language] code into idiomatic [target language], keeping behaviour identical and using the target language's standard libraries: [paste].
Working with existing code
10. Add a feature to existing code
Here is my existing code: [paste]. Add [feature] in the same style, changing as little as possible, and explain each change.
11. Write code that follows my patterns
Here are examples of how we write [services, components or handlers] in our codebase: [paste]. Write a new one for [purpose] following the same patterns.
12. Integrate a third-party API
Write code to integrate [API] in [language] for [use case]: authentication, requests, pagination, rate limiting, retries and error handling. Use environment variables for keys.
13. Write a data model
Design data models or classes for [domain] with these entities and relations: [describe], including validation rules.
Quality
14. Ask clarifying questions first
Before writing any code for [task], ask me the questions you need answered about requirements, constraints and edge cases. Then wait for my answers.
15. Write code with tests first
Write failing unit tests for [function behaviour] first, then the implementation that makes them pass, in [language and test framework].
16. Make code production-ready
Take this prototype code and make it production-ready: input validation, error handling, logging, configuration, tests and documentation: [paste].
17. Generate code with security in mind
Write [feature, e.g. a file upload handler or login endpoint] in [framework], following OWASP guidance on validation, authentication, injection and secrets.
18. Optimise an algorithm
This function is slow for large inputs: [paste]. Explain its time and space complexity and write a faster version with the same results.
19. Write a small utility library
Write a small, dependency-free utility module in [language] for [purpose, e.g. date formatting or slugs], with tests and examples.
20. Explain generated code
Explain this code line by line, including any assumptions, risks and how to test it: [paste].
What are code generation prompts?
Code generation prompts help developers write functions, classes, scripts, boilerplate, regular expressions, data transformations and API integrations with AI coding assistants. The best coding prompts for ChatGPT, Claude and GitHub Copilot give clear requirements, the language and version, edge cases and examples from your codebase, and ask for tests alongside the code.
How to write better code generation prompts
- Specify inputs, outputs and edge cases: vague requests produce fragile code.
- Show your patterns: paste an example so new code matches your codebase.
- Ask questions first: let the AI clarify requirements before writing.
- Always run tests: never merge code you have not run.
Best AI tools to use with these prompts
Popular AI coding tools include GitHub Copilot, Cursor, Claude Code, ChatGPT, Claude and Gemini Code Assist. Editor-integrated tools see more of your codebase; chat tools are handy for focused tasks and explanations.
Frequently asked questions
Is AI-generated code safe to use?
It can be, after review. Read it, run it, test it and check for security issues and licence concerns before using it in production.
How do I get better code from AI?
Give clear requirements, your language and versions, examples from your codebase, edge cases and ask for tests. Smaller, focused requests work better than huge ones.
Which AI is best for coding?
ChatGPT, Claude, Gemini and GitHub Copilot all help with code. The best choice depends on your editor, language and the size of your codebase.
What is the best AI for coding?
It depends on your language, editor and project size. GitHub Copilot, Cursor and Claude Code work inside your codebase, while ChatGPT and Claude are useful for focused questions.
How do I write a good coding prompt?
State the language and version, the exact behaviour, inputs and outputs, edge cases, constraints and the style to follow, and ask for tests.
More software development prompts
- Debugging PromptsFind and fix bugs, errors and performance problems.
- Code Review and Refactoring PromptsReview, clean up and improve existing code.
- Software Testing PromptsUnit, integration and end-to-end tests.
- Technical Documentation PromptsREADMEs, API docs, comments and guides.
- SQL and Database PromptsQueries, schema design, indexes and optimisation.
- API Development PromptsDesign, build, secure and document APIs.
- DevOps PromptsCI/CD, Docker, cloud, monitoring and deployment.
- System Design and Architecture PromptsArchitecture, scaling, trade-offs and design reviews.
Back to all software development prompts or the full prompt library, including SEO prompts.
Building a product? Your marketing site needs SEO too. Request a free audit and I will show you what to fix first.
Request received
Thank you! Saad will contact you within the next 24 hours.
Keep an eye on your phone and inbox.
