Boolean functions

Share this topic:



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"!
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Return to Intriguing World of Logic

Who is online

Users browsing this forum: No registered users and 2 guests


Boolean functions

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





cron
free counters

eXTReMe Tracker