Dynamic: To Be or Not to Be?

Given that Groovy is a dynamically typed language that supports optional typing, should we specify the type or rely on dynamic typing? There are no real rules in this area, but we can certainly develop some preferences.

When programming in Groovy, I lean toward omitting the type and instead making the parameter/variable names very expressive. Not specifying the type has the added advantages of duck typing (Design by Capability) and the ease of applying mocks for testing (Unit Testing Java and Groovy Code).

I opt to specify the type if I am forced to, like when JUnit requires test methods to be void or when specificity provides a significant benefit, like when mapping types to databases in Grails object-relational ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.