Prompt Engineering for Code Generation — What Actually Works
Most prompt engineering advice is generic. This is what actually works when you're using Claude Code daily to write Kotlin, TypeScript, and Rust across 190+ projects.
On this page
Most prompt engineering content reads like a listicle written by someone who's never actually shipped with AI tools. Here's what works in practice — specifically for code generation, tested across real Android, web, and systems projects.
The core problem with vague prompts
"Write a ViewModel for my login screen" produces boilerplate. It has no idea about your architecture, your state management approach, your error handling conventions, or what your existing code looks like.
The AI isn't bad at code. It's bad at reading your mind.
Better: "Write a LoginViewModel using Hilt injection, StateFlow for UI state, and a sealed class for LoginUiState with Loading, Success, and Error variants. The repo interface is AuthRepository with a suspend fun login(email: String, password: String): Result<User>."
Now it can produce something you'd actually use.
Give it your conventions, not just your task
The biggest lever in code generation quality is context about your existing patterns. Before asking for any non-trivial feature:
- Paste a representative existing file (a ViewModel, a composable, an API service)
- State which libraries you're using and which you're not ("Hilt for DI, no Retrofit — using Ktor")
- Name the architecture ("Clean Architecture, data/domain/presentation layers")
One good example file is worth 10 paragraphs of description.
Surgical over sweeping
"Refactor this whole file" produces worse results than "Extract the error handling logic from lines 45–67 into a separate function called handleApiError."
The more precisely you define the scope, the better the output. This is true across all AI coding tools. The model isn't limited by intelligence — it's limited by ambiguity.
For every task, define:
- What specifically to change
- What to leave alone
- What the output should look like
Chain your context across prompts
A session with clear state produces better code throughout. Start each session by establishing:
- What project this is
- What you're trying to accomplish in this session
- What you've already done
"This is a Kotlin Android project using Clean Architecture with Hilt. Today I'm adding a notification system. I've already created the NotificationRepository interface — now I need the implementation."
This takes 30 seconds and saves multiple back-and-forth correction rounds.
Verification prompts matter as much as generation prompts
After generating code, prompt for verification rather than just assuming it's right:
- "Does this handle the case where the network call succeeds but returns an empty list?"
- "Is there a memory leak risk in this Coroutine scope usage?"
- "Does this compile given the imports I have?"
The model can catch its own errors if you ask it to look for them. Most people skip this step.
What doesn't work
- "Make this better" — no criteria, produces surface changes
- "Write tests" without specifying which cases to cover
- "Fix the bug" without pasting the stack trace and relevant code
- Asking for complete files without showing existing patterns
The actual workflow
- Paste relevant existing code as context
- State the task precisely with scope boundaries
- Specify libraries, patterns, conventions
- Generate
- Prompt for edge case review
- Apply only what's correct
That's it. No magic. Just specificity.
The engineers getting the most out of AI code generation aren't using better prompts — they're treating the AI like a senior developer who just joined and needs onboarding, not like a magic wand.
Sudarshan Chaudhari
AI Systems Builder / Product Engineer
Bangkok, Thailand
Solo Android developer with 13+ years in QA, building Android apps, AI automation systems, and developer tools at SudarshanTechLabs.
Related Posts
Building something? Available for Android dev and QA consulting.
Work with meComments — powered by Giscus
Apps tagged with this
FocusPulse
Pomodoro focus manager — configurable work/break intervals, session history, distraction logging, and ambient soundscapes for deep work sessions.
ReadHabitForge
RPG-style habit tracker — habits as quests with XP, leveling, and achievement unlocks. Streak protection mechanics and difficulty scaling per habit.
ReadLifeChronicle
Personal journal with structured entry capture, tag-based organization, and local Room DB persistence — search across entries without any cloud dependency.
Read