Page 1 of 1

Boolean functions

PostPosted: 21 Feb 2013, 22:04
by Ursego
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"!