简单的 Getter/Setter 注释
您使用什么约定来注释 getter 和 setter? 这是我想了很长一段时间的事情,例如:
/**
* (1a) what do you put here?
* @param salary (1b) what do you put here?
*/
public void setSalary(float salary);
/*
* (2a) what do you put here?
* @return (2b)
*/
public float getSalary();
我总是发现我为 1a/b 和 2a/b 写了几乎完全相同的东西,比如 1a) 设置员工的工资,1b)雇员的工资。 这看起来太多余了。 现在我可以看到,对于更复杂的东西,您可以在 (a) 部分中编写更多内容,以给出上下文,但对于大多数 getter/setter 来说,措辞几乎完全相同。
我只是好奇,对于简单的 getter/setter 来说,是否可以只填写 (a) 部分或 (b) 部分。
你怎么认为?
What convention do you use to comment getters and setters? This is something I've wondered for quite some time, for instance:
/**
* (1a) what do you put here?
* @param salary (1b) what do you put here?
*/
public void setSalary(float salary);
/*
* (2a) what do you put here?
* @return (2b)
*/
public float getSalary();
I always find I'm pretty much writing the exact same thing for 1a/b and 2a/b, something like 1a) Sets the salary of the employee, 1b) the salary of the employee. It just seems so redundant. Now I could see for something more complex you might write more in the (a) parts, to give context, but for a majority of the getters/setters out there the wording is almost exactly the same.
I'm just curious if, for the simple getters/setters its ok to only fill in either the (a) part OR the (b) part.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我通常只填充 setters 的 param 部分,以及 getters 的 @return 部分:
这样 javadoc 检查工具(例如 Eclipse 的警告)就会干净无误,并且不会出现重复。
I usually just fill the param part for setters, and the @return part for getters:
That way javadoc checking tools (such as Eclipse's warnings) will come out clean, and there's no duplication.
绝对毫无意义 - 如果没有这种垃圾扰乱您的代码,您会更好:
如果有保证的话,非常有用:
特别是对属性实际含义的解释在域模型中可能至关重要。 每当我看到一个 bean 充满了只有投资银行家、生物化学家或量子物理学家才能理解的晦涩难懂的属性,并且注释解释说 setGobbledygook() 方法“设置了 gobbledygook”,我就想掐死某人。
Absolutely pointless - you're better off without this kind of crap cluttering your code:
Very useful, if warranted:
Especially the explanation of what the property actually means can be crucial in domain models. Whenever I see a bean full of properties with obscure names that only investment bankers, biochemists or quantum physicists understand, and the comments explain that the setGobbledygook() method "sets the gobbledygook.", I want to strangle someone.
我总是两者都填写。 花费在打字上的额外时间可以忽略不计,而且一般来说,更多的信息总比更少的信息好。
I always fill in both. The additional time spent typing is negligible, and more information is better than less, in general.
如果 javadoc 未添加任何内容,我将删除 javadoc 并使用自动生成的注释。
If the javadoc does not add anything, I delete the javadoc and use the auto-generated comments.
如果字段名称足以描述内容,则不要输入任何内容。
一般来说,让代码独立,并尽可能避免注释。 这可能需要重构。
编辑:以上仅指 getter 和 setter。 我相信任何不平凡的事情都应该正确地进行 javadoc 处理。
Don't put anything if the field name is suficiently descriptive of the contents.
Generally, let the code be self standing, and avoid commenting if at all possible. This may require refactoring.
EDIT: The above refers to getters and setters only. I believe anything non trivial should be properly javadoc'ed.
填写 (b) 部分是可以的,特别是如果您在字段声明处添加注释以表明该字段的全部内容。
it is ok to fill in the (b) part, especially if you put a comment at the field declaration indicating what the field is all about.
如果 getter/setter 中没有特殊操作,我通常会写:
With Javadoc (with private option):
和/或
With Doxygen (with private extract option):
If there isn't special operation in getter/setter I usually write :
With Javadoc (with private option):
and/or
With Doxygen (with private extract option):
注释访问器,特别是如果它们不在任何地方执行任何操作,是不必要的,而且浪费指尖。
如果阅读您代码的人无法理解
person.getFirstName()
返回某人的名字,您应该尽一切努力让他被解雇。 如果它执行了一些数据库魔法,扔了几个骰子,调用名字秘书来获取名字,就可以安全地假设这是一个不平凡的操作,并很好地记录它。另一方面,如果您的
person.getFirstName()
没有返回一个人的名字...那么,我们不要去那里,好吗?Commenting accessors, especially if they don't do any operations anywhere, is unnecessary and a waste of fingertips.
If someone reading your code can't understand that
person.getFirstName()
returns the first name of a person, you should try everything in your powers to get him fired. If it does some database magic, throws a few dice, calls the Secretary of First Names to get the first name, It's safe to assume it's a non-trivial operation, and document it well.If, on the other hand, your
person.getFirstName()
doesn't return a person's first name... well, let's not go there, shall we?问问自己,当评论被视为 JavaDoc(从浏览器)时,您希望人们看到什么。 许多人说文档是不必要的,因为它是显而易见的。 如果字段是私有的(除非您明确地为私有字段打开 JavaDocs),则这将不成立。
就您而言:
不清楚工资用什么表示。是美分、美元、英镑、人民币吗?
在记录 setter/getters 时,我喜欢将内容与编码分开。 示例:
第一行表示返回高度。 返回参数记录高度以米为单位。
Ask yourself what do you want people to see when the comments are viewed as JavaDocs (from a browser). Many people say that documentation is not necessary since it's obvious. This won't hold if the field is private (unless you explicitly turn on JavaDocs for private fields).
In your case:
It's not clear what salary is expressed in. It is cents, dollars, pounds, RMB?
When documenting setters/getters, I like to separate the what from the encoding. Example:
The first line says it returns the height. The return parameter documents that height is in meters.
为什么他们不只包含一个引用标签来让您评论字段值以及 getter 和 setter 的引用。
这样文档就适用于 getter 和 setter 以及字段(如果私有 javadocs 已打开)。
Why don't they just include a reference tag to let you comment the field value and the reference from getters and setters.
So that the documentation applies to the getter and setter as well as the field (if private javadocs is turned on that is).
使用 Project Lombok 可以避免这种样板文件。 只需记录字段变量,即使它是私有的,然后让 Lombok 注释生成正确记录的 getter 和 setter。
对我来说,仅这个好处就值得成本。
This kind of boilerplate can be avoided by using Project Lombok. Just document the field variable, even if it's
private
, and let Lombok annotations generate properly documented getters and setters.For me, this benefit alone is worth the costs.
我对答案感到非常失望,基本上说全面的记录是浪费时间。 除非您在文档中明确说明,否则 API 的客户端如何知道名为
setX
的方法是标准 JavaBean 属性设置器?如果没有文档,调用者将不知道该方法是否有任何副作用,除了祈祷所遵循的明显约定之外。
我确信我不是唯一一个不幸地发现名为
setX
的方法不仅仅只是设置属性的方法的人。I'm really disappointed about the answers basically saying comprehensive documenting is a waste of time. How are clients of your API supposed to know that a method called
setX
is a standard JavaBean property setter unless you say so clearly in the documentation?Without documentation, a caller would have no idea whatsoever if the method had any side effects, other than by crossing their fingers about the apparent convention being followed.
I'm sure I'm not the only one here to have had the misfortune to find out the hard way that a method called
setX
does a whole lot more than just set a property.我想说,只担心注释 getter 和 setter 如果它们有某种副作用,或者需要初始化之外的某种前提条件(即:获取将从数据结构中删除一个项目,或者为了设置您需要的东西)首先让 x 和 y 就位)。 否则这里的评论是相当多余的。
编辑:此外,如果您确实发现 getter/setter 涉及很多副作用,您可能需要更改 getter/setter 以具有不同的方法名称(即:堆栈的推送和弹出)[感谢下面的评论]
I'd say only worry about commenting getters and setters if they have some sort of side effect, or require some sort of precondition outside of initialization (i.e.: getting will remove an item from a data structure, or in order to set something you need to have x and y in place first). Otherwise the comments here are pretty redundant.
Edit: In addition, if you do find a lot of side effects are involved in your getter/setter, you might want to change the getter/setter to have a different method name (ie: push and pop for a stack) [Thanks for the comments below]
一般来说没什么,如果我能帮忙的话。 getter 和 setter 应该是不言自明的。
我知道这听起来像是一个无法回答的问题,但我尝试利用时间来评论需要解释的事情。
Generally nothing, if I can help it. Getters and setters ought to be self-explanatory.
I know that sounds like a non-answer, but I try to use my time for commenting things that need explanation.