By Christian Prokopp on 2023-04-12
Learn to harness the potential of ChatGPT4, your virtual programming partner, with nine prompting tips. Improve your programming skills by communicating clearly, engaging in conversation, using the proper syntax, and iterating on complexity. Keep context fresh, and ChatGPT4 will be invaluable in your coding journey.
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.
Bad:
I need a function to get the sum.
Better:
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.
Start with:
Create a Python function to add two numbers.
Then iterate:
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.
Question:
Create a Python function to calculate a pseudo-random number. Are there any questions or clarifications needed?
ChatGPT response:
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.
Example
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.
Example
'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.
Example
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.
Example
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.
Example
How can I refactor the code to be more modular?
```
<relevant code>
```
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.
Happy coding!
Christian Prokopp, PhD, is an experienced data and AI advisor and founder who has worked with Cloud Computing, Data and AI for decades, from hands-on engineering in startups to senior executive positions in global corporations. You can contact him at christian@bolddata.biz for inquiries.
2024-03-14
Tax Shrink is a new online tool that helps owner-operators of Limited companies in the UK calculate and visualise the ideal salary-to-dividend rati...
2023-11-07
OpenAI's DevDay announcement yesterday addresses issues I wrote about in the infeasibility of RAG after building Llamar.ai this summer. Did I get i...
2023-02-07
The Battle of the AI Chatbots Begins: Google's Bard Takes on ChatGPT.
2022-12-02
Finally. AWS re:Invent 2022 brought the answer to both Databricks and Athena's worst limitations. Athena Spark promises to bring Delta Lake scale-o...
2022-08-08
There is one simple thing most companies miss about their data. It has been instrumental in my work as a data professional ever since.
2022-05-30
Your hard work is not appreciated. So why should you still do it? There is a good reason.