Here I’m going to talk specifically about how to harness the power of Android Studio’s keyboard shortcuts, refactoring options and live templates to allow you to navigate and refactor the code as fast as you can think about it. When you can do this effectively, refactoring, renaming and improving the code has close to zero friction. Without friction, you will start to notice these things happen automatically and continuously. With friction, you see something you want to refactor and think to yourself, “This will interrupt my flow; I’ll coming back to it later.” And how many times do you actually go back to it later? So let’s lower the friction and enable opportunistic refactoring of our codebase.
Jetbrains’ IDEs (including Resharper) are arguably the best around. It’s easy to dismiss the power contained within them if you haven’t fully experienced it. (I’m looking at you XCode users.) There are so many shortcuts and tools that I’m not going to attempt to cover them all here, but I will highlight some I use most often and provide me the most value in my daily work.
As developers, we live in a fast-paced environment and must always look for ways to build software better and faster than we did yesterday.
Let’s explore an opportunity for making big leaps in both of these areas by embracing better tools and mastering them, inside and out. Like the term or not, this helps us get closer to the fabled “10x” level of productivity.
Note: all the shortcuts I discuss here are the default Android Studio shortcuts for Mac OS X. I also recommend disabling the Mac shortcut keys that override the F* keys, making many of the following shortcuts easier to work with.
Our goals here are to increase speed and accuracy. The first step to achieving speed is to stop touching the mouse. This first group of shortcuts is based on simple operations that you probably do a lot, but if you do it with the mouse, you’re spending 10x longer getting there.
Compile java sources: SHF+CMD+F9
Build the project: CMD+F9
Run the current configuration: CTR+R
Run in debugger: CTR+D
Open project properties: CMD+;
Open Android Studio preferences: CMD+,
Find any command: SHF+CMD+A
Auto-format code: OPT+CMD+L
Delete line: CMD+DELETE
Duplicate line/selection: CMD+D
Copy line: CMD+C (with nothing selected)
Select next occurrence(s): CTR+G
Scope based selection
Another important way to avoid having to touch the mouse and speed up your workflow is improving text selection. A large portion of the time when you are selecting text, you are trying to select it as a valid unit. Trying to find the right parenthesis to select when many things are nested is difficult. This shortcut makes it easier and faster.
Select next higher scope: OPT+UP
Select next lower scope: OPT+DOWN
Our next goal is to identify some existing code where we want to perform simple refactorings, and do it quickly and accurately without skipping a beat. With the cursor in the scope of the change we want to make you can perform the following with a single keystroke:
Extract Field: OPT+CMD+F
Extract Parameter: OPT+CMD+P
Extract Constant: OPT+CMD+C
Extract Method: OPT+CMD+M
Navigating the code
A large amount of our time is spent trying to understand the existing code base before making a change. If we can make navigating in and out of different areas of the code base frictionless, then we can maintain context and speed.
Open class: CMD+O
Open file: SHF+CMD+O
Find symbol: OPT+CMD+O
View implementation of symbol:CMD+B
View implementation (if interface or abstract): OPT+CMD+B
Find all usages of a symbol: OPT+F7
Navigate back to last position: CMD+[
Navigate forward to previous position: CMD+]
Switch to recently used files: CRT+TAB
Unfortunately, we spend a lot of time writing the same type of code over and over again. We try to keep things DRY, but at the end of the day, repeating code is unavoidable in a lot of situations. For these, assistance is provided in a few different ways:
Intention Actions (aka Mindreading Actions)
If it’s not already the case, OPT+RETURN will become your best friend once you realize its power. It basically reads your mind and suggests things you probably want to do in the context of where the cursor position is. Especially if it’s on red, underlined or highlighted text.
If/Switch actions: OPT+RETURN
And if you get in the habit of writing the code that makes use of something before it actually exists, Android Studio will have enough information to create the stubs for it with just a keystroke. This goes for almost anything you can think of. Classes, public methods, private methods, fields, variables, parameters and so on. And it will be smart about the types being used when generating code.
Create method: (one of many examples)
There are so many of these context-aware actions that I can’t even begin to scratch the surface here. I recommend that you put the cursor in lots of different places where your intentions could be inferred and then press OPT+RETURN to see what you can do.
Auto-complete override methods
Anytime you are in a subclass that has methods available to override, you can just start typing the name of the method and select from the IntelliSense popup to auto generate a method stub for it.
Android Studio Live Templates
For each loop: “[variable name].for”+TAB
For i loop: “[variable name].fori”+TAB
For i loop (reversed): “[variable name].forr”+TAB
Generate debug log statement: “logd”+TAB
Generate error log statement: “loge”+TAB
Generate info log statement: “logi”+TAB
Generate TAG declaration: “logt”+TAB
Generate parameter logging: “logm”+TAB
Generate method return log: “logr”+TAB
On and on and on…
Creating newInstance() method with arguments: “newInstance”+TAB
Create a Toast statement: “Toast”+TAB
Add layout_height=”wrap_content” to xml widget: “lhw”+TAB
I could go on for days here. I encourage you to open the Android Studio Preferences and explore the Live Templates section to see everything that is possible. Don’t see one you want? Fear not, you can create your own as well!
Own your tools
The moral of the story is: if you aren’t taking advantage of all that your tools make available to you, then you are leaving a barrier between thought and implementation. Removing barriers not only make you faster but better able to express yourself in the code. Like any profession where you rely on tools to excel (musicians, carpenters, surgeons, etc), it’s about knowing the tools so well that they become an extension of you, enabling a direct path from thought to results.
Like any profession that relies on tools, it also takes practice. It’ll be hard at first, but force yourself to stop touching the mouse and stop typing things out the hard way, and use these shortcuts. The more you do, the easier it gets, and you’ll quickly blow away your previous levels of productivity.