Generative AI can be a valuable tool when creating, modifying, and understanding code in many languages. Popular uses include writing code to serve a specific purpose, translating code from one coding language to another, and summarising code in plain English. We discuss each of these use cases below.
Note: The following prompts were tested in Copilot.
To begin, we will ask the generative AI (in this case Copilot) to write us a snippet of code in C#. We are using C# for this first example, but many AI tools can write code in a range of different languages.
Let's start with a very simple prompt.
Prompt:
Write a C# Script that adds two numbers entered by the user.
A very simple prompt and a simple response. This code does exactly what you'd expect, it takes two numbers from the user and adds them:
A simple prompt is great for making small objects or functions, but what if you're collaborating with others and want the code to be more easily readable for your peers? In response, clarify exactly what you need, and be as specific as you feel you need to be!
Prompt:
Can you add comments that will help explain what each part of the method does?
The prompt clearly defines the task and sets out expected inputs and outputs.
The prompt defines the style, depth and clarity of the commenting desired.
Note: The following prompts were tested in Copilot for Web.
What if you don't need to start from scratch with your code? Sometimes there are already repositories full of useful code, but they are in a completely different language that you don't understand. This is another case where AI can play a helpful role.
For this example, let's start with the C# code generated in the previous section. Let's imagine that you came across this code online and want to replicate the results in Python, but you don't even know what language it's in!
Prompt:
What language is this code written in:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
double sum = num1 + num2;
Console.WriteLine($"The sum of {num1} and {num2} is {sum}");
}
}
Not only does Copilot tell us what language the original code is written in, but it gives a brief description of what it does, which we will go over in more detail in Prompt 3.
Now that we know the code is in C#, we can ask our AI tool to translate that into Python.
Prompt:
Thank you, can you rewrite that code in Python with comments?
And just like that, our code is now in python, and there's even comments!
The prompt clearly defines the task (asking it to determine the language), and sets out the requirement (writing an equivalent function in a specified language). This can also be extended to defining specific desired inputs and outputs for each function
Note: The following prompts were tested in Copilot for Web.
Sometimes you may come across code that you don't understand, whether that be from poor commenting, legacy systems, or hard concepts. AI tools can be very helpful in deciphering how a snippet or variable in a program functions.
Let's start small, and assume you want to understand what a specific variable or command within a function does. Looking at the C# code given from Prompt 1, let's find out more about the code.
Prompt:
In the following code, what is "num1" and what is "Convert.ToDouble" doing?
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
double sum = num1 + num2;
Console.WriteLine($"The sum of {num1} and {num2} is {sum}");
}
}
In the above example, Copilot described exactly what "num1" and "Convert.ToDouble" do. It even got a little carried away and explained what the entire function does.
Next, let's assume you know absolutely nothing about how a function works. You already saw some of its summarisation capabilities in the previous exercise, but this prompt will be specifically asking for maximum detail using the translated Python script from Prompt 2.
Prompt:
What does the following Python script do. Please give details on all variables and called functions:
# Prompt the user to enter the first number
num1 = float(input("Enter the first number: "))
# Prompt the user to enter the second number
num2 = float(input("Enter the second number: "))
# Calculate the sum
sum_result = num1 + num2
# Print the result
print(f"The sum of {num1} and {num2} is {sum_result}")
Copilot has given a detailed, line-by-line explanation of the code along with an example.
It is important to note that, as with all AI tool use, it is important to be clear and specific about what you would like the tool to do. There could be variables it doesn't understand or functions it hasn't seen before, and it may not always give accurate descriptions. Remember to sense check explanations provided by the AI tool.
The prompt specifically names the variables and functions it wants explanations for.
The prompt clearly defines the detail and scope of explanation required.
Note: The following prompts were tested in Copilot for Web. When uploading code to an AI tool, be careful not to upload any sensitive information.
Every programmer's favourite activity is debugging. However, debugging is another situation where AI tools such as Copilot can provide valuable assistance. When debugging, you can either give the code, the error, or both as a prompt. Depending on the specifics of your error, you may need to play around with what information you give and what broader context you provide.
One example is giving your AI Tool a description of the problem you're facing and asking what sorts of things could cause it.
Prompt:
I'm writing a C program that gives the sum of two numbers as an output. I keep encountering an error that says "Segmentation fault (Core Dumped)". What does this mean and how might I be able to fix it?
As you can see, since we didn't provide any specific code, Copilot gave a broad overview of the error and some common problems/solutions.
What if you made a typo, or accidentally defined a variable twice and just want your AI tool to tell you where the mistake occurred? In that case, it can be helpful to give it the entire problem section of code.
Prompt:
My code keeps giving an error, what is wrong with it?
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
double sum = num1 + num2;
Console.WriteLine($"The sum of {num1} and {num3} is {sum}");
}
}
The prompt specifies the error message and asks the AI to explain the meaning of the error, its possible sources, and a possible solution.