Convert string concatenations into StringBuilder or MessageFormat calls with Eclipse’s Quick Fix

Eclipse sports a really nice Quick Fix that turns string concatenations into the recommended calls to StringBuilder. It saves you the time of creating a variable, having to pull out each of the string parts into a call to StringBuilder’s append and then using the string with toString().

So with one key, it will turn code like this:

System.out.println("The year " + year + " is a leap year.");

Into this:

StringBuilder message = new StringBuilder();
message.append("The year ");
message.append(year);
message.append(" is a leap year.");
System.out.println(message.toString());

Continue reading

Join/split if statements and rearrange expressions using Eclipse Quick Fix

A common cause of bad readability is nested if statements, especially when they could be combined into one if with an AND (&&). This also opens the opportunity to extract private methods to make the if more readable. While you’re at it, you might also want to rearrange the expressions so they make more logical sense.

But making these changes can be tedious and time consuming and often error-prone. It may not happen that frequently, but when it does you want it to go as fast as possible.

Eclipse comes to the rescue with a quick fix that allows you to join or split the statements in one keystroke. And the quick fix allows you to rearrange expressions on the if statement as well.

Continue reading

Switch and close editors faster with the keyboard using Eclipse’s Quick Switch Editor

I spend a huge amount of my programming time in editors, editing, switching between them and closing them. This means that managing editors has to be as efficient as possible, which means that, for one thing, I shouldn’t be swapping between the mouse and keyboard all the time to work with editors.

Luckily, there is a way to manage editors using the keyboard (and it doesn’t involve the laborious Ctrl+F6). It’s a feature called Quick Switch Editor and it makes working with editors a lot more enjoyable.

Quick Switch Editor works by popping up a list of all open editors (sorted by editor title) to the right of the editors tabs. The list is searchable so you don’t have to navigate long lists with the arrow key and searching supports wildcards. On top of that, you can also close editors from the list. Here’s a quick glimpse of what it looks like:

Continue reading

Select entire strings and methods in Eclipse with a single keystroke

Selecting strings is a common thing especially if you want to (a) extract a constant representing it, (b) replace the string with a message bundle key or (c) use it as a search term to look for the same string across the codebase. When refactoring, you’ll sometimes want to delete a method or move it around, requiring you to select the entire method.

The problem is that selecting these can be slow. To select a string you’ll typically move the cursor to the start of the string then select the string using the arrow keys or Ctrl+Shift+Right (select next word) repeatedly until you reach the end of the string which can be slow depending on the number of words in the string. The strategy for methods is similar, move to the start and select but using .

Eclipse has a keyboard shortcut called Select Enclosing Element to make this faster. It allows you to select entire strings from anywhere within the string and methods from anywhere within the method. A bonus is that you do the same thing no matter how big the string/method is.

Continue reading

Automatically format and cleanup code every time you save

Eclipse has a wonderful feature that formats according to your coding standards. It handles things like indentation, where curly braces are placed, if blank lines should occur between field declaration and hundreds of other options.

However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F but (1) you have to do that every time and (2) you have to remember to do it in the first place (and we all know how good developers are at remembering things).

Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free. Continue reading

Move, copy and delete lines with a single keystroke

When coding new features or making big changes to existing functions, I end up moving around a lot of code by typing something, deleting it, copying another line to modify it slightly, moving another line up or down, etc. Deleting a line is also more common than you think.

Instead of selecting lines and using standard copy, paste and delete to copy, move or delete lines, I’d rather use only single keystrokes. Why? Because selecting a line is slow since you have to move the beginning to fully select it. Then you have to copy, move to the new location and paste. Also, it impresses any colleagues watching me code.

Continue reading

How to tweak Eclipse templates to suit you

I discussed templates and why to use them in another post. Basically, we use them to eliminate the pesky, repetitive code zombies. I also showed some built-in templates and what they do.

But what if you want to add your own template or modify an existing one? Managing your own templates is important because your business domain and how you work determines to a large extent what is the common code you use all the time.

Don’t be tempted to say “It takes too long to create the template. In that time I could’ve typed the code myself.” Are you going to say that the next 50 times you have to type the same code? I always force myself to sacrifice the 2 minutes because I’ll save me 20 minutes in the long run.

Continue reading

Speed up boilerplate code with Eclipse templates

Take the following piece of code:

for (int i = 0; i < arguments.length; i++) {
    String argument = arguments[i];
    // ... Do something with argument...
}

How many times have you typed out a similar for-loop, mistyped arguments the 2nd time, made a syntax error by forgetting a semicolon, etc, etc. Same goes for writing a main method or a try-catch block. Even typing an enhanced for-loop in the above example is slow.

To me they’re code zombies. Nobody likes them, everybody wants to get rid of them asap and just when you think you’ve killed one, it wakes up again and doesn’t stop… ever.

This is where templates bash in with modified shotguns and spiked baseball/cricket bats. You use templates to rid yourself of these code zombies, because life’s too short and fast to have to write long and slow code.

Continue reading