DEV Community

Cover image for AI Writes Code Fast. Here's How I Make Sure It Writes Good Code.
deepak kumar
deepak kumar

Posted on Edited on

AI Writes Code Fast. Here's How I Make Sure It Writes Good Code.

AI coding assistants feel like magic one minute and a confident liar the next.

One moment it produces a clean, working function in seconds. The next, it invents an API that doesn't exist and defends it with total certainty.

After using LLM assistants every day, I've noticed the difference rarely comes down to the tool. It comes down to how you work with it.

Here are the seven habits that made the biggest difference for me.

- Give context, not just a request
Compare these two prompts:

"Write a function to validate emails."

versus

"Write a TypeScript function isValidEmail(input: string): boolean for a signup form. We already use Zod, so avoid regex-heavy solutions. Reject empty strings and anything over 254 characters. Include Vitest unit tests."

The second one tells the model the language, the signature, the constraints, the existing stack, and what "done" looks like.

My rule: tell it what you'd tell a new teammate on their first day.

- Work in small steps
Asking for a whole feature in one shot gives you a giant blob of code that's painful to review.

Instead:

• Ask for a plan first

• Agree on the approach

• Build one piece at a time

• Run the code between steps

Small steps mean small diffs. Small diffs are easy to check.

- Paste the real error
"It doesn't work" gives the model nothing to reason about.

Paste the actual stack trace, the failing test output, and the code it points to. Nine times out of ten, that alone gets you the fix in one round.

- Review AI code like a PR from a stranger
Generated code always looks polished, even when it's wrong. So I review it with healthy suspicion:

• Does it do what I asked, or just what it sounds like it does?

• Do these APIs actually exist?

• What happens with empty input, nulls, or huge input?

• Is there anything insecure, like unsanitized input or hard-coded secrets?

• Does it fit our codebase's conventions?

If I can't explain what a block of code does, it doesn't get merged.

- Ask for tests, then read the tests
Tests are one of the best uses of an assistant. But a test that asserts the wrong thing gives you false confidence, so read them.

A great trick: write the tests first, confirm they fail, and only then ask for the implementation.

- Use it to learn, not just to type
Some of my most valuable prompts aren't "write this" at all:

• "Explain this function line by line."

• "What are the trade-offs between approach A and B?"

• "What could go wrong with this in production?"

• "How could this be simpler?"

Treat the assistant as a patient reviewer and tutor, and you'll get better at the craft instead of just outsourcing it.

- Know when to stop prompting
If you're five or six replies deep and the output keeps getting worse, the conversation has drifted.

Don't keep patching. Start fresh, restate the problem with what you've learned, and try again. Or spend five minutes thinking it through yourself. It's often faster.

A few things to stay careful about
• Never paste secrets, credentials, or customer data into any tool, and check your company's policy first.

• Know your team's rules on licensing and generated code.

• Don't let your skills atrophy.

If you stop understanding the code, you can't debug it when it breaks.

An AI assistant is a fast, tireless collaborator that sometimes gets things wrong.

You are still the engineer. You set the direction, verify the result, and own what ships.

Do that, and AI makes you noticeably faster without making you sloppier.

What's the best prompting habit you've picked up? I'd love to hear it in the comments.

For more, visit https://codespanda.com

Top comments (0)