Preference for Variable Types in Groovy

This is a suggestion for a small UX enhancement.

When coding in Groovy, one can declare a variable with an unspecified type:

     def msg = 'Hello'


or a specific type:

     String msg = 'Hello'


Some shops and individual developers prefer the loosest possible type declaration, arguing for greatest flexibility and least ceremony. Some shops and individual developers prefer the strongest possible type declaration, arguing for greatest readability and maintainability.

Sometimes, the choice is driven by the context; i.e. in Grails domain artifacts (aka artefacts :-), the fields should be declared with their true types to assist schema generation of the correct column types. Likewise, Grails controller action methods (the preferred approach in Grails 2.0) should use specific types to help Grails to convert the String request parameters to the appropriate type.

I don't want to debate the merits of using the loosest type declaration versus using the strongest type declaration. I am merely noting that, while there is not a right answer, there is often a general preference.

In IDEA, when doing an "Introduce Variable" refactoring, the dialog prompts for the declaration type. It would be nice if there was an IDE preference that could drive whether 'def' or the specific type showed up first in the dropdown list in the dialog. In fact, the preference may allow the Groovy Introduce Variable refactoring to work like Java's where the preference for strong/loose typing could be used automatically avoiding the dialog altogether.

It's a small thing, but as I use refactorings a lot, the dialog where I have to keep changing the variable type from 'def' to a specific type becomes a small annoyance of a speed bump.

Jack

Please sign in to leave a comment.