I have been given access to GitHub CoPilot at work and have started experimenting with it today. I say experimenting because the client for whom I am working does not permit AI generated code to be used in production systems. For my first experiment I used the following prompt:
Provice a Java class to determine the next date where the day of the week is on of Tuesday, Thursday or Saturday
Despite the typographical errors in the prompt, CoPilot returned the following code
import java.time.DayOfWeek;
import java.time.LocalDate;
public class NextSpecificDayFinder {
public static LocalDate getNextSpecificDay(LocalDate startDate) {
LocalDate nextDate = startDate.plusDays(1); // Start from the next day
while (true) {
DayOfWeek dayOfWeek = nextDate.getDayOfWeek();
if (dayOfWeek == DayOfWeek.TUESDAY ||
dayOfWeek == DayOfWeek.THURSDAY ||
dayOfWeek == DayOfWeek.SATURDAY) {
return nextDate;
}
nextDate = nextDate.plusDays(1); // Move to the next day
}
}
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate nextSpecificDay = getNextSpecificDay(today);
System.out.println("Today: " + today);
System.out.println("Next specific day (Tuesday, Thursday, or Saturday): " + nextSpecificDay);
}
}
On casual inspection that is reasonable code and would work. However there are a few issues with the code:
- The if clause uses the
==operator to compare objects instead of using theequals()method, for exampleTUESDAY.equals(dayofWeek). - In the
main()the arguments to contain concatenated strings instead of putting each string in its own call toprint()orprintln().
There is no need to have a local variable nextSpecificDay instead the call to getNextSpecificDay() could be used as an argument in a println() call, although the same is true of the today variable, but that is a question of style. The output concluded with an option to get details on how to handle edge cases. While most of the edge cases were reasonable, it did suggest checking invalid date input due to incorrect parsing (while failing to note that the method does not need to parse anything and even the example of how to call it is not actually parsing a date).
I would be unhappy if any developer had produced code like this for use in our systems and they would have been asked to refactor it. However it was an interesting first experiment but it is going to be awhile before tools like this should be allowed to produce code for applications that have to be stand the test of time.