[NEW] Provenance and Attribution: Minimize IP liability for GenAI output
Home / Blog /
AI code refactoring: 7 ways Tabnine transforms refactoring
//

AI code refactoring: 7 ways Tabnine transforms refactoring

//
Tabnine Team /
7 minutes /
July 15, 2024

What is AI code refactoring? 

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.

Benefits of AI code refactoring 

Here are some of the benefits of refactoring code with AI:

  • Increased efficiency: AI-driven refactoring tools can process and analyze large codebases quickly, identifying areas for improvement much faster than human developers.
  • Enhanced code quality: By consistently applying best practices and coding standards, AI refactoring tools help improve the overall quality of the code. They can spot and rectify code smells, redundant code, and other issues that might be overlooked during manual refactoring.
  • Improved maintainability: AI refactoring can be performed on an ongoing basis, ensuring that the codebase remains clean and organized. This improves readability, reduces the learning curve for new team members, and aids in long-term project sustainability.
  • Error reduction: Automated refactoring minimizes the risk of human error that can occur during manual refactoring. AI tools can ensure that refactoring changes do not introduce new bugs, thus maintaining the code’s functional integrity.
  • Consistency across codebases: AI-driven refactoring tools can enforce consistent coding styles and practices across different parts of a large project or across multiple projects within an organization.
  • Cost savings: By reducing the time and effort required for code maintenance, AI refactoring tools can lead to significant cost savings. This allows companies to allocate development resources more effectively, diverting resources from maintenance to innovation.

Learn more about generative AI in software development in this article.

7 transformative features of Tabnine’s AI code refactoring

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.

1. Automatically refactor code for any use case

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:

  • Change of technical method requiring multiple changes in your code: For example, your team might decide to switch to using Axios instead of Fetch for requests.  
  • Complex functions: Modern IDEs warn about code complexity and reworking complex functions can be a chore. Tabnine can automatically simplify your function. 
  • Handling errors: It’s common to write a function that works on the “happy path” but doesn’t handle all possible error scenarios. You can use Tabnine to refactor the function so it properly handles errors thrown. 

2. Explain existing code to plan your refactoring

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. 

3. High-level overview of complex projects 

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. 

4. Personalized AI refactoring recommendations

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: 

  • Your previous questions or instructions
  • The selected block of code from the current file (or the cursor location)
  • Currently open files and other open files
  • Chat history from the current conversation, including all the previous questions and answers
  • Relevant files or code blocks from the current user workspace 
  • Runtime errors
  • Imported libraries
  • Compile/syntax errors
  • Noncode sources of information
  • Current selected code
  • Connected repositories
  • Conversation history
  • Git history
  • Project metadata and other project files

There are also more advanced ways to personalize Tabnine:

  • The enterprise version of Tabnine also takes in the global context from connected repositories. 
  • You can fine-tune your private model of Tabnine to get an even more personalized AI refactoring tool. 

5. Safe refactoring recommendations 

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:

  • A secure SaaS offering (in a multitenant environment or single-tenant environment) 
  • A fully private installation (on-premises or on VPC) to ensure that the code stays within the boundaries of the corporate network and isn’t shared with any external party

6. Support for multiple programming languages and IDEs

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.

7. Use your preferred LLM for AI refactoring

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:

  • Tabnine Protected: Tabnine’s original model, designed to deliver high performance without the risks of intellectual property violations or exposing your code and data to others.
  • Tabnine + Mistral: Built to deliver the highest possible performance while maintaining complete privacy. The model is fully hosted by Tabnine.
  • Codestral: Trained on more than 80 programming languages, Codestral — Mistral’s first-ever code model, demonstrates proficiency in both widely used and less-common languages. This model is recommended for users looking for the best possible performance who are comfortable sharing their data externally and using models trained on any publicly available code.
  • GPT-4o, GPT-4 Turbo, and GPT-3.5 Turbo: The industry’s most popular LLMs, proven to deliver the highest levels of performance for teams willing to share their data externally.
  • Claude 3.5 Sonnet: This model raises the bar for intelligence and outperforms its predecessor, Claude 3 Opus, on a wide range of evaluations, including coding. It operates at twice the speed of Claude 3 Opus. This model is recommended for users looking for the best possible performance who are comfortable sharing their data externally and using models trained on any publicly available code.
  • Command R: Cohere’s Command R model is ideal for large-scale production workloads and balances high efficiency with strong accuracy. This model is recommended for users looking for the best possible performance who are comfortable sharing their data externally and using models trained on any publicly available data.

Read also how AI helps in software testing in this article.

How to use Tabnine’s AI code assistant to extend and refactor your code

Example #1: Extend existing code with new functionality 

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!

Example #2: Add functionality via Chat

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.

Example #3: Refactor by asking for alternative implementations 

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.

 

Get personalized refactoring recommendations with Tabnine: The AI code assistant that you control

Join millions of developers using Tabnine to boost their development productivity while avoiding privacy and compliance risks: