Question

I'm working on a Grails project and the team seems to be split on whether the type should be declared on injected services or whether def should be used instead. I was curious to hear what most people recommend?

Was it helpful?

Solution

If you ask 10 people you may get 11 opinions but there are benefits to having the references statically typed. For example, you can take advantage of Groovy's static type checking. This capability is particularly interesting in Grails 2.4 with the introduction of the GrailsCompileStatic annotation. If you don't provide a static type there is no good way for the type checker to do static type checking or static compilation. If things are generally done well in the Grails app, you don't give up much by using static types for this sort of thing so I think if I were going to make a contextless recommendation specifically for the types of things described in the question, I would recommend using static types.

OTHER TIPS

It's most common in Grails to simply use def.

Jeff is right, Declaring it can save you time in with bugs and testing as well as giving you speed gains. At the same time, 'def' is a nice benefit when quickly developing and templating your app.

Alot of developers do not understand the benefits of quick templating especially in web development and how 'def' lends itself to that and that is a real bonus over static Java. But you WILL have to go back and statically code those vars to get any real speed gains.

So if while coding you know its a String, Long, List, Hashmap, etc, just code it as such to save yourself headache in the long run. But if you can't remember or it has a wrapper, its fine to use def to run the app until you get back to it.

The addage is get it running first and then impove on it later

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top