Java-14之后的getter/setter命名约定 [英] Post Java-14 getter/setter naming convention

查看:222
本文介绍了Java-14之后的getter/setter命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 14引入了记录功能.记录创建的吸气剂与字段同名,因此例如可以写print(person.name()).但是旧的Java bean约定规定,应将此方法命名为getName().

Java 14 introduced records feature. Record creates getter with the same name as field, so one would write print(person.name()) for example. But old Java bean convention dictates that one should name this method as getName().

在同一代码库中同时使用这两种样式看起来不太好.无法将所有内容迁移到记录,因为它们太局限了,无法替换所有用例.

Using both styles in the same code base does not look very nice. Migrating everything to records is not possible, as they are too limited to replace all use-cases.

是否有正式或半官方的指南如何在Java 14中以新代码命名吸气剂和吸气剂?

Is there any official or semi-official guidelines how to name getters and setters after Java 14 in new code?

推荐答案

JEP 359 :

宣告样板战争"不是目标;特别是,使用JavaBean命名约定来解决可变类的问题并不是目标.

It is not a goal to declare "war on boilerplate"; in particular, it is not a goal to address the problems of mutable classes using the JavaBean naming conventions.

根据同一文档,我的理解是记录transparent holders for shallowly immutable data.

My understanding, based on the same document is that records are transparent holders for shallowly immutable data.

话虽这么说

  1. 记录不是查找getter/setter语法糖的地方,因为它们并不打算替代JavaBean.
  2. 我非常同意JavaBeans太冗长. 也许可以实现一个附加功能(称为 beans 而不是 records )-与 records 功能非常相似的行为但这将允许可变性.在这种情况下,记录 beans 将不会互斥.
  3. 如前所述,记录处于预览模式.让我们看看来自社区的反馈.
  1. Records are not the place to look for getters/setters syntactical sugar, as they are not meant to replace JavaBeans.
  2. I strongly agree with you that JavaBeans are too verbose. Maybe an additional feature (called beans instead of records) could be implemented - very similar behavior with the records feature but that would permit mutability. In that case, records and beans would not be mutually exclusive.
  3. As it has been mentioned, records are in preview mode. Let's see what the feedback from community would be.

总而言之,恕我直言,它们是前进的一步……我写了

All in all, IMHO they are a step forward... I wrote this example set where you can see a code reduction to ~15% LOC from standard JavaBeans.

此外,请注意records behave like normal classes: they can be declared top level or nested, they can be generic, they can implement interfaces(来自同一文档).实际上,您可以通过提取包含getter的接口来部分模拟JavaBean(尽管只有getter才有意义),但是这将是很多工作,而且不是一个真正干净的解决方案...

Also, note that records behave like normal classes: they can be declared top level or nested, they can be generic, they can implement interfaces (from the same document). You can actually partly simulate JavaBeans (only getters would make sense, though) by extracting an interface containing the getters - however that would be a lot of work and not a really clean solution...

因此,根据上述逻辑,请回答您的问题,-我没有看到针对(getter)和(setter)方法的任何(半)官方指南,并且我认为没有任何一个针对吸气者和二传手的官方指南.这样做的动机是因为,再次,记录不能替代JavaBeans ...

So, based on the logic above, to address your question, no - I didn't see any (semi)official guideline for getters and setters and I don't think that there is a motivation for it right now because, again, records are not a replacement for JavaBeans...

这篇关于Java-14之后的getter/setter命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆