【问题标题】:Java naming guide (specific class type naming - not conventions)Java 命名指南(特定类类型命名 - 非约定)
【发布时间】:2018-11-22 18:04:27
【问题描述】:

(这与Java约定无关)

我正在寻找可以帮助我了解如何命名以特定方式起作用的类的指南。

我已经在网上查看并在各种网站上阅读过这方面的内容;但是,我学到的只是不该做什么。即下壳/破折号/美元符号。

但我真正想知道的是如何命名这样的类:

(查看类似的项目,我发现人们将其命名为“模块”) 在本例中,我将它们命名为“函数”。

public class HouseButtonsFunction extends ButtonSwitchFunction{

@Override 
public void interact(Person p, Object...args){
    //if person turns a light switch or open-garage switch
}

}


public class CarButtonsFunction extends ButtonSwitchFunction{

@Override 
public void interact(Person p, Object...args){
    //if person turns on ignition or roof lights
}

}

还有像

这样的超类
PersonInteractionFunction

这将处理以下事情:

PersonWalkingFunction extends PersonInteractionFunction
PersonSittingDownFunction extends PersonInteractionFunction
PersonDrivingFunction extends PersonInteractionFunction

那么这里的“功能”是正确的词吗?

我在哪里可以找到正确的命名法?

【问题讨论】:

  • 什么是你的“功能”?我真的不明白你的意思。
  • 我认为没有规则。只有常识说“命名事物,以便您和其他人能够理解它是什么”
  • 你可以试试这个:link
  • 我可能会遵循java.util.function 中的类示例。
  • 是的,但我希望它被普遍理解,所以我不能将它命名为 HouseButtonsWidget 或类似的名称,因为它会被误解为 java 小部件。

标签: java syntax naming


【解决方案1】:

我建议您阅读关于命名的最著名的经典文档,称为“Ottinger's Rules for Variable and Class Naming”。 它是由 Tim Ottinger 早在 1997 年写的,至今仍具有相关性。

我还建议您阅读 Robert C. Martin(鲍勃叔叔)写的 Clean Code 和 Kent Beck 写的 Implementation Patterns 的书

【讨论】:

  • Clean Code 中的第 2 章是 Bob 要求我为选集撰写的那篇论文的更新版本。如果你能得到每一个的副本,你会看到一些演变(我不会从你那里赚一分钱)。
【解决方案2】:

我不会在这里使用“模块”或“功能”。

“模块”与存储相关联。例如,它可以是一个类,其中包含一个集合或收集有关可以在逻辑上分组在一起的类的信息。

“功能”是将某物转化为其他物的一种手段。例如,Function<Person, Rabbit> 将人变成兔子。

我会使用“Action”这个词,因为您定义了一个方法interact

【讨论】:

    【解决方案3】:

    没有关于如何命名类和方法的规则/约定。您必须遵循常识:明确命名事物,以便您和其他人了解它是什么以及它的作用

    例如,对于房屋中的按钮,具有与该按钮交互的功能。您可以考虑不同的有效命名。类可以命名为HouseButtonFunctionHouseButtonAction 等。方法可以命名为interact()action() 等。区别取决于您的方法到底是什么。如果它切换某些东西,请使用toggle()使用你认为最容易理解和最明确的那个。就你个人而言,因为你的类定义了这个按钮的交互,我建议将它命名为HouseButtonInteraction

    无论如何,我强烈建议您对您的内容发表评论,以免造成更多混淆。

    /**
     * This method is called when a person interact with the House button.
     * This method will action the given object
     * @param p : the person that interact with the house button
     * @param o : the object to action
     */
    public void interact(Person p, Object o) { ... }
    

    还要尽量在命名上始终保持一致。即,如果对于房屋按钮,您将类命名为 HouseButtonFunction 和方法 interact。不要更改汽车按钮的命名:

    //not to do !!
    class : CarButtonAction
    method : actionObject()
    //To do
    class : CarButtonFunction
    method : interact()
    

    【讨论】:

      【解决方案4】:

      理想情况下,类的名称应该是一个具体且易于阅读代码的人理解的名词。考虑到超类似乎也被命名为Function,以“功能”结尾的类名很好。但是,如果您可以重命名超类,我会将它们命名为以Handler 结尾。喜欢

      • PersonWalkingHandler
      • PersonSittingDownHandler
      • PersonDrivingHandler

      【讨论】:

        【解决方案5】:

        没有像 cmets 中所说的那样硬性规定。命名约定是一种最佳实践,因此当所有开发人员都遵循某个命名标准时,他们可以轻松有效地关联和理解代码,否则建立关系是一项繁琐的任务。

        在一般实践中,一个类应该指代名词,而方法指代动作,因此它们与动词相关联。

        在上述情况下,我会建议这样的事情..

        HouseButtonsFunction => House 或 HouseBotton

        ButtonSwitchFunction => 开关或 SwitchBotton

        CarButtonsFunction => CarButton

        PersonWalkingFunction => 步行者

        PersonSittingDownFunction => 保姆

        PersonDrivingFunction => 驱动等。

        请注意,这完全取决于您遵循的命名约定..

        【讨论】:

          猜你喜欢
          • 2010-12-02
          • 2015-02-19
          • 2014-08-07
          • 1970-01-01
          • 2016-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多