One of my friends said once:
I don’t like IDEs. They help to write verbose code. In real life 90% of time I spend on code reading. Verbose code kills my productivity
I think he’s wrong. The reality is that IDEs are used not only for writing/generating code, but to a greater extent IDEs are used to read and analyze the code. You might be a hard-core Emacs/Vim ninja but still, there are things that you just cannot do with the text editors.
Navigation through the source code is one of the activities that programmer executes on a regular basis. Basically, it is not hard to navigate the code with Emacs or Vim, but it would limit you only to a basic navigation options. IDEs take navigation abilities one step further.
Let’s see what are the navigation features in IntelliJ. First of all, “Open Type” which is executed via Ctrl+N:
The same can be achieved with Ctrl+Shift+T in Eclipse and the behavior is exactly the same.
Ctrl+Shift+N will help you to open any resource. Eclipse does the same with Ctrl+Shift+R.
And it could have been all but there’s more – Ctrl+Shift+Alt+N will find you all the appearances of a specific symbol, e.g. a method name:
It doesn’t mean IntelliJ will scan the code for the occurrence of the specific symbols but it rather will match the code structure and look for the elements in your code so you will get a list of method declarations but not just any appearance of the method name in the code. This is quite useful if your classes are not sharing the same hierarchy.
Often when you navigate the source code you jump over class hierarchies, browse different methods, open type declarations, etc. It is quite common that once in a while you would like to step several steps back in your navigation and proceed with some other direction. Ctrl+Alt+(left|right) arrow is a very useful shortcut for such purpose. Whenever a caret hits any place in the code this place is remembered and you can go back and forward in the your browsing history. This is quite cool as if you navigate a large code base you might not even remember which classes you actually opened just a second ago.
Actually you can get a list of the recently visited files by pressing Ctrl+E – also quite often used to reopen the recently closed files.
Navigating back in forward is the same in Eclipse (with Alt+(left|right)arrow shortcuts). But I failed to find a list of recently edited files there, while this can be done in IntelliJ by hitting Ctrl+Shift+E shortcut.
There are plenty of other feature that make your code browsing experience more pleasant. For instance, both IntelliJ and Eclipse provide a feature to quickly browse the location of the currently opened class – Alt+Home will popup a navigation bar with the path to the class.
The cool feature in IntelliJ for this navigation par is that it is possible to brows the content of any file with Ctrl+Shift+I without having to really open the file:
In Eclipse a similar feature is called “Browse in Breadcrumb” called via Alt+Shift+B. The difference is that in Eclipse it works only if a Java type is opened and it acts only as a navigation bar – no quick preview available. Also, I didn’t find an easy way how to get rid of that “Breadcrumb” in Eclipse besides clicking a dedicated button on the toolbar.
Ctrl+Shift+I also works in IntelliJ for any symbol that you’d probably like to inspect. For instance, you may press this shortcut on any method call, and it will raise a popup that displays the methods source code:
Also, if you try to browse the code that maybe involved in the type hierarchy, it is possible to select the implementation by choosing the type in the very same quick view popup:
Talking about navigation on the hierarchy of classes, IntelliJ implements that also quite nicely – quick hierarchy view is available the same way as in Eclipse. The feature that distinguishes IntelliJ is the ability to easily navigate to implementation. For instance, from interface method declaration to any of the implementations. If there are multiple implementations of an interface method Ctrl+Alt+B will call a popup dialog with the list of implementations:
“Find Usages” functionality is something that always made me nervous in Eclipse. Ctrl+Shift+G (find references in workspace) alone is ridiculously unusable. I mean, fine, you can find all the references to a method in the workspace with a single shortcut, but to do the same in a project scope you have to touch the mouse.
Here’s what you have in IntelliJ. First of all Alt+F7 would give you almost the same as Gtrl+Shift+G in Eclipse besides that you don’t have to touch the mouse in order to define scope of the lookup. Arrow down will give you a choice of scopes available for the reference loopup, the hit Enter and you’re done. Even such simple feature is much usable in IntelliJ.
Besides that a quick lookup is also available – with Ctrl+Alt+F7 you will get a popup with the list of the references for a method of interest and directly from there you can use the Ctrl+Shift+I (mentioned before) to view the sources code in which the method is used:
Marking the occurrences of a variable works the same way in IntelliJ and Eclipse, besides that in Eclipse you can toggle the highligh and in IntelliJ you press a shortcut (Ctrl+Shift+F7) but no essential difference in the result for me. In IntelliJ, though, the highlight works on multiple variables while in Eclipse only on a single one.
Navigating through the code is easier if there are means to bookmark some places in the code in order to be able return back to the location of interest. It seems that IntelliJ developers think of it as a priority as all the Ctrl+Shift+# and Ctrl+# shortcuts are reserved for bookmarking needs.
Ctrl+Shift+# (e.g Ctrl+Shift+1) sets a quick bookmark on the line where the cursor is located. After that you can return back to the bookmarked location by pressing Ctrl+# (e.g. Ctrl+1). The character symbols can also be used for bookmarking the locations but without quick access to the location afterwards – a special menu must be used. The screencast below demonstrates this feature:
In Eclipse, the bookmarking functionality is also available, but it lacks same level of usability as in IntelliJ – no shortcuts by default and not that simple to use without the mouse.
Both the IDEs, IntelliJ and Eclipse, keep track of local changes and provide local history view where you can browse the changes you’ve made. The feature is somewhat similar to using VCS, but without VCS.
At first it seems that the feature is implemented the same way in IntelliJ and Eclipse but if you dig deeper and start really using it – you’ll notice the difference. Again, IntelliJ takes the feature a bit further in respect to usability (but I’m quite biased on this).
Specially the diff-view, is quite intuitive for this feature – you can clearly see what are the changes and easily migrate ’em. In Eclipse, the view is really similar but when I tested it – added new methods to a class, introduced new statements – I could not find those changes afterwards although an entry was created in the history view. In IntelliJ’s view you can create a patch or revert to a desired version.
Smart Code Completion
Typing is just tying, right? It is actually amazing how intelligent cat the IDE be for this purpose. Isn’t it cool when the IDE can autocomplete your statements just as you wanted?
Here’s a simple example. I’d like to introduce a field in a class – I’ll type List<String> list = new .. and hit Ctrl+Space then. And what does Eclipse suggests me? List and a name of the class I’m declaring this field in. Not really intelligent, uh?
In IntelliJ, after tying the same line of code, and pressing Ctrl+Space the popup gives me a list of List implementations available in classpath. Also, Ctrl+Shift+Space tries to be even smarter and proposes me ArrayList as a preferred choice.
Another cool autocomplete behavior I discovered in IntelliJ is that if you try smart type completion twice (Ctrl+Shift+Space) then IntelliJ will search for chained expressions which have expected type.
Templates is the other feature that helps you to write code fast. Usually an IDE provides you a set of templates but you’re allowed to add new templates also. Let’s see how it is done in IntelliJ.
Ctrl+J will popup a list of all the templates available and can be filtered by the template name. In case if you have just a few templates in the list it is not hard to find a correct one. But in case the list is too long it would be nice to have some other means for filtering the list.
Just a coupe use cases for the templates:
It is quite common to check the method parameter for null equality. The corresponding if-statement can be generated with inn template. So by typing “inn” in the editor and pressing the expansion key (TAB by default) we get the null-check-if-statement.
Next, quite common piece of code, is iteration over a collection of elements. IntelliJ provides a number of iteration templates for various types of loops. The common thing among the iteration templates is that all they start with “it” – iter, itco, itar, etc. So for instance we have a collection of elements and by typing iter and the expansion key we’ll generate a foreach loop iterating the collection in context.
Templates could also be used for surrounding statements in case it is applicable. Ctrl+Alt+T does the trick. For instance, id like to surround the objects retrieval with try/catch block but I’ve already managed to generate a loop iterating the resulting collection:
Another template that I would mess in Eclipse by default is soutv that expands to a System.out.println(…) statement with a variable in context:
Editing and creating the templates is quite straightforward and well-documented. Usually I define my own try/catch template as it is not provided out-of-the box (which is strange).
The unique feature of the templates in IntelliJ is that you can define new templates that are backed with available features from the IDE, e.g smart code completion and other useful functions.
I think I’ll skip the refactoring part at large – this is just insane to compare the huge amount of refactorings available in the IDEs. Only one note on the usability for a simple refactoring use case – extract local variable/method/etc. In Eclipse you have to select the full statement before you press Alt+Shift+L (or position the cursor just after the expression), otherwise the IDE refused to understand what do you want to do popping up a message “An expression must be selected to activate this refactoring”.
IntelliJ seems to be a bit more intelligent still detecting the context and providing you options for the same refactoring:
More to come, stay tuned :)
- What’s Cool In IntelliJIDEA. Part II: Live Templates
- What’s Cool In IntelliJIDEA. Part III: External Tools
- Eclipse 3.6 vs IntelliJ IDEA 10.5: Pros and Cons
- Eclipse Shortcuts for Increased Productivity
- Eclipse Memory Analyzer (MAT)
- What to do with IDE project files
- Eclipse: How attach Java source
- Java Tutorials and Android Tutorials list