面向 Java 开发人员的 Groovy 迁移
我从事 Java 开发人员已有几年了。最近,关于 Groovy 的讨论相当热烈。我检查了一下,它看起来很有趣,但我没有看到它有任何固有的“哇因素”;也就是说,我没有看到任何开始开发它的内在价值。
现在,我确信我在这里只是没有见树木不见森林。因此,我向整个 SO 社区询问:学习 Groovy 对任何 Java 开发人员来说有何意义?有哪些特性/功能/等等。它比普通的 Java 做得(或者做得更好)吗?
软件世界中的事物不会无缘无故地像野火一样腾飞。我确信 Groovy 具有各种漂亮的小(和大)功能,这使其成为必须了解的;我只是没有“明白”它。提前致谢。
I've been a Java developer for several years now. Lately there's been quite the buzz over Groovy. I checked it out and it looks interesting and all, but I'm not seeing any inherent "wow factor" to it; meaning, I'm not seeing any intrinsic value to begin developing in it.
Now, I'm positive that I'm just not seeing the forest through the trees here. So, I ask the SO community at large: how would learning Groovy behoove any Java developer? What features/capabilities/etc. does it do (or do better) than plain ole' Java?
Things in the software world don't take off like wildfire without a reason. I'm sure Groovy has all sorts of nifty little (and big) capabilities that make it a must-know; I'm just not "getting" it. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,Groovy 相对于 Java 的一些最大优势是:
你可以写
写,我认为这样更具可读性。
地图和列表的内联符号。而不是
您可以为标准库编写
用于初始化属性的语法糖 - 例如,
可以替换为
'safe' 解引用 -
if (str != null) {return str.toUppercase();} else { return null;}
变为return str?.toUppercase()
正则表达式看起来更好
In my opinion, some of the biggest adventages Groovy has over Java are:
terser code thanks to overloaded operators and simplified property access. Instead of writing
you can write
which I consider much more readable.
inline notation for Maps and Lists. Instead of
you can write
useful extensions to standard libraries. For example, you can read files and webpages like this:
syntactic sugar for initializing properties - for example,
can be replaced by
'safe' dereferencing -
if (str != null) {return str.toUppercase();} else {return null;}
becomesreturn str?.toUppercase()
Regular Expressions look nicer
...由于到目前为止还没有人参与,我建议查看 Grooy 邮件列表 。关于 Grails 的类似问题也时常出现。这些都是伟大的社区,由曾经在某个时刻面临过相同类型问题的人们组成。
... since no one pitched in so far, I'd suggest looking at the Grooy mailing list. Similar questions pop up from time to time regarding Grails as well. Those are great communities of people who at some point or another faced the same type of questions.
我会添加另一个,因为我没有看到明确提到它。
对我来说,虽然简洁本身就很好,但这并不是重点。
无论您编写什么代码,Java 总是看起来像 Java,而且它永远不会读起来像您正在解决的问题——它读起来像 Java,解决您的问题。
我想要相反:我希望我的代码读起来像我正在解决的问题,用[在此处插入语言]编写。
Groovy 比 Java 更具表现力、更具延展性。它可能看起来像我正在尝试做的事情。典型的例子包括内部 DSL。我经常使用的一个是 easyb,它只是 Groovy 代码,但听起来像我的代码当我大声读出来时我正在做。还有很多例子,但 easyb 很容易推销。
还没有提到的是 AST 转换允许很酷的编译时技巧。您可以使用 AspectJ 等工具在 Java 中玩类似的游戏,但它并不是“内置”的。像 @Delegate、@Singleton 等这样的东西非常方便。
对我来说,一种语言需要是可延展的、可变形的、灵活的:Java 不是。 Java 的抽象模型贫乏,使用起来令人沮丧——整个语言中大大小小的额外工作量有时令人难以置信。
I'll add another, since I don't see it explicitly mentioned.
For me, while terseness is nice on its own, it isn't the main point.
Whatever code you write, Java will always look like Java, and it will never read like the problem you're solving--it will read like Java, solving your problem.
I want the reverse: I want my code to read like the problem I'm solving, written in [insert language here].
Groovy is more expressive, more malleable than Java. It can look like what I'm trying to do. Canonical examples of this include internal DSLs. One I've used frequently is easyb, which is just Groovy code, but sounds like what I'm doing when I read it out loud. There are a bunch more examples, but easyb is an easy sell.
Also not mentioned are the AST transformations allowing cool compile-time tricks. You can play similar games in Java with things like AspectJ, but it's not as "baked in". Things like @Delegate, @Singleton, etc. are just plain handy.
For me, a language needs to be malleable, deformable, flexible: Java is not. Java has an impoverished model of abstraction making it frustrating to work with--the amount of extra work, writ both small and large throughout the language, is mind-boggling at times.
你参加聚会有点迟到了。大约 4 年前,Groovy 迎来了它的曙光,当时人们厌倦了 Java 的荒谬冗长,准备为了减少代码而放弃性能,而 Groovy 正是以一种非常熟悉的语法为他们提供了这一点。但现在它已经存在了一段时间了,它像所有语言一样受到过去的拖累,一些不是最好的设计决策仍然存在,就像我很确定它的性能问题一样。这基本上就是 Groovy++ 出现的原因,它的整个存在表明它的前身存在一些问题。事实上,Groovy++ 解决了其前身的许多问题,但它也有自己的问题:主要的一个问题是它基本上仅由一个人的奉献驱动 - 即 Alex Tkachman, - 并且没有资金,所以你应该看到所涉及的所有风险。
如今,越来越多强大的竞争对手出现(Kotlin、Ceylon),对我来说,很明显,Groovy 甚至已经过了它的顶峰 - 甚至它的核心团队也在颤抖(你应该看看他们对 Grumpy 模式的讨论)。这就是我开始逐渐放弃这项技术而转向 Scala 的原因,并期待即将推出的 Kotlin,这两个项目都是很棒的项目,但要让“Java 思维”转向需要付出一些努力,但绝对值得。
由于过于挑剔的反应而进行的更新:
我想让您知道,我曾经是 Groovy 的忠实粉丝,并且连续两年专门使用它进行开发。转向新的语言对我来说是非常困难的一步。但由于我了解 Groovy 的每个角落,我了解它的核心团队及其倾向,所以当我建议不要将 Groovy 视为 Java 的替代品时,您应该明白我知道我在做什么。
You're kinda late to the party. Groovy saw its dawn about 4 years ago when people fed up with Java's ridiculous verboseness were ready to give up performance in the sake of code reduction and Groovy was providing them with exactly that in a very familiar syntax. But now that it's been there for a while it gets dragged by its past as much as every language does and some not best design decisions are there to stay as much as I'm pretty sure are its performance issues. This basically was the reason of emergence of Groovy++, the whole existance of which suggests that there are some problems with its predecessor. And in fact Groovy++ addresses a lot of issues of its predecessor, but it also has its own problems: the main one being that it's basically driven only by the devotion of a one man - Alex Tkachman that is, - and no funding, so you should see all the risks involved.
Nowadays that more and more strong rivals appear (Kotlin, Ceylon) it becomes obvious to me that Groovy has already passed even its zenith - even its core team is trembling (you should see their discussions on Grumpy mode). This was the reason for me to start gradually leaving this technology in preference to Scala and to look forward to forthcoming Kotlin which both are great projects but take some effort for a "Java mind" to switch to, but are definitely worth it.
Updates due to hypercritical response:
I would like you to know that I once was a huge fan of Groovy and was developing in it exclusively for 2 straight years. Moving on to a new language was a very hard step for me to take. But since I know Groovy's every corner, I know its core team and its tendencies you should understand that I know what I'm doing when I'm recommending not to consider Groovy as an alternative to Java.