Sunday, February 8, 2009

IntelliJ IDEA 8 Groovy Intentions in 2:45

The Groovy plugin within IntelliJ 8 offers Alt+Enter triggered intentions to simpily, modify, and groovify your code.


The full list of intentions is on the JeyGroovy wiki, but here is a 2 minute 45 second video demonstration for those too lazy to read:








Trying to make this screencast has been a nightmare: bought microphone, doesn't work; upgrade IDEA, license expired; bought firewire card, out of PCI Ports; record screencast, can't add audio track; format to .swf, text illegible; pour huge glass of gin, no tonic. It can't get any worse.


All these IDEA posts are preparation for the Groovy IDE Shootout at Groovy.MN this Tuesday. Stop by and say Hi!

Wednesday, February 4, 2009

Groovy + IDEA Debugging Tip: Skip Stepping into GDK Classes

Debugging Groovy code in IDEA can be a hassle because you spend more time stepping into and out of classes in the Groovy language rather than your own classes. Nothing like stepping through a whole bunch of "invokeMethod" reflection layers.

You can fix this by telling IDEA not to step into the Groovy framework classes. Open up the Settings, and go to the Debugger->Stepping panel. Using the "Add Pattern..." button, add the patterns "groovy.*" and "org.codehaus.groovy.*" This screenshot is from IDEA 8, but this works fine on 7 too:




February 10, 2009 is the Groovy IDEA shootout at Groovy.MN... please stop by. And, of course, check out the IDEA Refcardz if you haven't yet.

Groovy Dynamic Method Support in IDEA

When IDEA 8 was announced, the press release simply said "Dynamic properties and methods". Uh, okay. I never understood what that meant until I stumbled upon a discussion board post for them.


Here's the explanation:

The JetGroovy plugin underlines expressions in Groovy code that it doesn't recognize. So misspelling a variable or referencing a missing class gives you a visual cue:


Notice how "mystring" is underlined (it is mis-capitalized). But frequently these are valid expressions that will resolve at runtime... for instance, Grails GORM methods. You know these methods will exist so the underlining is annoying. Accepting the Alt+Enter quick-fix on these properties and methods for "Add dynamic property" will add the expression to your list of dynamic expressions. You can view this dynamic expressions in the Dynamic properties panel.


Notice how the "".greetings is not underlined, whereas before it would have been. Also, the expression is now available for code completion:


To remove an expression just select it in the list and press delete on the keyboard.


That's not such a bad feature.

February 10, 2009 is the Groovy IDEA shootout at Groovy.MN... please stop by. And, of course, check out the IDEA Refcardz if you haven't yet.

Tuesday, February 3, 2009

97 Reasons to use IDEA for Groovy Development

In preparation for the Groovy IDE Shootout at Groovy.MN next Tuesday, I decided to crack open my IDEA settings pane today and see that additional inspections the JetGroovy plugin defines for IDEA.


Holy crud... there are 97 Groovy Inspections (98 if you count the ones in the svn repo). Most of these are not turned on by default, so it may be worth the effort to go through and turn them on. I've found that following the inspections' advice makes my code a lot Groovy-ish, it does a good job of flagging Java idioms and converting them into the equivalent Groovy code.

Here's a rundown of the more exciting ones:


Control Flow - "Conditional expression can be conditional call" teaches you to use ?. instead of conditionals. Nice. "Conditional expression can be elvis" teaches you to use ?:, which in my opinion is confusing but some people like it. And "'return' statement can be implicit" reminds you the return keyword is optional. There's also a bunch of other inspections similar to the Java ones, like "Constant if statement", "Loop statement that doesn't loop", and "Redundant conditional expression".


GPath inspections - "Getter call can be property access" turns all your foo.getBar() into foo.bar invocations. The autofix on this makes it really helpful. The same inspection applies to setter calls, List get/sets, and Map puts/gets. I use these all the time.

Potentially confusing code constructs - The same inspections from Java, such as "Statement with empty body", "Negated conditional expression", "Overly complex arithmetic expression", and "Pointless boolean expression". I find the pointless expressions inspections helpful, especially during refactorings.


Validity issues - The "Method with inconsistent returns" inspection will catch some type errors that aren't possible in Java. "Unreachable Statement" is helpful too.

Threading issues - I've professed my love for the Java threading inspections before, and the plugin brings many of them to the Groovy platform. There are 18 inspections in this category, and I've found "Unsynchronized method overrides synchronized method", "Synchronization on non-final field", and "Synchronization on variable initialized with literal" to be invaluable.


Error handling - Nothing especially Groovy here, but "Unused catch parameter", Empty catch/try/finall block, and the rest are helpful.

Assignment issues - Again, many standard inspections from Java, such as "Assign to Method Parameter", "Silly Assignment", and "Result of Assignment Used". These are mostly good for keeping up with coding standards or just flagging your Friday afternoon code as crappy.


Method Metrics - When my "Overly complex method", "Overly long method", and "Method with too many parameter" inspections show up, I like to view the version history to find the culprit and then stink eye them all week in the lunch room. I suppose your work environment could be different.

Probable bugs - I've never actually had these show up, but I suppose inspections like "Divide by zero", "Infinite recursion", and "Result of object allocation ignored" could help someone out there who despises unit tests.


I opened my settings and turned them all on, and now I'm slowly turning off the ones I don't like (or that give false positives). Seems like a good plan.

The full list of inspections is available on the JetGroovy wiki, and don't forget to download the IDEA RefCardz from DZone. I heard the author is a nice guy.