Table of contents
Table of contents
Code refactoring is the process of restructuring existing computer code — changing the factoring or layout — without altering its external behavior and functionality. This technique is important for improving the readability, quality, and maintainability of the code while simultaneously reducing its complexity.
AI-driven refactoring automates these tasks by leveraging machine learning algorithms, most notably large language models (LLMs). These AI systems can analyze large codebases much faster and more deeply than humans, identifying complex patterns and suggesting optimizations. By automating routine or intricate refactoring tasks, AI helps reduce developer workload while maintaining a high standard of code health across large codebases or projects.
This is part of a series of articles about AI coding tools.
Here are some of the benefits of refactoring code with AI:
Learn more about generative AI in software development in this article.
Tabnine is a leading AI code assistant trusted by millions of developers and thousands of companies, based on cutting-edge LLM technology. One of Tabnine’s primary uses is automating and enhancing code refactoring.
These are the main features that can assist you and your team with refactoring tasks.
Consider the following cases in which your code requires refactoring on the go. Tabnine can handle the refactoring for you and allow you to move forward with your project:
A common challenge is running into a piece of code you don’t understand and having to refactor it. With Tabnine, you can select or reference the code and ask for an explanation. You can use the information provided to ask Tabnine to refactor the code in the particular way you need.
Following this two-step process — explain and then refactor — also increases the accuracy of Tabnine’s recommendations by adding the code explanation to the context that Tabnine considers when carrying out refactoring.
If you’re joining a new project, or returning to a project that you need a refresher on, you can get a high-level overview of the application by using the Tabnine Onboarding Agent. Type /onboarding in your IDE and reduce your ramp-up time.
The Onboarding Agent streamlines your exploration process, offering you a clear map of the project’s overall structure, key dependencies, and other critical elements like runnable scripts, directly within the development environment. In addition to this summary, the Onboarding Agent suggests follow-up questions, allowing you to dive further into relevant information for your current refactoring task.
Whenever you use Tabnine, it reads your local workspace and uses the information as part of its context, which allows it to provide more personalized recommendations relevant for your work. This local context includes:
There are also more advanced ways to personalize Tabnine:
The Tabnine Protected Model is trained exclusively on permissively licensed open source libraries. This protects your organization from any IP risk, ensuring Tabnine’s AI refactoring recommendations don’t contain code that’s proprietary or that can’t be used in commercial software projects.
Using the Tabnine Protected Model also ensures that your organization’s data is 100% private. When using Tabnine’s proprietary models, Tabnine doesn’t store customer code, doesn’t share customer code or usage data with third parties, and doesn’t use customer code to train our models.
Tabnine offers several deployment options:
Tabnine’s AI refactoring tool supports the most popular programming languages, libraries, and frameworks. You can refactor code in Java, Python, C++, Javascript, and more than 80 of the most popular languages and frameworks are supported.
Languages supported by Tabnine include: JavaScript, TypeScript, Python, Java, C, C++, C#, Go, Php, Ruby, Kotlin/Dart, Rust, React/Vue, HTML 5, CSS, Lua, Perl, and SQL. See a full list of Tabnine supported languages here.
Tabnine also supports the most popular IDEs, so you can use its AI refactoring tools wherever you prefer to code.
Supported IDEs: VS Code, Visual Studio, Eclipse and JetBrains IDEs (including IntelliJ, Android Studio, AppCode, Cion, GoLand, Neovim, PhpStorm, PyCharm, Rider, RubyMine, and Webstorm)
In each of these IDEs, you’ll get full access to local context awareness, model switching, and all the other Tabnine features we mentioned.
You can switch between eight large language models in real time to power Tabnine Chat. Depending on the project you’re refactoring, you may want to pick a different model to give you the right amount of privacy, protection, and performance. When you select a model in your IDE, you’ll see information about the model’s privacy, protection, and performance.
Supported LLM models:
Read also how AI helps in software testing in this article.
A common task for developers is extending existing code or adding on more functionality to an existing application. Consider an example in which a developer needs to modify an existing function to validate input. She needs to check that the data entered about the airline, flight number, destination, and departure date doesn’t have empty values. The validation also needs to make sure that the flight number only contains digits.
The following video shows how this could work. The developer starts by selecting the code she wants to work on, and then uses natural language to communicate with the Tabnine AI chat agent:
Check that an input row is valid before adding it to the list of flights. A valid input has nonempty values, and the flight number is a digit-only string.
Tabnine generates a contextually relevant code recommendation — the developer only needs to click Apply and she has automatically extended her code!
The video below shows how to use Tabnine’s AI chat agents to continue to extend the function from the previous example. The developer can highlight the selected function, then provide Tabine Chat with a prompt explaining how she wants to refactor the function:
read_flights_from_csv should return two values: a list of valid flights, and a list of invalid rows. Then in the main function print how many valid flights and how many invalid flights.
Tabnine generates a code suggestion, and the developer can review the code and simply click Insert to apply the suggested changes.
Now consider an example where a developer needs to switch an existing function to an alternative implementation. At first, it might not be clear what the best implementation is, and the developer might want to consider a few options.
To do this, the developer first highlights the function and then prompts Tabnine to explain the function in detail and propose alternative implementations. Tabnine generates a detailed explanation along with alternate examples. Among other options, it suggests avoiding the use of a loop, and instead using recursion. The developer can easily accept this change and automatically modify the function to use recursion.
Join millions of developers using Tabnine to boost their development productivity while avoiding privacy and compliance risks: