Getting Better Code from LLMs
Language models can accelerate your coding workflow significantly. But the quality of code you receive depends largely on how you ask. These practical techniques can help you get more reliable, accurate, and useful code responses.
Tips for Coding with LLMs
These prompting strategies work across most modern code-capable LLMs including GitHub Copilot, GPT-4, Claude, and Llama Code.
Core Coding Prompt Techniques
1. Specify Language and Environment
Always mention your programming language, version, and relevant frameworks.
Instead of: "Write a function to parse CSV files."
Try: "Write a Python 3.10 function using pandas 2.0 to parse CSV files with custom delimiters."
2. Include Context and Constraints
Describe your project environment, limitations, and specific requirements.
Example: "I'm working on a memory-constrained IoT device with 512MB RAM. Write a C++ function that efficiently processes sensor data without causing memory leaks. The data comes as a binary stream with the following format: [...]"
3. Provide Examples of Existing Code
Share relevant snippets of your codebase to maintain style consistency.
Example:
Here's how I've implemented other API endpoints: ```javascript app.get('/users', async (req, res) => { try { const users = await User.find().select('-password'); res.json(users); } catch (err) { res.status(500).json({ message: err.message }); } }); ``` Now help me implement a similar endpoint for retrieving product data with pagination.
4. Request Explanations
Ask the model to explain key parts of generated code, especially for complex algorithms.
Example: "Write a function to implement QuickSort in JavaScript. After the code, explain the time complexity and how the partitioning logic works."
5. Specify Error Handling
Explicitly request proper error handling and edge case coverage.
Example: "Write a Java method that reads from a file. Include comprehensive error handling for file not found, permission issues, and IO exceptions. Return a Result object rather than throwing exceptions."
Practical Coding Prompt Patterns
The Step-by-Step Implementation
I need to implement [feature/function] in [language/framework]. Requirements: 1. Must handle [specific requirement] 2. Should support [edge case] 3. Follow [specific pattern/style] Current project context: ``` [relevant existing code] ``` Please provide the implementation with comments for complex logic.
The Code Debugging Pattern
The following code has a bug: ``` [problematic code] ``` Expected behavior: [what should happen] Actual behavior: [what's happening instead] Error message (if any): [error text] Please identify the issue and provide a corrected version with an explanation of what was wrong.
The Optimization Request
Here's a working function that [describe what it does]: ``` [current implementation] ``` This code works but it's [slow/verbose/hard to maintain]. Please optimize it for [performance/readability/maintainability] while maintaining the same functionality.
Advanced LLM Coding Techniques
6. Iterative Refinement
Start with a basic implementation, then iteratively request improvements.
Initial Request: "Write a basic React component for a login form."
Follow-up: "Now enhance it with form validation using Formik."
Final Refinement: "Add accessibility features and error states to the form."
7. Test-Driven Prompting
Provide or request tests before implementing the actual code.
Example: "First write Jest tests for a function that validates email addresses with these requirements: [list requirements]. Then implement the function to pass those tests."
8. Interface-First Design
Ask for interfaces/signatures before full implementations.
Example: "I'm creating a library for file operations. First, design the public API interfaces for the main classes. After we agree on those, we'll implement the details."
Keep Learning
Effective prompting is a skill that improves with practice. Start with these techniques and refine your approach based on results.
View More Advanced Techniques