Coding with Generative AI
Coding assisted by generative AI requires us to develop skills in quickly assessing the quality of the suggested code and giving feedback to update the suggestions to more favorable ones. Understanding concepts in programming languages well helps us to assess the quality of the generated code and give the necessary feedback. Below, we illustrate some steps in an interaction with a coding assistant.
Github Copilot
Start typing to get suggestions, give feedback by continued typing to get updated suggestions. For example, starting from the partial function signature in Java below
public static int countEvenNumbersthe system suggests the following implementation:
public static int countEvenNumbers(int[] numbers) {
int count = 0;
for (int num : numbers) {
if (num % 2 == 0) {
count++;
}
}
return count;
}If we are not satisfied with the implementation, we can keep typing (let’s assume we want to have strings as arguments):
public static int countEvenNumbers(String[] numbers)The system then updates its suggestion as below:
public static int countEvenNumbers(String[] numbers) {
int count = 0;
for (String numStr : numbers) {
try {
int num = Integer.parseInt(numStr);
if (num % 2 == 0) {
count++;
}
} catch (NumberFormatException e) {
// Ignore invalid integers
}
}
return count;
}We accept the suggestion, and then can delete some code or make modification to get again suggestions for the remaining snippet. For example, we may want to report parsing errors rather than silently ignoring the NumberFormatException. We start by typing throw in the catch-block, which the system then completes to:
throw new IllegalArgumentException("Invalid integer: " + numStr);Larger changes may need multiple interactions. For the sake of illustration, let’s assume that we would rather want to use an index variable i instead of the iterator for (String numStr: numbers). We start by replacing the iterator by typing int
for (int ) {The system then completes the code to
public static int countEvenNumbers(String[] numbers) {
int count = 0;
for (int i = 0; i < numbers.length; i++) {
try {
int num = Integer.parseInt(numStr);
if (num % 2 == 0) {
count++;
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid integer: " + numStr);
}
}
return count;
}which now no longer compiles because in the loop body we are still using the iterator variable numStr. We use the ordinary quick-fix suggestion of the IDE to get a local variable numStr.
public static int countEvenNumbers(String[] numbers) {
int count = 0;
for (int i = 0; i < numbers.length; i++) {
String numStr;
try {
int num = Integer.parseInt(numStr);
if (num % 2 == 0) {
count++;
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid integer: " + numStr);
}
}
return count;
}This is still not entirely working. Typing = after String numStr then makes the system suggest String numStr = numbers[i];, which finishes the fix and the code again compiles.
public static int countEvenNumbers(String[] numbers) {
int count = 0;
for (int i = 0; i < numbers.length; i++) {
String numStr = numbers[i];
try {
int num = Integer.parseInt(numStr);
if (num % 2 == 0) {
count++;
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid integer: " + numStr);
}
}
return count;
}Development Environment Setup
Here is a quick summary of setting up our development environment. For detailed steps consult the worksheets Worksheet Java and C and Worksheet Scala (remember to tick off the worksheet quizzes on D2L when done).
- Install any Java JDK LTS 21 (preferred); Java JDK 23 also works
- Install SBT
- Install VSCode
- Add the Metals extension to VSCode