Refactoring with IntelliJ

Yesterday I paired with Jim on a refactoring exercise. Whilst there were some substantive coding lessons learned, I also picked up a whole series of IntelliJ tools for refactoring which, thus far, I have been doing manually. So here’s a list:

  1. Moving blocks of code up and down: select text the <cmd> + <shift> + direction.
  2. Moving function level variable to instance variable: <cmd> + <alt> + F.
  3. Reformat code: <cmd> + <alt> + L.

In the exercise I was focussing on making very small commits that ideally did one thing; double checking the changes before committing using git diff; and making clear well formed commit messages. Here are a few example commit messages, which give an idea of the granularity:

Reformats code: reformatting code (using IntelliJ) can make it difficult to track code changes, so it makes sense to have a code reformat in a single commit (so it’s clear nothing has changed except formatting).

Improves test names: again this does a single thing, which can be easily understood from the diff.

Replaces Vector with ArrayList for storing rentals: one substantive change.

I’m not sure whether commits need to be quite so granular on live projects, but the discipline of this kind of micro-committing was really helpful. And certainly exposes many bad habits I’ve gained!

Thinking about what needs to be fixed and trying to fix one thing at a time seems simple, but requires clarity of thought and discipline. The only way I’m going to develop this is by practice. Which is where refactoring exercises can be really helpful. But I also need to try and take this approach with my own code, when more of my energy is taken up by problem solving rather than just refactoring.

Show your support

Clapping shows how much you appreciated Matthew Glover’s story.