Exercise 1.5: Refactoring in VS Code

Author

Chuck Nelson

Published

September 8, 2025

1 Refactoring a Java Package in VS Code

What is Refactoring?

Refactoring is the process of restructuring existing computer code without changing its external behavior. Think of it as cleaning up and reorganizing your code to improve its design, readability, and maintainability.

Why is Refactoring Necessary?

As software projects grow, their codebase can become complex and difficult to manage. Refactoring is necessary to:

  • Improve Code Readability: Make the code easier for you and other developers to understand.
  • Reduce Complexity: Simplify the code’s structure, making it less prone to bugs.
  • Facilitate Feature Addition: A clean, well-organized codebase is much easier to modify when you need to add new functionality.

The Role of an IDE in Refactoring

While you could refactor manually by searching and replacing text, this process is highly prone to errors and can be very time-consuming. Modern Integrated Development Environments (IDEs) like VS Code provide automated refactoring tools that:

  • Ensure Accuracy: The IDE’s refactoring engine analyzes your code and automatically updates all references, from file paths to import statements, ensuring that nothing is missed.
  • Save Time: What might take hours to do manually can be done in seconds with just a few clicks.
  • Prevent Errors: By handling the changes intelligently, the IDE prevents syntax errors and ensures your project remains in a runnable state.

Prerequisites

  • You have the hello-world Java project open in VS Code.
  • Your project’s main class is App.java and is located in the com.example.helloworld package.

Refactor the Package Name in VS Code

Tip

Open the App.java and AppTest.java files in the VS Code text editor before you start refactoring. You’ll notice that the package name in those files will change automatically as the directories are renamed. You may get a popup from Visual Studio Code asking for “Extension ‘Language Support for Java(TM) by Red Hat’ wants to make refactoring changes with this file move”. Select “Do not ask me again” and then click on the OK button.

VS Code provides a simple and reliable way to rename packages and automatically update all references in your code.

  1. In the VS Code Explorer sidebar, navigate to the src/main/java directory.
  2. Right-click on the com folder and select “Rename…”.
  3. Type edu and press Enter. The folder structure should now be edu/example/helloworld.
  4. Next, right-click on the example folder and select “Rename…”.
  5. Type pstcc and press Enter.
  6. Finally, create a new folder inside pstcc and name it with your username (e.g., chnelson).
  7. Drag the helloworld folder into your new username folder.

Your package structure should now be src/main/java/edu/pstcc/student_username/helloworld. VS Code will automatically update the package declaration in App.java and any other classes to reflect the new name.


Update the Project’s pom.xml

Even though VS Code updated your Java files, the Maven configuration in your pom.xml still needs to be manually updated to reflect the new package.

  1. Open your pom.xml file in the root of your project.
  2. Find the <groupId> tag. Change its value from com.example.helloworld to edu.pstcc.student_username.
  3. Find the <mainClass> tag within the maven-jar-plugin configuration. Change its value from com.example.helloworld.App to edu.pstcc.student_username.helloworld.App.
Tip

Select any com.example string in your pom.xml file then hit Ctrl+H to open the Replace in Files tool. Type edu.pstcc.your_student_username in the replace box, then replace one at a time or all at once using the buttons next to the replace box.

Your pom.xml should now look similar this, but with your student username:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.pstcc.student_username</groupId> ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>edu.pstcc.student_username.helloworld.App</mainClass> </manifest>
          </archive>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</project>

Verify and Build the Project

  1. Open your terminal in VS Code.

  2. Run the following command to clean, compile, and package your project:

    mvn clean package

If the build is successful, congratulations! You have successfully refactored your project’s package and updated its build configuration. You can now run the executable JAR file to confirm your app still works as expected.

Back to top