Google

Wednesday, July 16, 2008

Top 10 Tips For New Eclipse Users

Need to get up to speed fast with the Eclipse IDE? In this entry, I’m going to share my top 10 list of basic Eclipse tips. These are very simple tips, but I’ve noticed that for some reason many people using Eclipse for the first time end up learning these tips the hard way (through discovery). The following 10 tips should be known by anyone using Eclipse on a day to day basis. In future entries I’ll share some advanced tips - but first the basics.

1 Use Code Assist
Code assist (also known as auto-complete) is the kind of feature that becomes almost completely automatic after a very short time. Most modern IDEs have a similar feature - for instance, Visual Studio calls it Intellisense.
The concept is simple - just press the code assist key combination (by default ctrl-space) and the IDE will either fill in where you’ve started typing or display a menu to allow you to choose an auto-completion from several possibilities.
This is the feature you will use the most in Eclipse. I just noticed that I can type ctrl-space without moving my fingers from the home position at all :).

2 Navigate Through Code By ctrl-Clicking
This is another feature that you stop consciously thinking about once you’ve gotten in the habit. While editing, you can move to a field, method, or class by holding down the ctrl key and clicking on the identifier you’re interested in. This makes navigation dead simple.
When looking through unfamiliar code, this feature is great. If you are wondering what a class is, or what the implementation of a method looks like, it’s as easy as ctrl-clicking it.

3 Quickly Open Classes and Resources by Name
There are two ways to open a class or resource in an editor: the slow way and the fast way :). Don’t use the slow way - there’s no good reason to.
To open a Java class, press ctrl-shift-T. This will display a dialog box that allows you to type in a class name. To open a non-Java resource (a .txt file for instance) use the ctrl-shift-R shortcut. These open boxes support wildcards, partial names, and even camel case names (try typing ctrl-shift-T, AIOOBE to see camel case search in action).
There is hardly ever a reason to use the Package Explorer view to manually pick out a class to open if you know its name.

4 Know the Keyboard Shortcuts
Eclipse has extremely good keyboard shortcut support. Most of the common operations have an associated keyboard shortcut, and learning these keyboard shortcuts is the most effective productivity boost you can give your Eclipse experience.
There are too many keyboard shortcuts to enumerate them all here, and the previous tip already mentioned two of the most useful shortcuts. However, I will list the shortcuts I think are most useful:
ctrl-shift-G: searches for references to a highlighted class, method, field, or variablectrl-shift-F4: closes all open editor windowsctrl-o: outline popup - very useful for quickly jumping to a method in a large classF4: shows the hierarchy viewer for a class (ctrl-T shows similar data in a popup version)ctrl-m: toggle maximize of the current editor or viewctrl-F11: run the last launched configuration
There’s even a keyboard shortcut you can press that will show you all available keyboard shortcuts in the current context: that’s ctrl-shift-L.
It’s worth noting that the keyboard shortcuts can be customized. However, I don’t recommend you do this. It makes pair programming and team collaboration harder because each machine may have a different set of keyboard shortcuts. The shortcuts Eclipse ships with are good defaults - I recommend leaving them that way.

5 Set the Heap Size
Eclipse is a large, complex, Java-based IDE. It needs lots of memory to perform well. In fact, if you’re using Eclipse to do daily development, you should configure your machine so that Eclipse can use RAM in preference to any other processes.
When starting Eclipse, you should always specify a large maximum heap size. The default Eclipse uses is far too small for normal use. There are lots of ways to specify this when starting Eclipse (see Google). The easiest way is to use the -vmargs parameter with the Eclipse native launcher executable:
eclipse -vmargs -Xmx1024M
That syntax will work only on Sun’s JVMs, by the way. And, yes, I do give my IDE 1GB of RAM to work with. My development box has 4GBs so I have plenty to spare :).

6 Configure Eclipse To Use a JDK, not a JRE
Eclipse comes with its own Java compiler, so technically you don’t need to install a Java Development Kit (JDK) from Sun to develop Java programs with Eclipse - a Java Runtime Environment (JRE) works just fine.
However, you’re going to want to install a JDK and configure Eclipse to use it. The reason is that this is the easiest way to get the Java source code into your development environment. Doing this will allow you do set browse into and set breakpoints in Java classes, as well as giving you Javadoc support in the IDE for all the standard library classes and methods.
To configure Eclipse to use a JDK, first make sure you’ve installed a JDK on your machine. Then in Eclipse, go to Window -> Preference -> Java -> Installed JREs, and make sure that the default (checked) JRE points to your JDK installation.

7 Use the Eclipse’s Refactoring Support and Code Generation
Eclipse originally became popular because of its support for various kinds of refactoringsof Java classes. These refactorings are still some of the best of any IDE available today.
Many of the refactorings are somewhat hidden and will show up when performing other operations, but you can get started with refactorings by right-clicking in the Package Explorer and looking at the Refactor and Source menus.
Eclipse also has extensive support for code generation. If you’re writing constructors, getters, or setters by hand then learning to use code generation for this will be a huge time saver. Looking at the Source menu mentioned above will get you started with code generation.

8 Use Multiple Workspaces Effectively
Eclipse supports a concept called workspaces. A workspace is a container of projects that has its own set of preferences and metadata. An installation of Eclipse can work with multiple workspaces - you can tell Eclipse which workspace to use when starting up.
Why are multiple workspaces useful? Many developers create an Eclipse workspace for each version of an application, so doing maintenance work and new development don’t conflict. This maps very naturally to branches in a source control system. Multiple workspaces are also good for separating different projects you may be working on at the same time.
The easiest way to specify which workspace Eclipse uses is to use the -data and -showlocation parameters with the launcher:
eclipse -data c:\myworkspace -showlocation
The -data parameter directs Eclipse to use the “c:\myworkspace” directory on the local machine as the workspace directory. The -showlocation parameter shows the workspace location in the Eclipse title bar - very helpful when running multiple Eclipse instances at the same time.
One note - don’t waste time setting your preferences in each workspace you create. Simply use the Import and Export options under the file menu to export preference from one workspace you’ve already set up into new workspaces you create.

9 Use Templates
Eclipse has two different concepts of templates. I’m not going to talk much about Code Templates, which control what generated code looks like. Instead, I’d like to point your attention to Editor Templates, which are a bit like macros.
For instance, open a Java class in an editor, type in “sysout” and press ctrl-space. An editor template automatically expands that to “System.out.println()” and positions your cursor in the right place. This is probably the simplest example of templates, but they can be much more complex than that.
Get comfortable with templates, and learn the template for each piece of boilerplate code like loops, casts, try/catch blocks, etc. You should never have to write these things by hand if you are comfortable with the templates. Even better, you can create your own parameterized templates for commonly used code constructs.

10 Set Type Filters
You can filter the types that Eclipse uses for auto-completion and when searching for classes. For example, there are two classes called List in Java’s standard library - java.util.List and java.awt.List. If you’re not a Swing, developer, you probably don’t care about java.awt.List and almost always want Eclipse to prefer java.util.List. To do this, you can filter out java.awt.List (even better, java.awt.*).
Filters are configured through Window -> Preferences -> Java -> Appearance -> Type Filters. If you’re seeing classes show up at the top of your search you know you’re not interested in, filter them out for faster searching.

No comments: