4 ms·
I took over a large Groovy codebase that was damn near unreadable since it's controller and service layers heavily abused Groovy 'features': the def keyword (ba
by mcphilip 13y ago
I took over a large Groovy codebase that was damn near unreadable since it's controller and service layers heavily abused Groovy 'features': the def keyword (basically never defining intended types at all); not declaring return or argument types; implicit return statements; etc.
My first project was to spend a few days and literally remove all possible occurrences of def and add back in return and parameter types everywhere.
It was possibly the best decision I've made since it didn't involve throwing away any code and made existing code much more readable.
It's just sad that the highly capable developers that wrote the original code base didn't take the extra few seconds to declare their types.
- Roboprog 13y agoGroovy 2.x makes a bit of a nod in that direction. There are annotations to turn static typing on (or off) on classes and methods as needed. This allows you to be "safe" in most of the code, and explicitly label the parts with needed "magic". I agree with you that it's probably best to limit def or type-inference-like operations to local variables within functions/methods. Tangent: I like how Go does strict typing with interfaces, but still allows duck typing of parameters by not binding classes to interfaces at class definition time.
- vorg 13y agoThe static typing features in Groovy 2 were written by just 1 person only 1 year ago, Grails doesn't use them in its codebase, and Groovy's only other user with any traction, Gradle, still ships with Groovy 1.x. Best not to trust its features until Grails does.
- Roboprog 13y agoHmm. Thanks for the tip. I've been working on trying to graft Groovy 2.1 onto an old Java app. The static type annotations don't work with Java 1.5. You have to have at least Java 1.6. (I discovered today) So, if you have Java 1.5, you can really only use Groovy 1.x language features, as well. Otherwise, it turned out to be not so hard to start putting little Groovy seeds into a Java garden, which I'm not going to rewrite as Grails. Just being able to use closures with the Groovy Collection extensions is pretty useful.