AI Tools Compared

Provide 2-3 concrete working examples showing your preferred style and patterns; include edge cases you want handled; show error handling patterns from your codebase. Use real code snippets rather than descriptions. Highlight the specific parts you want the AI to emulate. Examples significantly improve code quality and alignment with your project standards. This guide covers effective example strategies for AI coding tools.

Why Examples Matter for AI Code Generation

AI coding tools work by recognizing patterns in your input and generating code that matches the style, structure, and conventions of your project. Without clear examples, these tools default to general-purpose patterns that may not fit your specific requirements.

When you provide well-crafted examples, you establish a reference point that the AI uses to understand:

The more relevant context you provide, the more accurate the generated code becomes on the first try.

Strategy 1: Include Real Code from Your Project

One of the most effective approaches is including actual code from your existing codebase. This teaches the AI your specific patterns and conventions.

Instead of:

Write a function to validate user input

Try:

Based on this existing validator in our codebase:

```python
def validate_email(email: str) -> bool:

 pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

 return bool(re.match(pattern, email))

Write a similar validator for phone numbers that follows the same style and error handling approach.



This approach works because the AI sees your actual implementation patterns—how you handle edge cases, what libraries you use, and what return types you prefer.


## Strategy 2: Provide Input-Output Examples


For tasks involving data transformation or API responses, showing concrete examples of expected input and output often produces better results than describing the transformation verbally.


Create a function to transform this API response:

Input:

{

 "user_id": 12345,

 "display_name": "John Doe",

 "joined": "2024-01-15T10:30:00Z",

 "preferences": {

 "theme": "dark",

 "notifications": true

 }

}

Output (normalize the structure):

{

 "id": 12345,

 "name": "John Doe",

 "memberSince": "2024-01-15",

 "settings": {

 "theme": "dark",

 "notifications": true

 }

}

Write a Python function that performs this transformation using dataclasses.



The AI can see exactly how field names map, what data types to expect, and how to restructure nested objects. This reduces back-and-forth iterations significantly.


## Strategy 3: Show Error Handling Patterns


AI-generated code often lacks proper error handling because the AI doesn't know what exceptions your project handles or how you prefer to manage failures.


Include examples that show your error handling approach:


Write a function to fetch user data from our API. Follow the same error handling pattern used here:

def get_user_by_id(user_id: int) -> Optional[User]:

 try:

 response = requests.get(f"/users/{user_id}", timeout=10)

 response.raise_for_status()

 return User(**response.json())

 except requests.exceptions.Timeout:

 logger.warning(f"Request timeout for user {user_id}")

 return None

 except requests.exceptions.HTTPError as e:

 if e.response.status_code == 404:

 logger.info(f"User {user_id} not found")

 return None

 logger.error(f"HTTP error fetching user {user_id}: {e}")

 raise

Apply this same pattern to fetch user preferences.



This ensures the generated code fits your existing error handling infrastructure rather than using generic try-except blocks.


## Strategy 4: Specify Testing Requirements


If you need tests alongside your generated code, show examples of your testing patterns:


Generate a dataclass for user preferences and write pytest tests following this pattern:

import pytest

from datetime import datetime


class TestUser:

 def test_user_creation_with_valid_data(self):

 user = User(id=1, name="Test User", created_at=datetime.now())

 assert user.id == 1

 assert user.name == "Test User"


 def test_user_repr_includes_id_and_name(self):

 user = User(id=42, name="Example", created_at=datetime.now())

 assert "42" in repr(user)

 assert "Example" in repr(user)



This approach produces tests that match your existing test structure, naming conventions, and assertion styles.


## Strategy 5: Use File References and Context


Most AI coding tools can read files from your project. Use this capability to provide rich context:


Looking at our existing models in models/user.py, generate a new Order model that follows the same patterns for:

The AI examines your existing code and generates consistent additions rather than starting from scratch with different conventions.

Strategy 6: Chain Examples for Complex Tasks

For complex requirements, build up context incrementally rather than providing everything at once:

Step 1: Establish base patterns

Our project uses this base repository structure:
```python
class BaseRepository:

 def __init__(self, db: Session, model: Type[Base]):

 self.db = db

 self.model = model


 def get_by_id(self, id: int) -> Optional[Base]:

 return self.db.query(self.model).filter(self.model.id == id).first()



Step 2: Build on that foundation

Using the BaseRepository above, create a UserRepository that adds:

This incremental approach helps the AI maintain consistency across complex, multi-file generation tasks.

Common Mistakes to Avoid

Providing too few examples: A single line of code rarely provides enough context. Three to five relevant examples usually work better.

Using outdated examples: Make sure your reference code reflects current library versions and best practices. AI tools may reproduce old patterns if that’s what you show them.

Mixing different coding styles: If your examples come from different parts of your codebase with different conventions, the AI may produce inconsistent output. Stick to examples from the same module or files with consistent patterns.

Forgetting to specify the scope: Examples should clarify not just what to do, but what NOT to do. If you don’t want certain features, mention that explicitly.

Practical Example: Complete Prompt

Here’s a complete example combining several strategies:

Generate a FastAPI endpoint for user registration.

Context from our existing codebase:
- Our User model in models/user.py uses SQLAlchemy with these fields: id, email, username, password_hash, created_at
- Our error handling in utils/exceptions.py uses custom exception classes
- Our tests in tests/api/ follow pytest conventions

Example of our existing endpoint:
```python
@app.post("/login")

def login(credentials: LoginRequest, db: Session = Depends(get_db)) -> TokenResponse:

 user = db.query(User).filter(User.email == credentials.email).first()

 if not verify_password(credentials.password, user.password_hash):

 raise InvalidCredentialsError()


 token = create_access_token({"sub": user.email})

 return TokenResponse(access_token=token, token_type="bearer")

Write a registration endpoint that:

Also write a unit test file following our testing conventions. ```

This prompt provides everything the AI needs to generate consistent, production-ready code.

Built by theluckystrike — More at zovo.one