避免变量名冲突的布尔字段命名约定

huangapple go评论98阅读模式
英文:

Naming convention for boolean field to avoid variable name conflict

问题

我有一个Employee类。它需要一个布尔标志来指示该员工是否为经理。

从这个问题中https://stackoverflow.com/questions/5322648/for-a-boolean-field-what-is-the-naming-convention-for-its-getter-setter
我理解命名约定是将变量命名为X,getter命名为isX。所以我的Employee类会像这样

class Employee {
  boolean manager; 

  boolean isManager(){
    return this.manager;
  }
}

但是manager这个名称让我感到困惑。单词manager是一个名词,不是形容词,就像上面的问题中的activecurrentopenmanager可以是一个布尔标志,也可以是一个经理对象。此外,如果我确实想在Employee类中为经理设置成员变量,manager变量名已经用于标志。

class Employee {
  boolean manager; 

  boolean isManager(){
    return this.manager;
  }

  //Employee manager; //不知道如何命名
}

在这种情况下,有没有另一种方法来命名布尔型经理标志?谢谢。

英文:

I have an Employee class. It needs a boolean flag to tell if this employee is a manager.

From this question https://stackoverflow.com/questions/5322648/for-a-boolean-field-what-is-the-naming-convention-for-its-getter-setter
I understand that the naming conversion is to name the variable as X and the getter as isX. So my Employee class would be like this

class Employee {
  boolean manager; 

  boolean isManager(){
    return this.manager;
  }
}

But the name manager is confusing to me. The word manager is a noun, not an adjective, like active, current, open in the above question. manager could be a boolean flag or a manager object. Furthermore, if I do want a member variable for the manager in the Employee class, the manager variable name is already taken for the flag.

class Employee {
  boolean manager; 

  boolean isManager(){
    return this.manager;
  }

  //Employee manager; //don't know how to name this
}

Is there another way to name the boolean manager flag in this case? Thanks

答案1

得分: 2

一种选择是使用双值枚举来替代布尔值:

class Employee {
    enum Type { REGULAR, MANAGER }

    Type type = Type.REGULAR;
    Employee manager;

    Type getType() {
        return type;
    }

    Employee getManager() {
        return manager;
    }
}

也可以在枚举名称和获取方法名称中使用 RoleLevel,而不是 Type

英文:

One option is to use a two-value enum in place of a boolean:

class Employee {
    enum Type { REGULAR, MANAGER }

    Type type = Type.REGULAR;
    Employee manager;

    Type getType() {
        return type;
    }

    Employee getManager() {
        return manager;
    }
}

One could also use Role or Level instead of Type, both in the enum name and in the get-method name.

答案2

得分: 1

好的,以下是翻译好的内容:

这里真正的冲突在数据建模层面。

  • 员工 是一个 经理(布尔标志)
  • 员工 拥有一个 经理(员工值字段)

这里有两个(可能)不同的“经理”概念,但名称相同。那么我们如何在模型层面处理这个(明显的)冲突?

  • 我们可以忽略它。
  • 我们可以简单地重命名其中一个概念。但这是一个不好的想法,因为这两个概念可能密切相关1
  • 我们可以深入研究这些概念;即“作为具有经理地位的人”和“担任另一个人的经理角色”之间是否有显着(对于模型而言)的区别。这 可能 告诉我们这些概念有足够不同,需要具有不同的名称。

所以假设我们在建模层面忽略它,我们如何在 Java 层面处理它呢?以下是一些建议:

版本#1具有相同名称的两个不同属性。这可能违反了JavaBeans的约定... 至少大多数人理解的约定。

public class Employee {
    private boolean isManager;
    private Employee manager;  
    
    public boolean isManager(){
        return this.isManager;
    }
    
    public Employee getManager(){
        return this.manager;
    }
}

版本#2调整了属性名称。例如,使用“manager”和“aManager”:

public class Employee {
    private boolean isManager;
    private Employee manager;  
    
    public boolean isAManager(){
        return this.isManager;
    }
    
    public Employee getManager(){
        return this.manager;
    }
}

或者您可以调整另一个属性名称。

1 - 例如,在 UML 术语中,您可以将“经理”属性建模为从将员工与他们的经理相关联的关联中 派生 出来的。该属性值可以通过测试是否通过关联是另一位员工的经理来派生。

英文:

OK, so the real conflict here is at the data modelling level.

  • An Employee is-a manager (the boolean flag)
  • An Employee has-a manager (the Employee valued field)

There are two (possibly) different "manager" concepts with the same name. So how do we deal with this (apparent) conflict at the model level?

  • We could ignore it.
  • We could simply rename one or other of the concepts. But that is a bad idea because the two concepts are probably intimately related<sup>1</sup>.
  • We could drill down on the concepts; i.e. is there a significant (to the model) difference between "being a person with manager status" and "being in the role of manager for another person". This could tell us that the concepts are sufficiently different that they need to have different names.

So assuming that we ignore it at the modeling level, how do we deal with it at the Java level? Here are a couple of suggestions:

Version #1 has two distinct properties with the same name. This is probably a violation of the JavaBeans convention ... at least as most people understand it.

  public class Employee {
      private boolean isManager;
      private Employee manager;  
    
      public boolean isManager(){
        return this.isManager;
      }
    
      public Employee getManager(){
        return this.manager;
      }
  }

Version #2 tweaks the property names. For example, use "manager" and "aManager":

  public class Employee {
      private boolean isManager;
      private Employee manager;  
    
      public boolean isAManager(){
        return this.isManager;
      }
    
      public Employee getManager(){
        return this.manager;
      }
  }

Or you could tweak the other property name.


<sup>1 - For example, in UML terms you could model the "manager" Attribute as derived from the Association that relates an employee to their manager. The attribute value can be derived by testing if this employee is a manager of any another employee via the association.</sup>

答案3

得分: 0

这是一个很好的问题!... 因为我多年来一直在为这种情况苦苦挣扎,但始终没有找到最佳的表达方式。😄 是否考虑使用名词 + "Typed" 后缀来命名布尔变量,并在 getter 方法中保持 "is" + 名词 的形式?这样你就可以得到:

class Employee {
  boolean managerTyped;

  boolean isManager() {
    return this.managerTyped;
  }

  Employee manager;
}
英文:

This is a great question! ...as I also been struggling with this kinda thing for years, and yet still not able to find the best formulation for it. 😄 How about naming the boolean variable with the noun + &quot;Typed&quot; suffix, and stick with the &quot;is&quot; + Noun for the getter? So you would get:

class Employee {
  boolean managerTyped;

  boolean isManager() {
    return this.managerTyped;
  }

  Employee manager;
}

答案4

得分: -2

像这样怎么样?

class Employee {
    Employee manager;

    boolean isManager(){
        return (manager != null && manager instanceof Manager);
    }
}
英文:

How about something like this?

class Employee {
    Employee manager;

    boolean isManager(){
        return (manager != null &amp;&amp; manager instanceof Manager);
    }
}

huangapple
  • 本文由 发表于 2020年3月4日 11:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/60518451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定