Feeds:
Posts
Comments

Taming large methods

Next to using a chair and a whip, one of the most effective ways to tame large, complicated, methods that do many things is to refactor them into smaller more manageable methods that each do a single thing. Eclipse offers an “Extract Method” refactoring for this case. You can select a block of code that you want to extract into its own method and then right-click and select Refactor > Extract Method or use they keystroke Ctrl-Alt-M. You’ll be prompted with a dialog box that lets you name your new method, assign an access modifier (private is the default) and play with the parameters. You can then preview or accept your changes and the new method will be placed directly following the method it was extracted from.

Today’s tip:

Ctrl-Alt-M: extract method refactoring

Sometimes I wish that my IDE were smart enough to write my code for me by having a keystroke for “fix bug” or “imagine what customer wants and implement it… elegantly”. Eclipse can’t quite do that but it does have a wicked-cool keystroke in Ctrl-1, which depending on the context can suggest some pretty intelligent things.

For example, if you have written a method call and you highlight it and press Ctrl-1, Eclipse will give you options to extract to local variable, assign to new local variable or assign to new field – pretty cool. Or, if you’ve modified a method in a superclass and you highlight the subclass’ name and hit Ctrl-1, you’ll get options to add unimplemented methods or make the subclass abstract. Or suppose you’ve misspelled “equipment”, you can highlight it, press Ctrl-1 and you’ll get an option to correct the spelling. It’s like it’s doing the thinking for you! Finally, you can get that drinking bird to cover for you while you go out to a matinee.

Today’s tip:

Ctrl-1: quick fix, ie. context sensitive suggestions for all your editing needs

Check your references

Got a misbehaving class? Need to find out who’s using it? Ctrl-Shift-G and Ctrl-Alt-G are on your side. Ctrl-Shift-G is used to find all references to a particular symbol in the workspace and it’s also activated by right-clicking in the editor and selecting References > Workspace. Ctrl-Alt-G is textual search for the highlighted word in the workspace. Both are handy so you should probably learn to use them.

These two are good if you’re in a jam, but if you’ve got serious trouble you’ll need to look somewhere else:

In 1972, a crack commando unit was sent to prison by a military court for a crime they didn’t commit. These men promptly escaped from a maximum security stockade to the Los Angeles underground. Today, still wanted by the government, they survive as soldiers of fortune. If you have a problem, if no one else can help, and if you can find them, maybe you can hire… The A-Team. 

Today’s tips:

Ctrl-Shift-G: finds references in workspace

Ctrl-Alt-G: find text in workspace

Can anyone comment on this?

I’ve thought about this for some time and have come to the conclusion that commented out code is evil. Sometimes it’s left there because someone’s hinting at alternative solutions in an attempt to be helpful or as a piece of historical antiquity for future generations to puzzle over but most of the time it serves as confusing misdirection, much like road signs in Ireland.

However, there are times when you need to quickly comment out a block of code (hopefully with the intention of uncommenting it later). For those cases, Ctrl-Shift-C does the trick and it can work on single or multi-line blocks of code. Just don’t turn into a comment-toggling fool. Comment goes on, comment goes off, comment goes on…

Today’s tip:

Ctrl-Shift-C: toggle comment

Run, Forrest, run!

The first time I saw Forrest Gump I thought it was a great movie. I was particularly impressed with Tom Hanks’ portrayal of the mentally retarded Gump. Years later, I had the misfortune of watching The Terminal in which Hanks revises his Gump role, this time as a mentally retarded traveller/interior decorator who gets stuck in an airport due to a quirk of international law. Only, in this airport, they don’t taser you if you can’t just calm down and speak english within 30 seconds. Where am I going with this? I don’t know. This whole tasering situation has got me really upset.

Back to the tips… When you’ve got a file open that is “runnable”, you can hit the run lrun.gif or debug ldebug.gif icons in the toolbar and Eclipse will launch it in whatever way is most appropriate, i.e. as a java application (if it has a main method) or as a JUnit test (if it has tests defined in it), etc. And if Eclipse can’t figure it out because there are multiple ways the file could be run, it’ll prompt you. And if the file is not runnable in any commonsense way, Eclipse will re-run the last thing you ran. But wait, there’s more! You can avoid the laborious mouse-click by using the F11 or Ctrl-F11 keystrokes to debug or run. Now, how much would you expect to pay for all this functionality? $100? $50? For the low-low price of Free, it can be yours if you call right now.

Today’s tips:

F11: debug

Ctrl-F11: run

Did you know that Sting used to be named Gordon Sumner, that John Denver was Henry John Deutschendorf or that Michael J. Fox was Michael A. Fox? Name changes are uncommon when we’re talking about people (except in the entertainment industry, apparently) but they’re very common when we’re talking about code.

Renaming is one of the most common refactorings I use, probably because naming things is hard but also possibly because I am fickle. In any case Eclipse provides a handy refactoring shortcut for renaming things. Try highlighting a symbol in an editor, say a class name, and hit Alt-Shift-R. You’ll be prompted to type a new name and then hit Enter at which point Eclipse will show you a preview of all the changes it’s going to make – any of which you can accept or reject. The same shortcut can be used on files and packages in the Package Exporer view.

Today’s tip:

Alt-Shift-R: rename refactoring

List all shortcuts

At the risk of rendering this blog superfluous, Ctrl-Shift-L is the shortcut that lists all shortcuts – a meta-shortcut if you will. Type it and it opens a scrollable table in the bottom right hand corner of the workbench. Shortcuts are listed in alphabetical order according to the rather intuitive names given them by the Eclipse team. For example, to find the shortcut that transforms all selected text to lower case, you’d look for “To Lower Case”.

Today’s tip:

Ctrl-Shift-L: pops up a helpful list of all shortcuts

Kills lines… dead

I’ve become so accustomed to using this keystroke that I now constantly type it in applications that do not support it, then I become angry that they don’t. Anger leads to hate, hate leads to suffering and the next thing I know, I imagine reaching for a lightsaber (you know, the one that says “Bad Mother F***er”) to slice my monitor in half.

Ctrl-D might be the ‘Bad Mother F***er’ of keystrokes but it’s not as bad as Keyser Soze:

He lets the last Hungarian go. He waits until his wife and kids are in the ground and then he goes after the rest of the mob. He kills their kids, he kills their wives, he kills their parents and their parents’ friends. He burns down the houses they live in and the stores they work in, he kills people that owe them money. And like that he was gone. Underground. Nobody has ever seen him since. He becomes a myth, a spook story that criminals tell their kids at night. “Rat on your pop, and Keyser Soze will get you.” And no-one ever really believes.

Today’s tip:

Ctrl-D: kills line

Got stacktrace? Jump to an arbitrary line in an editor with Ctrl-L. ‘Nuff said.

On a related note, I like to turn line numbers on in my editor (kinda makes me feel grounded). Go to Window > Preferences > General > Editors > Text Editors … phew… and click “Show line numbers”. Or don’t. Nobody’s got a gun to your head.

Today’s tips:

Ctrl-L: goto line

Window > Preferences > General > Editors > Text Editors and click “Show line numbers”: to, well, show line numbers in the left side of your editor

Mark ye all occurances

Some classes and methods are long and sometimes searching for a variable in them feels like you’re reading a Where’s Waldo book, which apparently is less annoying than playing the Where’s Waldo game. Turn on “Mark Occurrances” and Eclipse will “mark” all “occurances” (get it?) of the currently highlighted symbol by highlighting them throughout the file and by indicating their locations on the right side of the editor. You can toggle mark occurances either on the toolbar, it’s the icon that looks like the tip of a highlighter highlight.gif, or by using the Alt-Shift-O keystroke.

Today’s tip: 

Alt-Shift-O – toggles Mark Occurances

« Newer Posts - Older Posts »