Boolean method naming readability

后端 未结 12 725
南笙
南笙 2020-12-12 15:10

Simple question, from a readability standpoint, which method name do you prefer for a boolean method:

public boolean isUserExist(...)

or:

12条回答
  •  粉色の甜心
    2020-12-12 15:55

    I would go with userExists() because 1) it makes sense in natural language, and 2) it follows the conventions of the APIs I have seen.

    To see if it make sense in natural language, read it out loud. "If user exists" sounds more like a valid English phrase than "if is user exists" or "if does user exist". "If the user exists" would be better, but "the" is probably superfluous in a method name.

    To see whether a file exists in Java SE 6, you would use File.exists(). This looks like it will be the same in version 7. C# uses the same convention, as do Python and Ruby. Hopefully, this is a diverse enough collection to call this a language-agnostic answer. Generally, I would side with naming methods in keeping with your language's API.

提交回复
热议问题