【问题标题】:Which method should change the field, when calling a hierarcy of private methods?在调用私有方法的层次结构时,哪个方法应该更改字段?
【发布时间】:2014-08-10 21:02:00
【问题描述】:

当一个类的公共方法需要调用一个私有方法导致字段被改变时,哪个方法应该改变这个字段?这有什么共同的约定吗?一种方法比另一种更可取吗?

考虑这两个代码sn-ps:

public class boolHolder {
    private boolean theBool = false;

    public void doYourThing() {
        // Do a lot of other stuff
        setBool();
    }

    private void setBool() {
        // Do a lot of other stuff, justifying a private method for this
        this.theBool = true;
    }
}

VS

public class boolHolder {
    private boolean theBool = false;

    public void doYourThing() {
        // Do a lot of other stuff
        theBool = setBool();
    }

    private boolean setBool() {
        // Do a lot of other stuff, justifying a private method for this
        return true;
    }
}

这两个剪断当然是一个非常简单的案例,但我敢肯定,我不是唯一一个以公共方法调用大量私有方法树而告终的人。该字段应该设置在分支的末尾,还是应该传回一个值?

【问题讨论】:

  • 一般来说,最好使用公共设置器并保持字段私有,因此可以从类可访问的任何位置设置字段。你的 doYourThing() 方法在我看来它实际上不是一个二传手;它还可以包含其他逻辑。感觉不是一个表演套装的好地方。

标签: java methods field conventions


【解决方案1】:

我认为只有一个地方会设置字段的值更有意义,它应该是最后一个被调用的方法。它使代码更容易理解。你的第一个 sn-p 在我看来更具可读性。

这是另一个我认为支持此约定的 sn-p:

假设我们有一个带有两个 setter 的 int 成员 - 一个接受一个 int,另一个接受该 int 的 String 表示(例如,如果我们从 XML String 反序列化一个实例)。

int value;

public void setIntField (String value) 
  throws SomeException
{
    if (value == null)
        throw new SomeException();
    try {
        int val = Integer.parseInt (value);
        setIntField (val);
    }
    catch (NumberFormatException ex) {
        throw new SomeException();
    }
}

public void setIntField (int value)
    throws SomeException ()
{
    if (value < MIN_ALLOWED || value > MAX_ALLOWED)
        throw new SomeException ();
    this.value = value;
}

【讨论】:

    【解决方案2】:

    除了将theBoolsetBool 重命名为更易于理解的名称(我将假设您在实际应用程序中这样做),我会选择第一个。带有单词set 的方法应该是setter,没有多少人会期望返回值。

    【讨论】:

      【解决方案3】:

      变化不大,但您可以尝试为您的方法使用更好的命名:我不喜欢您将第二个方法命名为 setBool()。

      如果你写“做很多其他事情,为此证明一个私有方法的合理性”,你可以尝试将 动词 与你所做的事情相关联。 假设您更新了帐户状态,并且在完成后想用布尔值表示状态,请使用类似于您所做的事情但以有意义的方式调用它,例如updateAccount() 并在更新正常时返回 true 或将其设置在内部:

      public class boolHolder {
          private boolean accountUpdated = false;
      
          public void doYourThing() {
             // Do a lot of preliminary stuff
             updateAccount();
          }
      
         private void updateAccount() {
             // try to update account
             // if update went fine
             this.accountUpdated = true;
         }
      }
      

      public class boolHolder {
          private boolean accountUpdated = false;
      
          public void doYourThing() {
             // Do a lot of preliminary stuff
             this.accountUpdated = updateAccount();
          }
      
         private boolean updateAccount() {
             // try to update account
             // if error happens, rollback change and set
             return false;
             // else (update went fine)
             return true;
         }
      }
      

      两者都很好,但是让你的方法告诉他们他们做了什么,因为更新 bool 不是主要操作,因为你“做了很多其他的事情,为此证明了一个私有方法”。

      如果您像以前一样使用默认值为 false,则内部值设置会更紧凑,但另一个在其作用方面更为明确。所以我更喜欢这样:为你的操作返回一个结果。

      【讨论】:

        猜你喜欢
        • 2014-04-04
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        相关资源
        最近更新 更多