Stop using ChatGPT wrong: the developer prompt guide that ships code
As developers, we've all been there - stuck in a sea of code, searching for a lifeline to get the job done. And then, we discover AI tools like ChatGPT. But, just like using Google without proper search queries, most devs use ChatGPT (and similar AI tools) without getting the most out of them. There's a gap between being a casual user and a power user - and that's what we're here to bridge.
The core mistake
The biggest mistake developers make when using ChatGPT is providing zero context, assigning no role, and specifying no format. Let's illustrate this with an example:
# Bad prompt
Write a function to sort a list of integers.
This prompt is doomed to fail. Why?
- No context: What programming language are we using? What are the performance requirements?
- No role: Are we looking for a simple implementation or an optimized one?
- No format: Do we want a code snippet, a full function, or an explanation?
Compare this to a well-crafted prompt:
# Good prompt
I'm a Python developer working on a data processing pipeline.
I need a function to sort a large list of integers efficiently.
Please provide a high-quality, readable code snippet with type hints and a brief explanation.
The difference is stark. By providing context, assigning a role, and specifying a format, we've set ChatGPT up for success.
The SPAR framework for dev prompts
To take your ChatGPT game to the next level, we introduce the SPAR framework:
S - Spec
- Clearly define the task: What do you want to achieve?
- Provide context: What's the project about, what language are you using, etc.?
- State the requirements: Are there any specific constraints or performance needs?
P - Pattern
- Reference existing solutions: Are there similar problems or patterns you've used before?
- Provide examples: Include code snippets, APIs, or data samples
A - Anti-pattern
- Highlight potential pitfalls: What mistakes should ChatGPT avoid?
- Specify error handling: How should errors be handled or reported?
R - Refinement
- Define the output format: What do you want ChatGPT to return (code, explanation, etc.)?
- Request refinement: Are there specific aspects you'd like ChatGPT to focus on?
Let's see the SPAR framework in action:
# SPAR prompt example
## Spec
I'm working on a Node.js project and need to implement authentication using JSON Web Tokens (JWT).
The task is to create a function that generates a JWT token given a user's details.
## Pattern
I've used similar libraries like `jsonwebtoken` before.
The function should take in a user's ID, name, and email.
## Anti-pattern
Please avoid using any deprecated libraries or functions.
Error handling should be minimal, just throwing a generic error for now.
## Refinement
The output should be a code snippet in JavaScript, with a brief explanation of the approach.
Please focus on security best practices.
5 copy-paste prompts for real tasks
Here are five actionable prompts you can use right away:
1. Debugging
I'm encountering a bug in my Python code where a list comprehension is throwing a `TypeError`.
The code is:
python
data = [{'id': 1, 'name': 'John'}, {'id': 2, 'name': 'Jane'}]
result = [item['id'] for item in data if item['active']]
The error message is: `TypeError: 'dict' object is not subscriptable`.
Please help me identify the issue and provide a corrected code snippet.
diff
2. PR review
I've got a pull request with the following changes:
diff
- const fetchData = async () => {
- const response = await fetch('https://api.example.com/data');
- return response.json();
- };
The reviewer mentioned that this might introduce a potential security risk.
Can you review the code, highlight potential issues, and suggest improvements?
javascript
3. Test writing
I need to write unit tests for a JavaScript function that calculates the area of a rectangle:
javascript
function calculateArea(width, height) {
return width * height;
}
Please provide a set of test cases using Jest, covering different scenarios (e.g., positive numbers, negative numbers, edge cases).
java
4. Refactoring
I've got a legacy Java method that's a bit hard to read:
java
public String processData(List data) {
StringBuilder result = new StringBuilder();
for (String item : data) {
result.append(item).append(",");
}
return result.toString().trim().substring(0, result.length() - 1);
}
Can you refactor this code to be more readable and efficient, while maintaining the same functionality?
plaintext
5. Architecture
I'm designing a new e-commerce platform and need to decide on a database schema.
The platform will have users, products, orders, and reviews.
Please suggest a high-level database architecture, including tables, relationships, and indexes.
Chaining prompts for complex features
For more complex tasks, you might need to chain multiple prompts together.
For example, let's say you want to implement a new feature for a web application:
- Initial prompt: Describe the feature and its requirements.
- Follow-up prompt: Provide more context and clarify specific aspects.
- Refinement prompt: Request changes or improvements.
Here's an example:
# Initial prompt
I'm building a web app and want to implement a search bar with autocomplete functionality.
The search bar should query a backend API for suggestions.
# Follow-up prompt
The API returns a list of suggestions in JSON format: `[{ label: 'Suggestion 1', value: 'suggestion-1' }]`.
Can you provide a basic implementation using React and the `react-autosuggest` library?
# Refinement prompt
The implementation should include a caching mechanism to reduce API calls.
Can you modify the code to use a simple cache with a 5-minute TTL?
Full prompt library
While these prompts should give you a great starting point, having a comprehensive library of tested and optimized prompts can save you even more time. If you're interested in learning more about our prompt library, check out our resources page.
By following the SPAR framework, using actionable prompts, and chaining prompts for complex features, you'll be able to unlock the full potential of ChatGPT and ship code faster than ever before. Happy coding!
Looking for a complete resource? I packaged up 200 prompts / the full playbook: *AI Prompt Engineering Mastery Pack — $12***
Top comments (0)