简体   繁体   English

Java 静态谓词命名约定

[英]Java static predicate naming convention

Let's say I want to write isSubdirectory static function in my FileUtils helper class.比方说,我想写isSubdirectory静电功能在我的FileUtils的辅助类。 This function would look like:这个函数看起来像:

class FileUtils {
  public static boolean isSubdirectory(File child, File parent) {
    // ...
  }
}

Or I could flip parent and child parameters:或者,我可以翻转parentchild的参数:

class FileUtils {
  public static boolean isSubdirectory(File parent, File child) {
    // ...
  }
}

And I cannot choose which order is right...而且我无法选择哪个顺序是正确的...

In Kotlin there would not be any doubts: just declare extension function:在 Kotlin 中不会有任何疑问:只需声明扩展函数:

fun File.isSubdirectory(parent: File) {
  // ...
}

With an eye on kotlin I have invented mnemonic rule: first parameter in static function should be considered as this (I'm not the first who invented this rule. I saw many people also using it).着眼于 kotlin,我发明了助记符规则:静态函数中的第一个参数应该被视为this (我不是第一个发明这个规则的。我看到很多人也在使用它)。 So in this example I'd prefer placing child as first parameter.所以在这个例子中,我更喜欢将child作为第一个参数。

But the question is: is this rule is already formalized and has well known name?但问题是:这条规则是否已经正式化并具有众所周知的名称? I've tired to repeat this rule to people who don't know it and I wish I could simply refer to this rule by it's name.我已经厌倦了向不知道它的人重复这条规则,我希望我可以简单地通过它的名字来引用这条规则。

I'm not sure that there's a name or any real formalization.我不确定是否有名称或任何真正的形式化。 At best, it's just a common convention to have the first parameter look like the this .充其量,第一个参数看起来像this只是一个常见的约定。 Although rarest, the "this last" convention also exists, more in C and early C++ (Example: stdio with fread/fwrite)虽然最罕见,但“最后一个”约定也存在,更多出现在 C 和早期 C++ 中(例如:stdio with fread/fwrite)

There also exist conventions based on the argument type;也存在基于参数类型的约定; for example promoting the following order:例如推广以下订单:

  • Collection and other objects with generic parameters具有通用参数的集合和其他对象
  • Arrays of objects对象数组
  • Objects without generics没有泛型的对象
  • String细绳
  • Arrays of primitive types, eg byte[]原始类型的数组,例如byte[]
  • double, float双,浮动
  • long, int, short, byte长、整数、短、字节
  • boolean布尔值

There also exist the more or less exact opposite order.也存在或多或少完全相反的顺序。 Other conventions also tend to group arguments by their type, prefering method(String, String, int, int) rather than method(String, int, String, int) .其他约定也倾向于按类型对参数进行分组,更喜欢method(String, String, int, int)而不是method(String, int, String, int)

As you can see, there are a lot of conventions that exist.如您所见,存在许多约定。 I'm not sure that any of them has a name, and that any is really much more used than any other.我不确定它们中的任何一个都有名字,而且 any 确实比其他任何一个都更常用。 It isn't as clear as camelCase vs. snake_case for example, which almost no one contradict.例如,它不像camelCase 与snake_case 那样清楚,几乎没有人反对。

What you can keep from all that is the following: put the arguments in the order that looks the most logical and straightforward to you.您可以避免以下所有内容:按照对您来说最合乎逻辑和最直接的顺序排列参数。 The most important is to stay consist in the entire project, ie don't write isFileX(a,b) and then isFileY(b,a) for example, a fortiori if the two methods are in the same class.最重要的是在整个项目中保持一致,即不要写isFileX(a,b)然后是isFileY(b,a)例如,如果这两个方法在同一个类中,则isFileY(b,a) IN case of doubt, don't hesitate to ask other people working on your project what they think is the best.如有疑问,请不要犹豫,询问其他从事您项目的人,他们认为什么是最好的。

For your particular case, it's reasonnable to put child first because of the "this first" rule, but it's as reasonnable to put the parent first, as it's also a common convention for example in GUI frameworks.对于您的特定情况,由于“这个第一”规则,将孩子放在第一位是合理的,但将父母放在第一位也是合理的,因为这也是 GUI 框架中的常见约定。 It's up to you to decide.由你来决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM