Groovy : Closures or Methods

前端 未结 2 1080
借酒劲吻你
借酒劲吻你 2020-12-08 06:34

I\'ve got into the habit of using Closures everywhere I can in place of regular methods, even when I don\'t need access to free variables. So, I will use this:

2条回答
  •  忘掉有多难
    2020-12-08 07:26

    While all the things @Don says are true, I don't know that any of them are all that significant. You can override the delegate of a closure which can change execution, but unless you're passing the closure around (which you can't do with a method anyways) you don't really have to worry about any of those things happening. Playing with the delegate is a feature, not a liability.

    As for a possible performance hit from extra class files, why are you using Groovy if you're that worried about performance?

    My opinion is that it doesn't really matter. If you have a lot of people on your team that are old Java developers, they're probably going to dislike it when you use closures where a method will do. On the other hand, someone fluent in Groovy will get annoyed if you clutter everything up with methods when a closure makes more sense.

    tl;dr - There is no hard and fast rule, instead you should exercise good judgement with an eye towards code readability.

提交回复
热议问题