Using Keyboard Shortcuts to Cheat Your Way to Good Code in Less Time

Writing good code takes time. We need structure and methods and fields and variables and constructors and constants and design patterns and beans and we need to think about what we're doing. I'm a strong advocate of the 'make it work, then make it better' philosophy, but it can take time restructuring and refactoring code, right?

Wrong.

With the help of our IntelliJ IDEA keyboard shortcuts from the last post, I'm going to demonstrate a few strategies we can use to accelerate some of the more tedious parts of development and refactoring. There are a handful of 'workflows' that I use on a regular basis which are applicable in different scenarios which vastly speed up development and help get good code written in a fraction of the time.

I'll be discussing a few common scenarios and the methods I use to speed up development in these situations.

Scenario 1: A list of things that need to be practically the same / repeating yourself over and over...


Let's imagine we need to end up with code which looks anything like the following:




















It would be pretty tedious writing each field out manually... or even writing one and copying and pasting it and changing it.

Fortunately, we have the amazing multi select shortcut (ALT + SHIFT + CLICK) and column select (HOLD ALT + MOUSE DRAG) which allow us to do some real magic very quickly:




















While having to write code like this might be a questionable design choice; at least there are tools which make dealing with it easier, and these shortcuts are applicable in many other similar scenarios where the code might be better designed.


Scenario 2: You've written a bunch of code and you're missing a lot of imports or have a lot of easily fixable problems.


There are many times when we get carried away and end up writing code pre-emptively and end up with a bunch of errors from missing imports like so:








Using a combination of F2 (go to next issue) and ALT + ENTER (quick fix), it's possible to systematically resolve all issues in a file very quickly:


















This solution scales very well when there are many more issues of a similar nature.

Scenario 3: You need to add some new classes/objects/methods


If you need to create some new code, it's often logical to write the actual code first, and then use/reference it, however working this way around can take some time, and requires moving back and forth between classes, and then referencing things you have already created.

There is a better way, which also complements a TDD approach - we can reference the code first, and use our trusty ALT + ENTER shortcut to create the missing classes and functionality. This is ideal for TDD as we can reference the code in the test and then create it with quick fix - even if you're not writing a test, this approach gets the job done very efficiently. In the example below we are writing some new functionality test-first, before any class exists, and using the shortcut to make it a reality:



























Scenario 4: Refactoring and restructuring code

Let's say you've written some code which works, but isn't exactly pretty or well structured. That's fine, but it'd be nice to tidy it up. With some of the 'extract...' shortcuts, we can restructure and move our code around with great ease. In the example below we are using shortcuts like extract method, extract constant, extract variable, and inline to speed up refactoring:
















With a combination of these techniques, we can speed up refactoring and make sure that the refactoring is done without mistake. With this we can spend more time thinking about awesome new designs and writing more real code instead of spending time moving things around and repeating ourselves.

Happy refactoring!


Disclaimer: These techniques will not magically make your code amazing. They are intended to complement a good design and simply speed up some of the more tedious parts of development.


Comments