- Prompt/Deploy
- Posts
- How to Write Prompts That Generate Reusable, Maintainable Code
How to Write Prompts That Generate Reusable, Maintainable Code
Most developers prompt for code. The better ones prompt for structure.
If your AI-generated output feels bloated, hard to reuse, or impossible to debug — the problem usually starts upstream: you're asking for implementation before you've defined the architecture.
LLMs excel at following patterns, but only if you give them a clear structural framework to fill.
Core Insight: Prompt for Architecture, Then Implementation
Instead of asking "Write a login form", prompt the model to design:
Component hierarchy and responsibilities
Data flow and interface contracts
File organization and layer boundaries
Testing strategy for each piece
This transforms the model from a code generator into a system architect.
Why Structure-First Actually Works
This approach forces adherence to time-tested software design principles, leading to higher-quality systems. It naturally guides you toward a SOLID architecture:
Single Responsibility & Composability: Each component or hook has one clear purpose, making it easy to test, reuse, and understand.
Open/Closed Principle & Maintainability: With clear boundaries (like an API service layer), you can change implementation details (e.g., swapping
fetch
foraxios
) without breaking consumer components.Dependency Inversion & Reusability: By defining
props
and interface contracts first, components depend on abstractions, not concrete implementations, making them reusable across different contexts.AI Efficiency: Focused, single-responsibility prompts produce dramatically higher-quality code than broad, ambiguous requests.
🚫 Common Approach:
"Write a login form with email, password, and 2FA support."
The model guesses the structure — and usually guesses wrong. You get a 200-line monolithic component that handles UI, state, validation, and API calls.
✅ Structure-First Approach:
"You are an expert software architect specializing in highly maintainable, scalable frontend applications. I need you to design the architecture for a login feature.
**Requirements:**
The feature must support email/password authentication, optional TOTP 2FA, and a basic account recovery flow.
**Instructions:**
Design a complete architectural plan. Do not write full implementation code. Your output must include:
1. **File & Directory Structure:** A clear file organization following domain-driven principles for a React/TypeScript project.
2. **Component Hierarchy:** Define the key React components and their single responsibilities.
3. **State & Logic Management:** Define the custom hooks responsible for state, validation, and business logic.
4. **Service Abstraction:** Define the API service layer responsible for all external communication.
5. **Type Contracts:** Provide the essential TypeScript interfaces for data flows (e.g., props, API responses).
6. **Testing Strategy:** Outline the testing approach for each layer (unit, integration).
7. **Documentation:** Include a basic structure for a `README.md` file for this feature directory."
Using a model like Claude 3.5 Sonnet, you'd get a detailed plan:
// 1. File & Directory Structure
src/
└── features/
└── auth/
├── components/
│ ├── LoginContainer.tsx
│ ├── LoginForm.tsx
│ ├── TOTPChallenge.tsx
│ └── __tests__/
├── hooks/
│ ├── useAuthFlow.ts
│ └── useLoginValidation.ts
├── services/
│ ├── authAPI.ts
│ └── tokenStorage.ts
├── types/
│ └── index.ts
└── README.md
// 2. Component Hierarchy & Responsibilities
// - `LoginContainer.tsx`: Orchestrates the entire flow. Manages state via `useAuthFlow`.
// - `LoginForm.tsx`: A pure, controlled UI component for email/password entry.
// - `TOTPChallenge.tsx`: A pure UI component for entering the 2FA code.
// ... (Other sections like Interfaces and Testing Strategy) ...
// 7. README.md Structure
# Authentication Feature
This directory contains all code related to user authentication.
- **/components**: Reusable React components (UI only).
- **/hooks**: Business logic and state management.
- **/services**: API layer abstraction.
- **/types**: TypeScript interfaces for this feature.
Note that this process can be iterative. You might ask a follow-up like, "Refine the AuthAPI
interface to better handle different error states," to hone the design before implementation.
Bonus Tip: Visualize the Architecture
For complex features, ask the model to summarize the design visually. Add this to your prompt: Finally, generate a Mermaid.js graph visualizing the component hierarchy and data flow from LoginContainer down to the authAPI.
This gives you a diagram you can paste directly into GitHub comments or documentation, making the architecture instantly understandable to your team.
What Happens Next
With this architectural scaffold, your implementation prompts become surgical and focused:
"Implement LoginForm.tsx
using the LoginFormProps
interface. It must use React Hook Form for validation and have accessible form labels. It should have no API calls or state management; that is handled by its parent."
Each component has clear boundaries. Each prompt has a focused scope. No more monolithic messes.
Tool Setup
Model: Use a state-of-the-art model like Claude 3.5 Sonnet or a GPT-4 series model, as they excel at architectural thinking and following complex instructions.
Storage: Save your architectural prompts as GitHub Gists or, even better, as templates in your project management tool (Linear, Jira).
Integration: This workflow is tool-agnostic and works in IDE extensions (Cursor, Continue.dev), web interfaces, or CLI tools (Aider).
The Compound Effect
This approach creates a positive feedback loop:
better architecture → cleaner prompts → higher quality code → easier maintenance
Most importantly, your architectural scaffolds become reusable templates. The login architecture above can be adapted for any authentication feature across future projects.
This is the difference between using AI as a code completion tool versus using it as an engineering partner.
This upgrade comes from Stage 2: Forge the Foundation of Prompt-to-Production → Read the full 6-stage system
Reply