Boolean functions names

Link to this posting

Postby Ursego » 21 Feb 2013, 22:04

If a function returns Boolean then its name should convey the meaning of the returned value to make the business logic in the calling scripts easily understandable. Calling that function as a condition of an if statement must produce a correct real-English sentence.

Here are some examples of well-named Boolean methods: rowIsValidated, ordersAreOk, fileNameExists. A strange tradition dictates to put the words "Is"/"Are"/"Does"/"Do" in the beginning (like isRowValidated, areOrdersOk, doesFileNameExist etc.), but I personally don't like that approach because it produces incorrect English in the calling scripts: we say "if row is validated then do something", not "if is row validated...". Of course, it's not an issue of high importance - you can even omit "Is"/"Are" at all, and the names still will be fine: rowValidated, ordersOk.

Never use verbs in imperative to name Boolean methods. For example, the following names are absolutely unacceptable for methods returning Boolean: validateRow, checkOrders, checkFileExistence - we don't say "if validate row then save", "if check orders then print", "if check file existence then open file"! :twisted:
User avatar
Site Admin
Posts: 120
Joined: 19 Feb 2013, 20:33

IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:

free counters

eXTReMe Tracker