Boolean method naming readability

后端 未结 12 751
南笙
南笙 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条回答
  •  猫巷女王i
    2020-12-12 16:03

    There are things to consider that I think were missed by several other answers here

    1. It depends if this is a C++ class method or a C function. If this is a method then it will likely be called if (user.exists()) { ... } or if (user.isExisting()) { ... }
      not if (user_exists(&user)) . This is the reason behind coding standards that state bool methods should begin with a verb since they will read like a sentence when the object is in front of them.

    2. Unfortunately lots of old C functions return 0 for success and non-zero for failure so it can be difficult to determine the style being used unless you follow the all bool functions begin with verbs or always compare to true like so if (true == user_exists(&user))

提交回复
热议问题