IntelliJ IDEA

3 ways to Refactor Java Code with AI Assistant

Read this post in other languages:

Neat, efficient code is undoubtedly a cornerstone of successful software development. But the ability to refine code quickly is becoming increasingly vital as well. 

Fortunately, the recently introduced AI Assistant from JetBrains can help you satisfy both of these demands. In this article, we’ll showcase how several easy AI refactoring actions and chat prompts can make wordy, overly complicated, or inaccurate Java code clean and precise. 

Start using AI Assistant in IntelliJ IDEA

Before we start, we want to remind you to always test your code after applying refactorings. After all, testing really is the best way to make sure everything still works as it should.

Explain Java code with AI prompts 

For our article, we’ll use an algorithm as our sample code. At first glance, we can determine that our demo project does some calculations and filtering, but the exact purpose of the algorithm isn’t immediately clear. Let’s use AI Assistant to get more information about this project. This will give us a clearer picture of what’s going on, setting the stage for later decisions we’ll make to enhance the code’s readability.

To do so, position your caret at the head project class, right-click to call the context menu, select the AI icon, and select Explain Code. AI Assistant then explains what this project was created for, comments on each meaningful part of the code, and even throws in some improvement tips.

With this information in hand, we now understand that for the purposes of this article, we’ll be exploring a project with an implementation of the Convex Hull algorithm

If you don’t need a step-by-step explanation of the code, just ask AI Assistant to Simplify it in the chat. You’ll receive a short description of your project’s logic.

1. Improve Java code with AI suggestions

Now that we have a better understanding of the code, let’s see what AI Assistant can offer in terms of improving its readability and neatness. 

We put a caret at the TheAlgorithm class, right-click to call the context menu, select the AI icon, and then click Suggest Refactoring. Below you can see that AI Assistant has identified several points that need improvement. 

It has also demonstrated how the code will look if the suggested changes are implemented. In the block with this Java code, you can see the See Diff and Apply button. Once you click it, a new tab with the changes will open in the editor. This gives you a chance to scan the proposed code and accept the changes if you find them all suitable. 

In our example, we’re cautious about immediately accepting AI Assistant’s suggestions to rename classes and methods, as it might not update their references elsewhere in the project. So, first things first, we’ll ask it to leave the class and method names unchanged.

If you have an idea for a way to further enhance the generated result, we advise you to edit the existing prompt instead of building a new one, as this will yield an outcome that is more consistent with what you already have. To do so, press the kebab menu (the three vertical dots in the right-hand corner of your prompt) and select Edit

Once we have revised the code, we can go ahead and apply those changes.

2. AI-powered Rename refactoring

But what about renaming? Even though we didn’t want to immediately implement AI Assistant’s suggestions on this front, we still think renaming is a smart move. Let’s take a look at how AI contributes to traditional Rename refactoring for Java code. Just place your caret on the class you want to rename and hit ⇧F6 on macOS or Shift+F6 on Windows. You’ll notice the IDE not only offers its suggestions but also flags more meaningful options with the AI Assistant icon. Select one and watch as the IDE seamlessly updates the class name throughout your entire project.

Awesome, the code looks a lot better now that we’ve refactored it! Don’t forget to test it, though, to make sure everything works right!

3. Detect Java code problems

Our AI Assistant features another helpful action called Find Problems, which offers more than code refactoring tips. It gives you ideas for various improvements, including changes to how the code functions. 

Let’s test it on our code and see what suggestions we get back.

This tool is particularly useful when your goal is to enhance the overall quality of your code, not just its readability and organization.

The Find Problems action works a bit differently from Suggest Refactoring and doesn’t provide ready-to-use modifications that you can apply directly to your code with a See Diff and Apply button. Instead, you have to manually copy and paste the recommended code into your editor if it suits your requirements.

That is all for today. We hope you liked our short demo of some AI-based refactorings and will use them to make your code picture-perfect! 

image description