DiscordInstagram
DR
David R. Fajardo15 min read

Using AI to Write Better Code (Without Losing Quality)

How to use AI tools to speed up coding while keeping quality high: prompt patterns, review checklists, and safe workflows.

#AI#Productivity#Code Review#Developer Tools#ChatGPT#Copilot#Cursor
Using AI to Write Better Code (Without Losing Quality)

AI coding assistants like ChatGPT, GitHub Copilot, and Cursor have changed how we write software. But here's the truth: AI can make you incredibly productive or incredibly sloppy - it all depends on how you use it. The goal is speed with safety, not speed without understanding.

AI won't replace developers, but developers who use AI effectively will replace those who don't.

When AI Helps (And When It Doesn't)

AI assistants excel at certain tasks and struggle with others. Knowing the difference saves time and prevents bugs.

AI is Great For:

  • Boilerplate code - Forms, CRUD operations, API routes
  • Syntax help - Remembering APIs you don't use daily
  • Converting between formats - JSON to TypeScript, SQL to Prisma
  • Writing tests - Especially unit tests for pure functions
  • Documentation - JSDoc comments, README files
  • Refactoring - Renaming, extracting functions, simplifying logic
  • Learning new frameworks - Getting starter code and examples

AI Struggles With:

  • Complex business logic - AI doesn't understand YOUR domain
  • Architecture decisions - It can't see the big picture of your system
  • Security-critical code - Authentication, encryption, authorization
  • Performance optimization - It often suggests correct but slow solutions
  • Context across files - Large codebases confuse AI models
  • Up-to-date information - Training data has cutoff dates

The Art of Prompting

The quality of AI output directly correlates with the quality of your prompt. Vague prompts give vague code. Be specific.

Bad vs Good Prompts

// BAD PROMPT:
"Write a function to handle users"

// GOOD PROMPT:
"Write a TypeScript function called 'validateUserRegistration'
that:
- Takes an object with email (string), password (string), 
  and age (number)
- Returns { valid: boolean, errors: string[] }
- Email must be valid format
- Password must be 8+ chars with 1 number
- Age must be 18+
- Include JSDoc comments"

The CONTEXT-TASK-FORMAT Framework

Structure your prompts with three parts for consistently better results:

// CONTEXT: What's the situation?
"I'm building a Next.js e-commerce app with Prisma and PostgreSQL.
Users can add items to cart and checkout with Stripe."

// TASK: What do you need?
"Create an API route that calculates cart totals including:
- Subtotal of all items
- Tax (8% for US, 0% for international)
- Shipping ($5 flat, free over $50)"

// FORMAT: How should it look?
"Use TypeScript, include error handling, and add comments
explaining the tax logic. Return JSON with breakdown."

Real-World AI Workflows

Workflow 1: Building a New Feature

  1. 1Describe the feature requirements to AI - get initial code structure
  2. 2Review and understand every line - don't copy blindly
  3. 3Ask AI to explain any confusing parts
  4. 4Modify to fit your codebase patterns and conventions
  5. 5Ask AI to write tests for the code
  6. 6Review tests, run them, verify they actually test the right things

Workflow 2: Debugging with AI

// Share error + context with AI:
"I'm getting this error in my Next.js API route:
'TypeError: Cannot read property 'id' of undefined'

Here's my code:
[paste code]

The request body looks like:
[paste example]

What's causing this and how do I fix it?"

Workflow 3: Code Review Assistant

// Ask AI to review your code:
"Review this function for:
1. Potential bugs or edge cases
2. Security vulnerabilities
3. Performance issues
4. Code style improvements

[paste your code]

Be critical - I want to catch issues before production."

The Review Checklist

Never ship AI-generated code without reviewing it yourself. Here's what to check:

Correctness

  • Does it actually do what you asked?
  • Are there edge cases it missed? (empty arrays, null values, etc.)
  • Does error handling cover realistic failure modes?
  • Are the types correct and complete?

Security

  • Is user input validated and sanitized?
  • Are there SQL injection or XSS vulnerabilities?
  • Is sensitive data (passwords, tokens) handled safely?
  • Are API keys or secrets hardcoded? (They shouldn't be!)

Performance

  • Are there unnecessary loops or redundant operations?
  • Could this cause N+1 query problems?
  • Is it fetching more data than needed?
  • Will this scale if data grows 10x or 100x?

Maintainability

  • Does it follow your project's coding conventions?
  • Are variable and function names clear?
  • Would a teammate understand this in 6 months?
  • Is it properly typed (if using TypeScript)?

Common Pitfalls to Avoid

  1. 1Copy-paste without understanding - You'll regret it when bugs appear
  2. 2Trusting AI with security code - Always have a human review auth/encryption
  3. 3Not verifying API suggestions - AI invents functions that don't exist
  4. 4Ignoring your codebase patterns - AI doesn't know your conventions
  5. 5Using AI for architecture - It can't see your whole system
  6. 6Skipping tests - If AI wrote it, you especially need tests
  7. 7Outdated information - AI training data has cutoff dates, check docs

AI Tools I Recommend

  • Cursor - AI-first code editor, great for full-file context
  • GitHub Copilot - Excellent for autocomplete and inline suggestions
  • ChatGPT/Claude - Best for complex explanations and architecture discussions
  • v0 by Vercel - Great for generating UI components quickly
  • Codeium - Free alternative with solid performance

The Human-AI Balance

The best developers I know use AI as a collaborator, not a replacement for thinking. They use it to move faster on tedious tasks, freeing mental energy for the hard problems that actually require human judgment.

AI is best when it amplifies your thinking - not replaces it. Use it to code faster, but never stop understanding what you're building.

Conclusion

AI coding tools are incredibly powerful when used correctly. The key is intentionality: know when to use AI, craft good prompts, and always review the output critically. Your value as a developer isn't typing code - it's solving problems, understanding systems, and making good decisions. AI just helps you implement those decisions faster.

All posts