Creating Effective Prompts for Coding with ChatGPT/Claude
Learning how to create effective prompts for ChatGPT, Claude or any other LLM to write code is a valuable skill that can save time and offer new perspectives on problem-solving. To achieve the best results, it is crucial to know how to craft effective prompts.
When creating a prompt for writing code, keep the following five rules in mind:
- Be Specific: Specify the programming language and clearly describe the functionality you need. The more precise your instructions, the better the code you will receive.
- Instruct, Don’t Ask: It helps to be assertive in your requests. Instead of asking the LLM how to do something, instruct it to do the task.
- Provide formatting instructions: to minimize later manual modifications, provide clear formatting instructions. Here is an example:
Instructions for the output format:
- Output code without descriptions, unless it is important.
- Provide comments only for intricate or non-obvious code segments
- Capitalize SQL special words and function names
4. Provide Context: You should include the code that needs modification or the code that the new code will interact with.
5. Define Inputs and Outputs: If the code requires specific input or output formats, make sure to include this information in your prompt.
Prompt Structure:
When crafting prompts, it’s important to structure them in a way that’s easy for ChatGPT/Claude to understand. Here is a sample prompt structure:
- Task Description
- Formatting Instructions
- Existing Code Context
Here’s sample complete prompt :
You are tasked to implement a feature. Instructions are as follows:
Using React and the native fetch JavaScript API, Fetch the list the users from the API endpoint https://jsonplaceholder.typicode.com/users and display all of their attributes in the Users component provided further below.
Instructions for the output format:
- Output code without descriptions, unless it is important.
- Provide comments only for intricate or non-obvious code segments
- Capitalize SQL special words and function names
Existing code base:
import React, { useState, useEffect } from 'react';
const Users = () => {
const [users, setUsers] = useState([]);
return (
<div>
<h2>Users List</h2>
</div>
);
};
export default Users;