Activity ‐ Improving Java Code - accentient/github-copilot-devs GitHub Wiki

Improve Java Code Using GitHub Copilot

Objective

This activity introduces Java developers to GitHub Copilot through hands-on exploration of legacy, modernized, and poorly-written Java code. The activity demonstrates how Copilot assists in analyzing, refactoring, and improving code quality while streamlining the development process. By the end of the activity students will:

  • Learn to use GitHub Copilot to analyze, modernize, and debug Java code effectively
  • Develop the ability to identify outdated or poorly-written code and apply best practices for improvement
  • Understand how to collaborate with AI tools like GitHub Copilot to streamline development workflows and enhance problem-solving skills

Project

Use GitHub Copilot to update and improve legacy Java code.

Prerequisites

  • Java Development Kit (JDK) installed
  • VS Code or IntelliJ IDEA with GitHub Copilot enabled
  • Active GitHub Copilot subscription

Steps

Step 1: Project setup

  • Create a new Java project
  • Open or add Main.java and replace it with the following code
import java.util.Vector;

public class Main {
    public static void main(String[] args) {
        // Use Vector instead of ArrayList
        Vector list = new Vector();
        list.addElement("Apple");
        list.addElement("Banana");
        list.addElement("Orange");

        // Iterate with a traditional for loop
        for (int i = 0; i < list.size(); i++) {
            String fruit = (String) list.elementAt(i); // Explicit cast needed
            System.out.println(fruit);
        }

        // Create a Runnable as a named class
        MyRunnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

// Runnable implemented as a separate class
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Hello from a thread!");
    }
}
  • Build and run the app

Step 2: Ask Questions About The Code

  • Highlight all code, right-click, and select GitHub Copilot > Inline Chat
What version of Java is this?
When did they make Vector obsolete?
What is the latest version of Java?
What version of Java do I have installed?
  • Go to the Terminal and run java -version if you want

Step 3: Modernize the Code

  • Open the GitHub Copilot Chat, not Inline Chat
Modernize the code in Main.java
  • Replace the code with the modernized code
  • Build and run the app

Step 4: Experience poorly-written Java code

  • Replace the code with the the following code
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Using raw type LinkedList instead of ArrayList
        List stuff = new LinkedList();
        
        // Adding completely random types
        stuff.add(true); 
        stuff.add("World");
        stuff.add(null);
        stuff.add(new Random());
        stuff.add(7);

        // Using an Iterator in the most awkward way possible
        Iterator it = stuff.iterator();
        Object a = it.hasNext() ? it.next() : true;
        Object b = it.hasNext() ? it.next() : "Still Nope";
        Object c = it.hasNext() ? it.next() : "Keep Guessing";
        Object d = it.hasNext() ? it.next() : "Almost There";
        Object e = it.hasNext() ? it.next() : "Done";

        // Casting with reckless abandon
        String value1 = (String) b; // May cause ClassCastException
        int value2 = (int) e;       // Definitely will cause ClassCastException
        boolean value3 = (boolean) a; // Uhm... no.

        // Random printing chaos
        System.out.println("First value: " + value1);
        System.out.println("Second value: " + value2);
        System.out.println("Third value: " + value3);

        // Bonus: Infinite loop for no reason
        while (true) {
            System.out.println("This is fine...");
            if (Math.random() > 0.999999) break; // Tiny chance to exit
        }

        // Arbitrary exception handling
        try {
            System.out.println(1 / 0); // Divide by zero, classic
        } catch (Exception eee) {
            System.out.println("Something went wrong, but who cares?");
        }

        // End the madness
        System.out.println("Program finished... or did it?");
    }
}
  • Build and run the app (you may have the manually break)

Step 5: Fix the Code

  • Open the GitHub Copilot Chat (not Inline Chat)
Update this code to follow best practices
  • Replace the code with the fixed code
  • Build and run the app

Summary

This activity guided your through exploring and improving Java code with the assistance of GitHub Copilot. You were able to analyze legacy and poorly-written code, use Copilot to modernize and fix issues, and gain insights into effective coding practices and collaboration with AI-powered tools.