Friday, 20 April 2007

Don't write code - use Eclipse

Eclipse IDE provides multiple functionalities which can help you when you are coding. The functionalities what I am talking about are typically very small and easy to use actions such as Content Assistant, Quick Fix, Refactoring and other code manipulations. Usage of those features can improve speed and quality of your coding.

In this article and also in the other articles under label coding tips I would like to describe usage of those features in the way where you will write as little code as possible and the rest of code will be generated by Eclipse.

Let start with very simple example - assignment of new instance of StringBuffer class to the local variable or to the field.

So what we want to have at the and will be something like:

StringBuffer buffer = new StringBuffer();

  • write keyword 'new'
  • write 'S' and push Ctrl+Space (shortcut for Content Assistant)
    you can see list of classes, interfaces and templates starting with 'S'
  • you can specify more accurately the name of requested class writing its following letters 'tr' or using Camel feature - just write 'B' as a Buffer. Then navigate selection to StringBuffer class.
  • press Enter. The name of the class is generated.
  • write '(' as an open parenthesis (the closing parenthesis is generated automatically) for constructor parameters and and push Ctrl+Space again.
  • select constructor you want to use and press Enter

  • write down the parameter values for the constructor, press End button, add semicolon ';' and save (off course using Ctr+S shortcut).
  • press Ctrl+1 (cursor should be somewhere on the line code). It is shortcut for Quick Fix proposal.

  • choose one of the proposals and press Enter. The Eclipse then generates all necessary code to have it compile able.
    For more advanced usage you can use direct shortcuts for proposed actions (you can see them in the proposal) - Ctrl+2, L for assignment to local variable and Ctrl+2, F for assignment to field.
    It also provides you possibility to give a name of created variable or field.

  • When you are sure about the name of the variable press Enter. It will offer you to specify type of variable in more detail. Of course the proposed types are super types of the type you instantiated.

  • When you will choose the right type press siply Enter again and once more for finalization.

Now we have what we wanted (55 characters) using 28 key presses.

AddThis Feed Button 7 comments:

Anonymous said...

It isn't just 28 keystrokes. It's 28 keystrokes and the mental and physical demands of dealing with the constant pop-up list boxes.

The kind of code you've written is not the kind of stuff that requires concentration. A good touch-typist is probably just as well-off typing the line of code as he is using the autocomplete and clicking stuff with the mouse.

Nothing wrong with using it of course, but don't pretend it's really any "better". It just depends on personal preference.

willCode4Beer said...

You can even do it in 11 keystrokes:
'type arguments'

Oliver said...

Nice little introduction.

I didn't know about the CamelCase feature of the Code Assistant. Why didn't I notice that?

* starts Eclipse

Magnus said...

Cool, so now were getting the (ineffecient) StringBuffer class used instead of StringBuilder 99% of the time.

Woohoo! progress.

Pack said...

I have been programming for over 18 years. I find using auto completions most useful when i am not sure about the method name or if it is too long.

Saving a keystroke for "new" is just plain amateurish. I have the statement constructed in my mind before I type and therefore it takes hardly any time to type it.
I touch type and that could be one reason i find it not worthwhile to use all the bells and whistles all the time....

Anonymous said...

Personally, I /hate/ when editors add closing characters for me. It's easier to get to ')', '}', ']', etc., than it is to get to the left arrow key or End keys. I much prefer doing this myself. I arrived here looking for ways to configure Eclipse to /not/ do it. I can't seem to find one...

Roman B. said...

Try Window > Preferences > Java > Editor > Typing ;-)
There is also new functionality in Eclipse from version 3.4 (which was released after I published the post) which adds semicollon to the right place event if you pushes it in the middle of the line. It is possible to configure it on the same Preference page.