Skip to content
Fabrice TIERCELIN edited this page Apr 6, 2021 · 4 revisions

AutoRefactor: Disambiguate your code

AutoRefactor is no longer planned to be released, it is planned to be merged in Eclipse. So update your Eclipse or install the development version of AutoRefactor.

Table of Contents

About

The AutoRefactor project delivers free software that automatically refactor code bases.

The aim is to fix language/API usage in order to deliver smaller, more maintainable and more expressive code bases.

The first software will be an Eclipse plugin to automatically refactor Java code bases. License will be dual licensed under EPLv1.0 and GPLv3+.

Why?

After years working in different large code bases, I (Jean-Noël Rouvignac) got tired of spending too much time applying the same code cleanups again and again. In addition, tools like SonarQube™ (providing their own Java analyzer and also supporting the aggregation of FindBugs™, Checkstyle and PMD) are fantastic, but they do not not help fix the thousand warnings you can find in legacy code bases.

I decided I had spent too much time on manually doing these changes, so I tried using regular expressions. They work great for some code transforms, but can only do so much: they do not understand the language semantics, reapplying them again and again consumes time, they sometimes fail to execute due to too much complexity. I then sought for better and libre solutions, but found none. This is when I started AutoRefactor.

Software rots, particularly if the developers in charge do not care enough. Software has to be nurtured like a garden. Other causes for software rot are:

  • Developers lack of knowledge of the language in use and its features/idioms When should you use String, StringBuilder and StringBuffer? I mean it. It's harder than you think.
  • Developers habits coming from other languages (any C programmers around?)
  • New language features get added, but the code base lags behind. It creates orphan beliefs
  • Incomplete automatic changes are applied
  • Short deadlines, lack of investment in the software, lack of stakeholder support for reducing the technical debt

Goals

  • Easier maintenance
  • Modernized code base
  • Leaner and more compact code base
  • Performance improvements

Features

Look at the samples showcasing each refactorings available in AutoRefactor. The samples are extracted from the unit tests suite.

Install

The development version

AutoRefactor release is outdated so the current dev state is the best solution:

  1. Download org.autorefactor.plugin.ui-1.3.0-SNAPSHOT.jar.txt and org.autorefactor.plugin-1.3.0-SNAPSHOT.jar.txt
  2. Remove the .txt extension
  3. Put the files in your eclipse/dropins folder
  4. Launch Eclipse

From the Eclipse marketplace

Drag to your running Eclipse workspace to install AutoRefactor: Drag to your running Eclipse workspace to install AutoRefactor

From AutoRefactor update site

stable release
  • Go to Menu > Help > Install New Software...
  • Click the Add... button on the right
  • Enter "AutoRefactor Releases" in the Name field
  • Enter "http://autorefactor.org/releases/" in the Location field
  • Select Programming Languages > AutoRefactor
  • Click the Next > button at the bottom
  • Click the Next button at the bottom
  • Accept the terms of the license agreement
  • Click the Finish button at the bottom.
Nightly (maybe unstable)

For more adventurous users, nightlies (potentially unstable) are available at "http://autorefactor.org/nightly/".

Usage

There are several ways to use AutoRefactor:

  • Open a Java file, then type Shift+Alt+Y to automatically refactor the code
  • In the package explorer, you can also select one or several Java files, packages or Java projects and type Shift+Alt+Y. The AutoRefactor plugin will apply the refactorings to all the selected Java files, or to the Java files located under the selected packages or under the Java projects.
Alternatively, you can open a Java file, or select one or several files, packages or Java project, then right-click > AutoRefactor, then:
  • Automatic refactoring will automatically refactor the code
  • Choose refactorings... will allow to choose which refactorings to automatically apply
You can see which changes have been applied in the "Git Perspective" (git) or the "Team Synchronizing" perspective (CVS, Subversion). You can also see which changes have been applied by looking at individual files Local history, and comparing the last revision with the previous revision.

Best practices

Choose the right time
  • Wait for the frozen zones
  • Prefer the beginning of coding iteration
  • Cleanup the code just before reviewing or testing
  • Cleanup the code after creating a release branch or before creating an epic branch
Cleanup the right code
  • Start to cleanup test code
  • Continue the cleanup on code well covered by tests
Do the right commits
  • Commit the cleanup results in separate commits
  • Do not merge automatic cleanups and handy cleanups
Choose the right rules
  • Start to cleanup with the less dangerous rules; avoid the rules with "may change the behavior" in their description
  • Run the remaining rules; as most of the code has been cleaned up, it becomes easy to review
  • Reapply all the rules as some cleanups can become available after the previous cleanups
You can also organize a weekly 'cleanup-learning. You choose a rule, you clean the code, you push and then you gather your team to explain the best practice the rule is related to. It will:
  • Improve your knowledge
  • Improve your code
  • Use the code as example
  • Avoid bad copy/paste from bad pattern
  • Ease any merge conflicts as people know exactly what have been changed
Developers are responsible for what they commit.

Since each commit should have a single purpose, it is recommended to separate code refactorings from features/bug fixes, and commit them separately from each other. See Martin Fowler's Preparatory refactoring from Workflows of Refactoring.

News

2018-06-30 - v1.2.0 is out!

Now AutoRefactor supports Java 5 -> Java 8 (not Java 9). AutoRefactor runs on Eclipse Mars and above.

Contribute

How to Contribute

Ideas

Links

Tools
  • Eclipse Java development tools (JDT), the base for this plugin
  • UCDetector: Unnecessary Code Detector, eclipse plugin that finds unnecessary (dead) public java code; suggests to the reduce visibility of methods, fields, classes; and make fields final. It offers to fix the reported issues in a batch mode.
  • FindBugs™ and fb-contrib™, bytecode-based software flaw finder for Java programs
  • Checkstyle, coding conventions checker for Java programs
  • PMD, source-based software flaw finder for Java programs
  • SonarQube™ provides its own Java static analyzer and also support the aggregation of the results of the precedent three software in a unified and coherent interface. It adds code coverage measurement and computes a lot of useful source code metrics. It is very useful for managing the health of source code and technical debt.
  • walkmod, a tool for Java source code that share common goals with AutoRefactor
  • Coccinelle, a fantastic source of inspiration for automated source-to-source transformations
    • Pfff, a continuation of Coccinelle that can be used with several languages
Library

Find here several books inspiring everyday how and why this plugin is written:

  • The book Refactoring: Improving the Design of Existing Code by Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts and refactoring.com, its companion website
  • The book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
  • The quote
"Programming is the art of telling another human being what one wants the computer to do."
by Donald Knuth
Trademarks

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Creative Commons Attribution-ShareAlike 3.0 logo This website is (C) 2013-2018 The AutoRefactor project and is licensed under the terms of the Creative Commons Attribution-ShareAlike 3.0