Prompting Best Practices
Learn about best practices in prompt engineering to ensure accurate, unbiased, and reliable outputs.
We’re about to start our journey of fully learning how to use AI coding assistants. However, before we look at their use cases and best practices, it’s essential to understand how to interact with them effectively. This begins with learning prompt engineering—the skill of crafting clear, precise instructions that guide AI to provide accurate, relevant results.
What is a prompt?
A prompt is simply what you tell or ask an AI program or system to get a response. Think of it like giving instructions or asking a question: just as a conversation starts when you ask someone something, interacting with an AI assistant starts with a prompt.
Prompts can take many forms, such as verbal or written instructions, visual cues, or physical gestures. For example, a teacher might give students a prompt to guide their writing on a specific topic:
This prompt is straightforward to understand and asks students to write a short piece about something they enjoy. The clearer and more specific your prompt is, the better the AI can understand what you want and give you a helpful response. In other words, the prompt is the starting point for any interaction with an AI assistant.
To drive this point home, let’s consider a scenario in which you’re traveling to Dubai and want to use AI as a travel planner to generate a budget-friendly itinerary.
Hands-on with prompt-writing
How would you ask an AI to create an itinerary for a trip to Dubai? Try writing your prompt in the widget below. Here’s a sample prompt for you:
Plan an itinerary for a Dubai trip with cool things to do.
With the prompt above, would you say it has done the job for us? Hold on to your thoughts, and we’ll get back to it shortly.
Basic guidelines
AI can interpret the requirements from even a poorly written prompt fairly smartly. However, there are instances when we do need to specify the constraints or give it some context for great results. So, for that, we have a few guidelines about how we should be careful when writing prompts. Keeping these guidelines in mind can make us great prompt engineers!
When writing prompts, we should avoid ambiguous terms or jargon to ensure that AI comprehends the instructions without confusion. Each part of the prompt should convey a clear message and clearly outline the desired outcome or task to be performed.
Following are the important prompt elements or aspects that we should consider when writing prompts.
Key elements of effective prompt writing
A prompt, generally speaking, is made up of elements that make it effective and efficient. These elements can vary depending on the context and the purpose for which it is being used. A prompt can generally consist of one or more of the following elements. Let’s understand some of them now:
Clarity
We must use clear and concise language in prompts and avoid ambiguous language. Instead of “Tell me about data analysis,” we can use “Summarize key data analysis techniques used in retail sales forecasting.”
Role
The role defines the perspective or position or hat we want GPT to wear while attempting the task. It sets the context for the interaction. For example:
Context
Context helps narrow the scope of possible responses and enables AI to provide more tailored and relevant information or assistance. It provides background information or details about the specific situation, topic, or task, guiding the AI’s understanding and generating more accurate and useful responses. For example:
Without context: What should I eat for dinner?
With context: I’m craving something savory and vegetarian for dinner, but I’m short on time.
Constraints
Constraints are limitations or guidelines that GPT must adhere to when completing the task. This can include word limits or specific criteria that must be met. For example:
Response format
Response format specifies the medium or format in which GPT should deliver its responses. This could be written text, a presentation, code, etc. For example:
Note: Not all elements apply to every prompt. Some prompts may need only clarity, while others might require additional aspects such as context or specific constraints.
Prompt writing in action: Try it again!
Considering the basic guidelines above, try again to write the prompt based on the same itinerary scenario. We hope you’ll appreciate the marked difference in the quality of the AI response. Good luck!
Here’s a sample good prompt for you:
You can add our sample prompt’s output to the AI Prompt widget below to test and compare the results with the one you wrote above.
Note: The AI Prompt widget below functions just like ChatGPT, allowing you to engage in interactive conversations to get code explanations, troubleshoot issues, and enhance your learning experience in real-time! While you'll learn how to interact with this specific AI, the use cases and techniques you develop here can be applied to any AI assistant, whether it's ChatGPT, GitHub Copilot, or another tool. By understanding how to effectively prompt and communicate with AI, you'll be equipped to use various assistants to ace your coding journey, no matter the platform.
Common AI assistant use cases for coders
Please refer to the table below to get a better idea of prompts for an AI assistant. These are only the sample prompts:
AI Assistant Use Cases | Prompt Keywords | Sample Prompts |
Code Generation | Generate | You are an expert coder. Generate a Python script that sorts a list of integers using the quicksort algorithm. |
Code Testing | Test | Test the code to ensure it runs as expected, catches any errors, and verifies correct functionality. |
Code Explanation | Explain/Analyze | I am an absolute beginner to coding and need help understanding the [insert coding language] code. Please explain what each line is doing, step-by-step, in simple terms. I’d like to know both the overall purpose of the code and how each part contributes to it. Following is the piece of Python code that’s hard for me to understand: [insert code] |
Debugging Assistance | Identify/Find | Identify potential issues in the following code snippet and suggest fixes: [insert code]. |
Documentation Creation | Create | Generate documentation for the following function, explaining its purpose, parameters, and return values: [insert function]. |
What’s next?
Exciting things are on the horizon! Now that you’ve grasped the essential prompting techniques in this lesson, you’re ready to tackle our next chapter on “Code Generation with AI Coding Assistants.” In the upcoming chapter, you’ll see firsthand how the AI coding assistant can transform our ideas into functional code, streamlining the development process and saving us valuable time.
But that’s just the start! In the following chapter, “AI Assistance for Developer Productivity,” we’ll explore how AI can take our productivity to the next level, assisting with tasks like code testing, debugging, and explaining. These powerful tools will not only make you more efficient but also help us sharpen our skills as a developer. So let’s get ready to discover how AI can become our go-to partner for tackling complex coding challenges and enhancing our workflow!