How to Use AI for Coding Safely and Effectively

Brief the AI like a colleague, then check its work like a reviewer. Prompts for debugging, new code, tests and reviews, plus the checks that matter.

6 min readUpdated

Glass blocks connected by glowing lines with small gears

AI chat is genuinely useful for programming: explaining unfamiliar code, finding the cause of an error, writing boilerplate, drafting tests and teaching you a new language or library. It is also confidently wrong often enough that you should treat every answer like a pull request from a fast but careless colleague. This guide shows how to ask well and how to check what you get back.

What AI chat is good at in coding

  • Explaining code: "walk me through this function line by line".
  • Debugging: reading an error message and stack trace and suggesting likely causes.
  • Small, well-defined functions: parsing dates, validating input, transforming data.
  • Boilerplate: config files, command-line argument parsing, basic API handlers.
  • Tests: drafting unit tests, including edge cases you forgot.
  • Regex, SQL and shell one-liners, with an explanation of each part.
  • Learning: "I know Python; show me how this looks in Go and explain the differences".
  • Code review: spotting readability problems, missing error handling and obvious security issues.

It is weaker at big design decisions that depend on your whole codebase, at very new library versions it may not know about, and at anything it cannot see. In a chat like ChatG, the AI only sees what you paste. It cannot open your files or run your code.

How to ask for a bug fix

Most weak coding answers come from missing context. A good debugging prompt includes five things: the language and versions, the relevant code, the full error, what you expected versus what happened, and what you already tried.

Weak:
My code doesn't work, fix it.
[200 lines pasted]
Strong:
Python 3.12, pandas 2.x. This function should return the average order
value per customer, but it raises KeyError: 'customer_id'.

Code:
[paste only the function and a 5-row sample of the data]

Full traceback:
[paste]

I checked that the CSV has a customer_id column. What are the likely
causes? Explain the fix before showing the corrected code.

Cut your code down to the smallest piece that still shows the problem. That often reveals the bug by itself, and it keeps the important lines in focus. You can open a debugging prompt in the chat and fill in the blanks.

How to ask for new code

Write a mini specification. The more precisely you describe inputs, outputs and edge cases, the less you have to fix later.

Write a TypeScript function slugify(title: string): string.
- Lowercase, ASCII only; transliterate common accented letters
  (for example, é becomes e).
- Replace any run of non-alphanumeric characters with one hyphen.
- Trim hyphens from both ends; max length 60, never cut mid-word.
- No external libraries.
Then write 8 unit tests with Vitest, including an empty string and
input that contains emoji.

Ask for tests together with the code. If the AI's tests and its code disagree, you have found a problem before it reaches production.

Say what you do not want: "no new dependencies", "keep the existing function signature", "standard library only", "do not change anything outside this function". Without limits, AI tends to rewrite more than you asked for.

Verify everything before you ship

AI-generated code can look clean and still be wrong. Build these checks into your routine:

  1. Run it. Never put code into a project without running it and its tests.
  2. Check that every function and option exists. AI sometimes invents methods, parameters or config keys, or uses ones from an older version. Look them up in the official documentation for the version you actually use.
  3. Check every package name before installing it. Security researchers have documented AI assistants recommending packages that do not exist, and attackers can register such names with malicious code (an attack sometimes called "slopsquatting"). Confirm that a package is the real, maintained one on the official registry before you install it.
  4. Review for security. Look for SQL built by string concatenation, missing input validation, disabled certificate checks, secrets in code and overly broad permissions. Ask the AI directly: "Review this for security issues, assuming every input is hostile."
  5. Read it until you understand it. If you cannot explain what a block does, ask, or do not merge it. You are the one who will maintain it.

Keep secrets and private code out of the chat

Never paste API keys, passwords, tokens, private keys or connection strings into any AI chat. Replace them with placeholders such as YOUR_API_KEY. If you accidentally expose a real secret anywhere outside your own systems, rotate it.

If you write code for an employer or client, check their policy before pasting their code into any external AI service. Many companies allow only approved tools for proprietary code. When in doubt, reduce the problem to a generic example that reveals nothing about the project. Our AI privacy and safety guide covers this in more detail.

Useful prompts for developers

Explain this code to a developer who knows [language] but not this
library. Go block by block, then list anything that looks risky or
outdated.
[paste code]
Review this diff like a senior engineer. Group your comments into bugs,
security, performance and readability. Be specific and quote the lines
you mean.
[paste diff]
Write a regular expression that matches [description]. Explain each part,
then give 5 strings that should match and 5 that should not.
I know Python well and I am learning Rust. Show me how to [task] in Rust,
and explain ownership and borrowing as if I have never seen them.

There are more in the coding prompts collection. For quick one-off jobs, the code assistant tool takes a description or a snippet and returns explained code, and the built-in coding assistant keeps a developer-focused style for a whole conversation.

Common mistakes

  • Pasting the whole project. Long pastes bury the important part and can exceed what the AI can process at once. Send the relevant files or functions only.
  • Accepting a "fix" that hides the symptom. Wrapping everything in a try/except block makes the error message disappear, not the bug. Ask why the error happens.
  • Going round in circles. If two or three attempts fail, stop and change the approach: more context, a smaller example, or a fresh chat with a clear summary of the problem.
  • Not stating versions. Framework APIs change between major versions, so say which one you use.
  • Ignoring licences. If the AI reproduces a recognisable chunk of someone else's code, check its licence before you use it.

See also how developers use ChatG and the general guide to writing prompts.

A workflow that keeps you in control

  1. Describe the goal first, without code. Ask for two or three possible approaches and their trade-offs before any code is written.
  2. Pick one approach and ask for a small first step. One function or one file at a time is easier to check than a whole feature.
  3. Run it and report back. Paste the real output or error, not "it doesn't work".
  4. Ask for tests, then a review. "What edge cases does this miss?" often finds the next bug.
  5. Write the commit message yourself. If you cannot summarise what changed and why, you do not understand the change well enough yet.

FAQ

Can AI replace learning to code?

No. It speeds up people who can read and judge code. If you cannot tell whether an answer is right, you cannot use it safely. Use AI to learn faster: ask why, not just what.

Which programming languages does it handle?

AI chat generally handles widely used languages such as Python, JavaScript, TypeScript, Java, C#, C++, Go, PHP, SQL and shell scripting well, and rarer languages less reliably. Quality also drops for very new libraries and versions.

Can ChatG run my code or see my repository?

No. ChatG is a chat: it only sees what you paste into it, and it does not execute code. Run and test everything in your own environment.

Is AI-generated code safe to use commercially?

Treat it like code from any outside source: review it, test it, check the licences of dependencies and of any code that looks copied, and follow your employer's policy. This is general information, not legal advice.

Ready when you are

Open the chat and ask your first question. It's free, and there is nothing to install.

Start chatting