Taking Advantage of IDEA


IDEA, like most modern IDEs, is full of many time-saving features. It behooves you to learn how to master your IDE. The difference between a novice using only the basic features versus an expert taking full advantage of the IDE can be measured in weeks over the lifetime of a one-year project.

Code Completion

One valuable feature is code completion, which I'll also refer to as auto-completion. When you begin typing things that IDEA can recognize, such as class names, package names, and keywords, click Ctrl-Space. IDEA will do one of three things: It may say "No suggestions," it may automatically complete the text you began typing, or it may bring up a list of selections to choose from. You can navigate to the selection using the cursor (arrow) keys and press Enter or you can click on the desired selection. IDEA will finish your typing based on this selection.

Pressing the Enter key results in the new text being inserted at the current location. If you instead press the Tab key, the new text will replace any text immediately to the right.

Get in the habit of pressing Ctrl-Space after typing two or three significant characters. I press it all the time. You may be surprised at what IDEA is able to figure out about your intentions.

IDEA provides two additional kinds of code completion: Smart Type and Class Name. These are more sophisticated tools triggered by alternate keystrokes. Refer to the IDEA help system for more information (HelpHelp Topics).

Navigation

One of the most significant strengths of using an IDE such as IDEA is the ease with which you can navigate through Java code. Click on the Go To menu. The size of this menu suggests how flexible this feature is. Click on the Class selection from the Go To menu. Type the three letters Stu. Pause. You will see a drop-down list of appropriate classes to choose from.

Class navigation is basic but important. More valuable is the ability to navigate from directly within code. Open the StudentTest class. From the line that reads new Student("Jane Doe");, click on the class name Student. Then press the Ctrl-b key combination (b for "browse," I suppose). Pressing Ctrl-b is equivalent to selecting the menu option Go To\Declaration.

IDEA will navigate directly into the Student constructor. You can return to your previous edit location by using the key combination Ctrl-Shift-Backspace. Learning all these navigation shortcuts will save you considerable time. I've watched programmers, even recently, use inferior tools. They waste several minutes every hour in manually navigating through a list of classes and scrolling through large amounts of code.

Searching is another form of navigation. IDEA understands Java and how it all fits together, making searches far more effective. Suppose you want to find all code that invokes a certain method. Using a programmer's editor, you can do only text searches against code. Suppose you're trying to find all client classes that call a method named getId. There might be other classes that implement getId; the text searches will report uses against these classes as well.

But IDEA can use its Java smarts to help you find only the usages you're interested in. As an example: In the Student class, click on the constructor. Right-click and select Find Usages from the context menu. Click OK when presented with the Find Usages dialog. The Find tool window will appear at the bottom of IDEA. As with the Messages tool window, you can double-click an entry in the find results to navigate directly to it.

Refactoring

Another important capability is the built-in refactoring support. IDEA automates a number of refactorings that allow you to quickly and safely transform your code. Take a look at all the entries in the Refactor menu to get an idea of the things you can do.

The simplest and perhaps most valuable refactoring appears firstRename, also effected by Shift-F6. In a few keystrokes, you can safely change the name of a method. This means that IDEA also changes any other code that calls the method to reflect the new name. It also shows you the proposed rename before it applies it to your code. This allows you to back out if you recognize a problem with the potential refactoring.

I use the Rename refactoring all the time. Rather than waste a lot of time coming up with the "perfect" method or class name, I'll often go with a name I know to be inferior. This allows me to immediately get moving. After coding a little, a better name usually suggests itself. A quick Shift-F6 and I'm on my way. Sometimes I might change a method name more than a couple of times before I'm satisfied.

Code Analysis

IDEA supplies you with a code analysis feature known as the code inspector (Analyze\Inspect Code). You execute code inspection on demand against a single source file or the entire project. The inspector looks for problem spots in your code, including things such as unused methods or variables or empty catch blocks. The list of options includes over 300 categorized entries. You pick and choose what youre interested in: individual entries or groups of related entries or both.

The inspection can take a bit of time to execute. When an inspection completes, the inspector presents you with a tree showing all the results. Each result is a detailed explanation of the problem or potential problem in your code. From a detailed explanation, you can click to navigate to the corresponding trouble spot. When possible, IDEA will give you the option to automatically correct the problem.

IDEA may report dozens or hundreds of entries that you may not think are problems. It's up to you and your team to decide the items that are relevant. Some items are not problems at all. But virtually everything on the list can point to the potential for trouble in certain environments or circumstances.

The practice of TDD can mitigate most of these concerns. In Agile Java, you've learned to build things incrementally and not always to an overly robust expectation. You can "get away" with things that would be deemed unacceptable in the absence of good testing.

Relax. Some others might consider the style in Agile Java to be fast and loose. Certainly there is always room for improvement in my code. But I don't worry about it as much when doing TDD.

I consider the bulk of code I come across difficult to understand and maintain. Blindly adhering to standards can seem like a good idea, but often it can lead you to waste considerable time for little gain. If you instead look to the simple design goals of testability, elimination of duplication, and code expressiveness, you will rarely go wrong.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net