Java setter和getter方法的默认注释

Java setter和getter方法的默认注释,java,pojo,Java,Pojo,在代码审查期间,我们将讨论代码注释部分。我们的一位团队成员建议在所有setter/getter方法上添加默认注释。它们真的有用吗?如果是,那么放默认注释有什么用呢 /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the billType */ p

在代码审查期间,我们将讨论代码注释部分。我们的一位团队成员建议在所有setter/getter方法上添加默认注释。它们真的有用吗?如果是,那么放默认注释有什么用呢

/**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the billType
     */
    public BillType getBillType() {
        return billType;
    }

    /**
     * @param billType the billType to set
     */
    public void setBillType(BillType billType) {
        this.billType = billType;
    }

    /**
     * @return the lateCharge
     */
    public Float getLateCharge() {
        return lateCharge;
    }

    /**
     * @param lateCharge the lateCharge to set
     */
    public void setLateCharge(Float lateCharge) {
        this.lateCharge = lateCharge;
    }

    /**
     * @return the lateChargeType
     */
    public LateChargesType getLateChargeType() {
        return lateChargeType;
    }

    /**
     * @param lateChargeType the lateChargeType to set
     */
    public void setLateChargeType(LateChargesType lateChargeType) {
        this.lateChargeType = lateChargeType;
    }

    /**
     * @return the billDay
     */
    public String getBillDay() {
        return billDay;
    }

谢谢:)

请不要那样做,鲍勃叔叔会永远诅咒你的。 阅读本文:

也许读一读这本书:

要点是:注释对理解代码有什么帮助??如果代码无法理解,那么您需要更好地编写它。但是getBanana方法确实需要像“返回香蕉”这样的注释

另外,如果将来您更改了代码,并且方法变成了getFruit,但是您忘记了更改注释,该怎么办?下一个阅读它的开发人员会感到困惑


真的,帮你自己一个忙:不要添加无用的评论。即使它们是自动的。

请不要这样做,鲍勃叔叔会永远诅咒你的。 阅读本文:

也许读一读这本书:

要点是:注释对理解代码有什么帮助??如果代码无法理解,那么您需要更好地编写它。但是getBanana方法确实需要像“返回香蕉”这样的注释

另外,如果将来您更改了代码,并且方法变成了getFruit,但是您忘记了更改注释,该怎么办?下一个阅读它的开发人员会感到困惑


真的,帮你自己一个忙:不要添加无用的评论。即使它们是自动的。

没有正确或错误的答案,这是一个意见的问题

但就我个人而言,我认为对getter或setter的评论是多余的,因为这种方法的作用通常非常明显。除非它有某种副作用或特殊情况,否则您认为注释真的会为getter/setter方法添加任何信息吗

在本例中,
setBuildType
设置对象的生成类型,这从方法名称和方法的快速扫描中可以明显看出。你真的需要占用额外的三行垂直屏幕空间来解释它吗


假设
setBuildType
方法有副作用,当您设置生成类型时,它会更改对象中的其他变量,或者根据您设置的生成类型调用其他方法,那么解释这些副作用的注释可能对该方法的用户有用。

没有正确或错误的答案,这是一个意见问题

但就我个人而言,我认为对getter或setter的评论是多余的,因为这种方法的作用通常非常明显。除非它有某种副作用或特殊情况,否则您认为注释真的会为getter/setter方法添加任何信息吗

在本例中,
setBuildType
设置对象的生成类型,这从方法名称和方法的快速扫描中可以明显看出。你真的需要占用额外的三行垂直屏幕空间来解释它吗


假设
setBuildType
方法有副作用,当您设置生成类型时,它会更改对象中的其他变量,或者根据您设置的生成类型调用其他方法,那么,解释这些副作用的注释可能对该方法的用户有用。

根据软件开发的最佳实践,编码良好的软件不需要注释。但有时,与客户签订的合同可能需要对每个方法或类进行文档记录。在这种情况下,您甚至需要默认注释。

根据软件开发的最佳实践,编码良好的软件不需要注释。但有时,与客户签订的合同可能需要对每个方法或类进行文档记录。在这种情况下,您甚至需要默认注释。

没有必要在模型中添加注释,建议使属性名称清晰简洁。一个非常好的优点是,您还可以获得更干净的代码。作为一名开发人员,您的目标是尽可能清楚地让其他人容易理解目的,而不必用评论压倒他们

/**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the billType
     */
    public BillType getBillType() {
        return billType;
    }

    /**
     * @param billType the billType to set
     */
    public void setBillType(BillType billType) {
        this.billType = billType;
    }

    /**
     * @return the lateCharge
     */
    public Float getLateCharge() {
        return lateCharge;
    }

    /**
     * @param lateCharge the lateCharge to set
     */
    public void setLateCharge(Float lateCharge) {
        this.lateCharge = lateCharge;
    }

    /**
     * @return the lateChargeType
     */
    public LateChargesType getLateChargeType() {
        return lateChargeType;
    }

    /**
     * @param lateChargeType the lateChargeType to set
     */
    public void setLateChargeType(LateChargesType lateChargeType) {
        this.lateChargeType = lateChargeType;
    }

    /**
     * @return the billDay
     */
    public String getBillDay() {
        return billDay;
    }
通常,您会将注释放在控制器/服务/逻辑条件上,但不会放在模型中


我在其他企业应用程序中也没有看到这种做法,因此我认为您不必这样做,相反,我将关注技术文档,以陈述/解释每个属性的责任和其他重要细节

没有必要在您的模型中添加注释,并建议使属性名称清晰简洁。一个非常好的优点是,您还可以获得更干净的代码。作为一名开发人员,您的目标是尽可能清楚地让其他人容易理解目的,而不必用评论压倒他们

/**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the billType
     */
    public BillType getBillType() {
        return billType;
    }

    /**
     * @param billType the billType to set
     */
    public void setBillType(BillType billType) {
        this.billType = billType;
    }

    /**
     * @return the lateCharge
     */
    public Float getLateCharge() {
        return lateCharge;
    }

    /**
     * @param lateCharge the lateCharge to set
     */
    public void setLateCharge(Float lateCharge) {
        this.lateCharge = lateCharge;
    }

    /**
     * @return the lateChargeType
     */
    public LateChargesType getLateChargeType() {
        return lateChargeType;
    }

    /**
     * @param lateChargeType the lateChargeType to set
     */
    public void setLateChargeType(LateChargesType lateChargeType) {
        this.lateChargeType = lateChargeType;
    }

    /**
     * @return the billDay
     */
    public String getBillDay() {
        return billDay;
    }
通常,您会将注释放在控制器/服务/逻辑条件上,但不会放在模型中


我在其他企业应用程序中也没有看到这种做法,因此我认为您不必这样做,相反,我将关注技术文档,以陈述/解释每个属性的责任和其他重要细节

这个问题可能是重复的,可能会征求意见,但看起来您的同事希望包含一些Javadoc。除了Javadoc之外,还可以提出一个非常有力的论点,即注释是错误代码的标志。毕竟,写得好的代码应该是相当可读和透明的。很可能,这只是为了避免声纳(或类似)投诉。它们有用吗?对于简单的getter和setter,只有当您的代码是垃圾,并且方法的名称没有反映它返回/设置的变量时,才可以使用。这个问题可能是重复的,可能会引起意见,但看起来您的同事希望包含一些Javadoc。除了Javadoc之外,可以提出一个非常有力的论点,即注释是