Those who know me know that I am a big fan of IntelliJ for Java. There are a few reasons for that, but one of the most important ones is the power of the refactoring tools that this IDE offers. From the simple rename, to inline, to extract X, to automatic static factory method generation and constructor replacement there is a lot to explore. This however will not be yet-another-post™ demonstrating the use of these tools. There are already many such articles and I will link to a few in the end of this post. Instead what I want to talk about is how using these tools has changed the way that I personally code.
One of the most powerful features of all those tools is that undo works with all of them. Rename a private variable? Undo works. Delegate a couple of methods to a collaborator class? Undo works. Replace a constructor that is used across the codebase with a static factory method? You get the idea… The other very powerful feature on offer here is the guarantee that a sizeable subset of these tools are purely doing refactoring and will not change the existing logic in any way.
Of course you are responsible for the changes you make and you should not be blindly trusting a tool, but computers don’t generally do typos when renaming variables and they don’t usually miss required method return points when extracting some lines from a larger method to a new one. Not if their static analyser is any good anyway. More to the point, the computer’s ability to perform these tasks is not affected by the rate of coffee consumption, hours of sleep and proximity to a deadline.
making good use of the tools
For me personally the confidence gained by the use of these tools has transformed the way I code by making me a lot less averse to changes. On the one hand, when I am changing an existing part of the code I am not afraid to spend a little bit of extra time to refactor its structure if I feel it can be improved. On the other, when I am writing something new it is sometimes quite helpful to just do a brain dump to the code and then refactor it to look the way it should after the full implications of the change are obvious. What enables this coding technique is the knowledge that the refactoring tools will make those changes both quick and relatively safe (the other important factor here is a good testing suite, but that is a topic for another day).
For everyone reading this I’d recommend to not only learn how to use those tools, but rather to try and make their use routine to the point where you never do something manually if it can be done automatically with a comparable level of effort. I think you will find that over time this will make those big changes much less stressful affairs. Give it a try and let us know what your impressions are!
For some good tutorials on using the refactoring tools in IntelliJ you can have a look here
If you enjoyed the read, drop us a comment below or share the article, follow us on Twitter or subscribe to our #MetaBeers newsletter. Before you go, grab a PDF of the article, and let us know if it’s time we worked together.