-
Notifications
You must be signed in to change notification settings - Fork 38
Home
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. |
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+.
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
andStringBuffer
? 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
- Easier maintenance
- Modernized code base
- Leaner and more compact code base
- Performance improvements
Look at the samples showcasing each refactorings available in AutoRefactor. The samples are extracted from the unit tests suite.
AutoRefactor release is outdated so the current dev state is the best solution:
- Download org.autorefactor.plugin.ui-1.3.0-SNAPSHOT.jar.txt and org.autorefactor.plugin-1.3.0-SNAPSHOT.jar.txt
- Remove the
.txt
extension - Put the files in your
eclipse/dropins
folder - Launch Eclipse
Drag to your running Eclipse workspace to install AutoRefactor: Drag to your running Eclipse workspace to install AutoRefactor
- 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.
For more adventurous users, nightlies (potentially unstable) are available at "http://autorefactor.org/nightly/".
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.
AutoRefactor
, then:
-
Automatic refactoring
will automatically refactor the code -
Choose refactorings...
will allow to choose which refactorings to automatically apply
- 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
- Start to cleanup test code
- Continue the cleanup on code well covered by tests
- Commit the cleanup results in separate commits
- Do not merge automatic cleanups and handy cleanups
- 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
- 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
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.
Now AutoRefactor supports Java 5 -> Java 8 (not Java 9). AutoRefactor runs on Eclipse Mars and above.
How to Contribute
- Pick an issue on GitHub
- Develop automatic refactoring for other languages:
- ESLint for TypeScript, JavaScript...
- C/C++ via the Eclipse CDT DOM or via clang
- Note: LLVM project provides clang-modernize to refactor codebases to use modern constructs allowed by the much improved C++11 and clang-tidy to detect and fix issues in C++ code bases.
- PHP via the Eclipse PDT DOM
- JavaScript via the Eclipse JSDT DOM
- Go like gofix
- Python via the
ast.NodeTransformer
class - C#: Consider contributing to SonarLint for Visual Studio which provides automatic fixes for rule violations
- 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
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."
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