Posted on May 5, 2010 by Byron
When you want to work with comments in Eclipse, you could use the slow way of moving to the start of the line, pressing // and then repeating this for all the lines you have.
Or you could use the quick way of adding a comment with a single keystroke no matter where the cursor’s positioned in the statement.
The same goes for Javadocs – there are just too many things to type before you can start commenting the good stuff. That’s why Eclipse also has a shortcut that let’s you add Javadoc to a field, method or class.
Read more »
Filed under: Editing, Keyboard Shortcuts | 6 Comments »
Posted on April 27, 2010 by Byron
Unused variables and methods should alway be unwelcome. Removing them keeps the code cleaner and easier to read. Now, by default Eclipse warns you about unused private variables and methods, but it doesn’t warn you (by default) about unused method arguments.
But there is a compiler setting in Eclipse that can warn you when you don’t use an argument in a method. You can even handle arguments on inherited methods, especially useful when using 3rd party libraries.
Read more »
Filed under: Preferences, Refactoring | 1 Comment »
Posted on April 19, 2010 by Byron
For readability’s sake, it’s almost always a good idea to replace magic numbers and string literals with constants. That’s all good, but it can take a bit of time to refactor these to constants, especially strings or parts of strings.
For example, in the code below we want to refactor “shovel and spade” to a private static final String called TOOLS. To do that manually would take some time. It goes even slower if we only want to extract “spade” to a constant because we first have to convert the string to a concatenation.
String tools = "shovel and spade";
...
String otherTools = "shovel and spade";
Luckily, Eclipse has a couple of ways to instantly convert literals to constants. Coupled with tools to speed up string selection and to pick out part of a string, you have the ability to create a constant in about 2 seconds flat. I’ll discuss all these features below.
Read more »
Filed under: Editing, Quick Fixes, Refactoring | 3 Comments »
Posted on April 8, 2010 by Byron
If you’ve been hanging onto one Eclipse workspace for the last couple of years, you’ll probably have dozens of projects cluttering up your workspace. If you’re an Eclipse RCP developer, you may be sitting with around 50+ projects easily.
The thing is that you’ll often only work with 1 or 2 at a time, not the whole lot. And sometimes you want a convenient way of only browsing projects belonging to a specific product/feature/layer/any-other-grouping-that-makes-sense. Having all projects in a long list makes it difficult to manage and more difficult to browse.
That’s why the Package Explorer and Project Explorer have a nice feature that reduces the clutter and allows you to organise your project into categories that make sense. So instead of the Package Explorer looking unwieldy and flat like this,

it could look like this:

Read more »
Filed under: Maximise Screen Space, Navigation & Browsing, Views | Leave a Comment »
Posted on March 30, 2010 by Byron
Java has a lot of boilerplate code, although each new version tries to remove a bit more every time. One area particularly affected is when you have to declare and initialise lists, maps or sets (some of it hopefully simplified in Java 7). Here’s some sample code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
...
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
String[] array = new String[] {};
To create any of these, you have to type a lot. Sometimes you make a mistake and have to retype. And half a minute later you’ve got one line of code that just declares a list. Also, you can have Eclipse automatically add imports when you save, but Eclipse normally can’t guess the correct List, instead prompting you to choose between java.util.List and java.awt.List (why isn’t there an option to use the former as the default?).
But Eclipse templates can help you avoid the hassle. Not only will it create the correct import statements, but also remove the effort of typing out the same type in the interface and implementation collection. If you’re new to templates, then see this tip to get an overview.
Read more »
Filed under: Templates | 2 Comments »
Posted on March 24, 2010 by Byron
When working with SVN, CVS or any other repo, there are some things you’ll just do over and over again. Things like committing files, comparing files and viewing a file’s history are all commonplace actions.
So you might be wondering where the keyboard shortcuts for these actions are, because, as I mentioned before, you normally work faster with keyboard shortcuts than jumping between the mouse and keyboard all the time. And often the longest part about working with repos is guiding your mouse through treacherous context menus and submenus.
The good news is that there are keyboard shortcuts in Eclipse for the most common repository commands (and not-so-common as well). The bad news is that most of them aren’t configured by default, so you need to set some of them up beforehand. But the 2 minutes spent configuring these may save you hours in the long run.
I’ll be using SVN as the example repository (using Subclipse) but these should apply to your favourite repository integrated into Eclipse (I know that the in-built CVS has these commands as well).
Read more »
Filed under: Keyboard Shortcuts, Navigation & Browsing, Source Control & Team | 4 Comments »
Posted on March 22, 2010 by Byron
Once you’ve configured Eclipse preferences to your heart’s content, you’ll often want to share those preferences across multiple workspaces. Now normally you can go to File > Export > General > Preferences to save your preferences to a properties file which you can then import into the other workspace. This will share settings such as your customised keyboard shortcuts, formatting, repository settings, etc.
But, for some reason, Eclipse doesn’t save perspective/window layouts, such as which views are open and where they are placed in the perspective. So you’ll find yourself spending another half hour configuring the window to the way you like it. After the 3rd workspace you need to create, this becomes frustrating and just wastes time.
Fortunately there are ways to save and restore these settings automatically. The first is to save the perspective into the preferences and the other is to use Eclipse’s Copy Settings feature when opening the other workspace. I prefer the first option, but I’ll mention the second option and when to use the one over the other.
Read more »
Filed under: Installation & Setup, Preferences, Views, Window & Toolbar | 10 Comments »