Skip to content

Prompting AI to Write Better Code

Oct 7, 2026 1 min read

AI coding tools can write functions, fix bugs and explain unfamiliar code. The difference between helpful and frustrating often comes down to the prompt.

Describe the environment

Always state the language, framework, versions and any libraries you're using. "A React 18 component using TypeScript and Tailwind" avoids a lot of rewriting.

Specify inputs, outputs and edge cases

Write a TypeScript function that takes an array of orders
({id, total, createdAt}) and returns total revenue per month.
Handle empty arrays and invalid dates. Include unit tests in Vitest.

Share relevant existing code

Paste the related files or functions so the AI follows your patterns, naming and structure. Ask it to "match the style of the existing code".

Ask for a plan before code

For larger features, ask for an approach first: files to change, data flow and risks. Agree on the plan, then generate code step by step.

Review everything

  • Run the code and tests.
  • Check for security issues like unvalidated input or exposed secrets.
  • Make sure you understand what it does before merging.

Find ready-made prompts in Coding & Development.

Put it into practice

Browse hundreds of free, ready-to-copy prompts for ChatGPT, Claude, Midjourney and more.

Browse prompts
Keep reading

More articles

All articles