Quantcast

Best Practices for def vs static typing: Industrial Strength Groovy

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Best Practices for def vs static typing: Industrial Strength Groovy

Steve Amerige
I've adopted the practice of using 'def' in writing Groovy class code only when I intentionally want to use the dynamic features of Groovy.  When I know that a property is a certain type or that a method returns a certain type or consumes arguments of certain types, I always specify those types.  In Enterprise coding, being explicit with type is generally considered to be a best practice.

Of course, Groovy offers metaprogramming and idioms that are so valuable that in some cases I loosen the static typing requirement.  For example, being able to call methods with named arguments and, therefore, needing to have the first formal parameter of the method be a Map is extremely useful.  In this case, the method implementation needs to take extra pains to do argument checking.  And, there are plenty of other dynamic features of Groovy that I cautiously use.

I do worry about code being as strong as it can be, so I'm hoping to hear any opinions as to best practices of using def as compared to specifying types that tend to make code more industrial strength.

Thanks,
Steve Amerige
SAS Institute, Senior Software Developer


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best Practices for def vs static typing: Industrial Strength Groovy

Guillaume Laforge-4
There are few thoughts on this topic here on the Groovy website:

In a nutshell, def is fine in method bodies or for particular dynamic aspects, but for everything that is a "contract" (method signatures, properties, etc), it's better to use explicit types.

Guillaume

On Wed, Oct 3, 2012 at 4:14 PM, Steve Amerige <[hidden email]> wrote:
I've adopted the practice of using 'def' in writing Groovy class code only when I intentionally want to use the dynamic features of Groovy.  When I know that a property is a certain type or that a method returns a certain type or consumes arguments of certain types, I always specify those types.  In Enterprise coding, being explicit with type is generally considered to be a best practice.

Of course, Groovy offers metaprogramming and idioms that are so valuable that in some cases I loosen the static typing requirement.  For example, being able to call methods with named arguments and, therefore, needing to have the first formal parameter of the method be a Map is extremely useful.  In this case, the method implementation needs to take extra pains to do argument checking.  And, there are plenty of other dynamic features of Groovy that I cautiously use.

I do worry about code being as strong as it can be, so I'm hoping to hear any opinions as to best practices of using def as compared to specifying types that tend to make code more industrial strength.

Thanks,
Steve Amerige
SAS Institute, Senior Software Developer





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Loading...