Beginners guide on how to prompt ChatGPT-like tools for programming tasks
Google’s generative artificial intelligence model Bard is now updated with the capability to generate code, debug, and give code explanations for over 20 languages like Python, Java, C++, and Go, among others. Not just Bard, many generative AI systems like ChatGPT can assist in software development tasks.
However, to use these tools efficiently, users need to input accurate prompts. Here are some examples of prompts for various programming tasks.
For generating code snippets
Users can make their prompts specific by including details like tasks required to accomplish, the programming language, and any additional constraints for the code snippet. To look for a specific type of answer, user may benefit from ‘training’ the tool by inputting examples. Further, to make sure the AI tool understands the prompt correctly, a user needs to use proper formatting and syntax, including correct capitalisation and punctuation.
To personalise the chatbot’s response, a programmer should give relevant user information and start the conversation with a system message to set the context. You may assign the tool a role to play.
For example:
As a (programming language) developer, write a (detailed description of the task)
Example:
#As a Python developer, write a program to multiply two integers and return the result as a whole number.
Or
#As a mobile app developer, write a Java program to calculate the heart rate of the user for an Android app
A user may also provide high-level task description and more context on where the code will be used and the functionalities it will interact with, suggests Microsoft’s blog on prompt engineering.
For example:
#Generate a Python program to load iris data from scikit-learn datasets and plot the training data. Import any needed libraries first.
Some other examples of prompts:
#Generate a code snippet to solve (program description) in (name of preferred language)
#Convert the code (insert code here) from (programming language name) to (programming language name)
#Write a function that accepts (input) and returns (output)
#Write a code for handling (specific error or exception) in (name of the programming language)
To fix bugs
ChatGPT and similar tools can be used not just for generating codes but also fixing bugs and errors.
Some of the examples are:
#Debug this code snippet (code)
#Diagnose and fix (issue in the code snippet) in my code (code snippet)
#What is causing (specific error message) in this code: (code snippet)
#What are the potential errors related to (specific module) in my code (code snippet)