简体   繁体   English

验证方法的良好命名约定是什么?

[英]What is good naming convention for validating method?

Let's say there is a method validating user's input.假设有一个验证用户输入的方法。 This method check if there is any duplication in list, and if exists, throw some kind of exception.此方法检查列表中是否有任何重复,如果存在,则抛出某种异常。

In this situation, which is proper naming for this method?在这种情况下,该方法的正确命名是什么?

  1. validateIsDuplicated验证重复
  2. validateDuplication验证重复
  3. validateNonDuplication验证非重复
  4. or else..要不然..

Since English is not my mother language, I'm not familiar with this.由于英语不是我的母语,我对此并不熟悉。 I'd appreciate it if you explain me why .如果您能解释一下原因,我将不胜感激。

I've not seen any conventions on this.我还没有看到任何关于此的约定。

Accentuate the positive强调积极的一面

I would suggest phrasing in the affirmative, stating the valid case.我建议用肯定的措辞,说明有效的情况。 Focus on what what you want to see rather than what you want to avoid.专注于你想看到的,而不是你想避免的。 Consistent use of positive logic makes the code easier to read and comprehend without having to to do Boolean logic flipping in your mind.一致地使用正逻辑使代码更易于阅读和理解,而无需在脑海中翻转 Boolean 逻辑。

For example… If you want your list to not have duplication, that means the the list has unique items, also commonly known as distinct .例如......如果您希望您的列表没有重复,这意味着该列表具有独特的项目,通常也称为distinct So state the valid case as the name of your validation method: isDistinct .所以 state 的有效案例作为您的验证方法的名称: isDistinct

boolean isDistinct( List< LocalDate > dates ) {
    return ( Set.copyOf( dates ).size() == dates.size() ) ; 
}

Per JavaBeans conventions, the leading is… means an accessor method (getter) for a property of the object.根据JavaBeans约定,前导is…表示 object 的属性的访问器方法(getter)。 If in your context this might prove confusing, choose another name such as datesListIsDistinct .如果在您的上下文中这可能会让人感到困惑,请选择其他名称,例如datesListIsDistinct


By the way, for sophisticated data validation, you may find useful Jakarta Bean Validation .顺便说一句,对于复杂的数据验证,您可能会发现有用的Jakarta Bean Validation See also specification page .另见规格页面

Technically from convention perspective, all the methods you mentioned are correct.从技术上讲,从约定的角度来看,您提到的所有方法都是正确的。 But naming conventions is a subjective topic and may vary from team to team.但是命名约定是一个主观的话题,可能因团队而异。 Finally it is about what as a team sounds more intuitive to all.最后,它是关于作为一个团队听起来对所有人来说更直观的东西。

Having said that, nonDuplicate version sounds a little better than others to me from the given options but I would like to reword it as validateNonDuplication .话虽如此,从给定的选项来看,nonDuplicate 版本对我来说听起来比其他版本好一些,但我想将其改写为validateNonDuplication The reason it sounds better than others is because your positive flow is to make sure there is no duplication while you are throwing an exception if there is a duplicate.它听起来比其他人更好的原因是因为您的积极流程是确保没有重复,如果有重复则抛出异常。 So expectations are more inline of having non duplicates.因此,期望更符合不重复的要求。

Hence in my opinion, I would pick validateNonDuplication in this case.因此,在我看来,在这种情况下,我会选择validateNonDuplication

According to Oracle Oracle naming standard :根据 Oracle Oracle 命名标准

  • Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.方法应该是动词,混合大小写,首字母小写,每个内部单词的首字母大写。

As you can see it doesn't specify anything else other than it needs to be descriptive.如您所见,除了需要描述性之外,它没有指定任何其他内容。 In your case, your examples can be more descriptive.在您的情况下,您的示例可以更具描述性。 If you method validades lists, maybe put it in. If it checks for a field in an object, specify it.如果您使用验证列表,可能会将其放入。如果它检查 object 中的字段,请指定它。 For example validateIdListDuplicate.例如 validateIdListDuplicate。

This is a subjective topic so there isn't a right answer.这是一个主观的话题,所以没有正确的答案。

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

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