As a developer, you always look for ways to improve your programming skills and streamline your workflow. Currently, ChatGPT4 is the state-of-the-art language model by OpenAI and can be your go-to virtual programming partner. To get the most out of this powerful AI, you must know how to interact effectively. I share nine prompting tips based on my experience that'll help you harness the potential of ChatGPT.
The first step to effective prompting with ChatGPT is being clear and concise. Lay out your requirements in plain English, but make sure there's no room for misinterpretation. This ensures that the AI comprehends your goals and provides accurate responses.
I need a function to get the sum.
Create a Python 3 function that takes two integer arguments and returns their sum.
Do not confuse ChatGPT with complex prompts from the get-go. Start with the most straightforward problem, the "happy path," and then gradually add complexity and edge cases.
Importantly, as code and complexity grow, refactor and modularise so your interactions and relevant code stay as small and focused as possible. Keep breaking down the code and problems into smaller chunks.
"Create a Python function to add two numbers."
"Now, modify the function to cast strings to numbers."
ChatGPT wants to please you and will guess missing or unclear requirements from your prompt. That can often work with minor oversights but waste time and frustrate otherwise. There is a simple solution, close your statements with a question to ask for clarifications or additional information if needed.
Create a Python function to calculate a pseudo-random number. Are there any questions or clarifications needed?
Before proceeding, I would like to confirm if you have any specific requirements or constraints for the pseudo-random number generation, such as a specific range, distribution, or seed value. Additionally, I would like to know whether you have a preference for a particular algorithm or library to be used for the generation.
Now, you can add the missing details to the original prompt and retry until ChatGPT runs out of questions. This can be powerful and rewarding, i.e. you might be surprised by what ChatGPT reveals about edge cases and issues you have yet to think of.
When sharing code snippets in your prompt, use triple backticks to help ChatGPT differentiate between code and text for more accurate responses.
Add error handling and logging to my code:
``` def add_numbers(a, b):
return a + b
If ChatGPT's code generates an error, immediately share the error message as a prompt with the previous ChatGPT code as existing context. You'll often get a helpful fix or explanation without even adding any thought or comment yourself. Note you will occasionally have to do the heavy debugging yourself. But it is sweet when a complex-looking stack trace is traced back to a typo or missing variable by ChatGPT in seconds with no effort.
"'TypeError: unsupported operand type(s) for +: 'int' and 'str''
This also works for your code. Of course, you would have to share the relevant snippet with the error to give it context.
The AI's output stops long answers in the middle of a sentence or code because it has a length limit. You can use 'continue' to prompt a continuation. However, things get messy when it breaks in the mid-line of code output. You want to continue from the last complete line and hint that it was code to give a better chance of it continuing the output formatted as a code block. To do so, backtick-quote the last complete line and ask to include it.
"Continue from and including `return a + b`"
Increase the quality of ChatGPT's output by adding 'Use best practices.' to your prompt. This nudges the AI towards implementing error handling, documentation, type annotations (in Python), logging, and other good practices. Of course, being explicit about anything you want to do or adding examples works even better.
Give it a hint.
Create a Python function to parse a CSV file. Use best practices.
Be explicit and get even more.
Create a Python function to parse a CSV file.
Use documentation, type annotation, error handling, and logging.
Use best practices.
ChatGPT can refactor your code or suggest improvements. However, it might apply suggestions immediately, which can combine good and bad suggestions. Prompt the AI to list and explain recommendations before applying them to maintain control.
Can you list and explain suggestions for improving this Python code without applying them?
After reviewing the suggestions:
Please apply suggestions 2 and 4 to the code.
ChatGPT has a limited context window, so it's essential to refresh relevant information, especially if you've made changes or mentioned it many questions ago. Include important aspects in your prompt within triple backticks to help the AI maintain context.
How can I refactor the code to be more modular?
By incorporating these prompt tips into your interactions with ChatGPT, you'll find that it can become an invaluable asset in your programming journey. The key is to communicate clearly, engage the AI in a conversational manner, and use the appropriate syntax for code snippets. Start simple and build complexity iteratively, and remember to keep the context fresh for the AI. With these strategies in place, you'll unlock the full potential of ChatGPT and elevate your programming productivity to new heights.
Christian Prokopp, BoldData.org Founder