【问题标题】:Duplicate vs Existed [closed]重复与现有[关闭]
【发布时间】:2020-01-10 10:15:29
【问题描述】:

假设我想插入一个产品。
但是,在插入它之前,我需要检查它的名称是否已经存在于表格产品中。


合适的方法名称是什么?我对此有点困惑。

isDuplicatedName(String _name)

isExistedName(String _name)

如有语法错误,请提前道歉。谢谢。

【问题讨论】:

  • hasName 或 containsName。只有调用者知道它想再次插入这个名字,这就是它重复的原因。该方法只是检查名称是否已经存在。

标签: java methods naming-conventions naming


【解决方案1】:

根据Elegant Objects (Volume 1): Yegor Bugayenko,总的来说,有两种方法。

  1. 构建器方法

    这些方法构建一些东西并返回一个新对象。

    他们的名字必须是名词。

    Student student (Long id);
    List<Car> cars ();
    Long salary(Long id);
    
  2. 操纵器方法

    这些方法总是对数据进行修改并且什么都不返回。(总是返回 void)

    他们的名字必须是动词。

    void save(Student student);
    void print(String name);
    void delete (Long id);
    void persist(Student student);
    

所以建造者建造和操纵者操纵

返回 boolean 的方法有一点例外。 对于这类方法,最好用形容词来命名。

   boolean empty();
   boolean existent(Long id);

我建议您在命名方法时使用形容词,并且不要在命名中使用 "is"。

您的方法名称可能是:

boolean productExistent(String name);
boolean productNameExistent(String name);
boolean nameExistent(String name);
boolean existent(String name);

【讨论】:

    【解决方案2】:

    方法应该是动词,混合大小写,首字母小写,每个内部单词的首字母大写。 ref.code-conventions-135099

    基于此,您可能正在使用 isDuplicate() 或 isExisting() 或 isDuplicatedName() 或 isExistingName()

    【讨论】:

      【解决方案3】:

      .isDuplicated(_name) 听起来您已经拥有多个具有给定名称的实体。 .isExist(_name) 返回 true > 然后 0。

      这些方法适用于不同的情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-05
        • 2019-04-26
        • 2015-02-14
        • 2014-01-02
        • 1970-01-01
        • 2018-11-05
        • 2019-02-10
        • 2020-09-12
        相关资源
        最近更新 更多