简单但反复出现的命名问题
假设我们有一个方法:
public String wishes(Date birthday) {
String birthayDateString = convertToString(birthay);
...
}
我想知道现在称为“birthayDateString”的字符串的最佳名称是什么。 该字符串表示转换为文本的日期。 我不能将其命名为“birthay”,因为这个名字已经被使用过。 将其命名为“birthdayString”或“birthdayDateString”是否违反了某些命名约定良好实践规则?
Let's say we have a method:
public String wishes(Date birthday) {
String birthayDateString = convertToString(birthay);
...
}
I wonder what's the best name to give to the string called now "birthayDateString". This string represents date converted to text. I can't name it "birthay" beause this name is alredy used. Does nameing it "birthdayString" or "birthdayDateString" violate some naming convention good practice rules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上看起来不错。
我个人更喜欢
birthdayStr
- 更简短、切中要点 - 使其既有意义又简洁。产量:
That actually looks fine.
I personally would prefer
birthdayStr
- shorter and to the point - making it both meaningful and yet concise.Yielding:
不,我认为您没有违反任何“最佳实践”标准。 在这种情况下,我经常使用诸如
birthdayText
或birthdayStr
之类的变量名称。不过,总的来说,我会尝试使变量名称尽可能有意义 - 如果出现命名冲突,我不会自动添加“Str”或“Text”。 但在本例中,它只是日期的字符串表示形式,所以我认为它足够有意义。
No, I don't think you're violating any "best practice" standards. In cases like those, I often use variable names like
birthdayText
orbirthdayStr
.In general, though, I try to make variable names as meaningful as possible—I don't just automatically tack on "Str" or "Text" if there's a naming collision. But in this case, it's just the string representation of the date, so I'd say it's meaningful enough.
随便你怎么称呼它。
变量名称仅在当前方法块的范围内(如果您的代码正确分解/封装,则该范围应该相当小)。
就我个人而言,我会使用
birthdayStr
只是为了在该特定方法中清晰起见。]我还将这些 MethodNames 大写为
Wishes
和ConvertToString
Call it whatever you want.
The variable name will only be in scope for the current method block (which should be reasonably small if you have your code properly broken up/encapsulated).
Personally I'd go with
birthdayStr
just for clarity in that particular method.]I'd also Upper case those MethodNames to
Wishes
andConvertToString