|
My brain is built of paths and slides and ladders and lasers and I have invited all of you to enter its pavilion. My brain, as you enter, will smell of tangerines and brand-new running shoes.
 Apoch's Peeve Corner |
Posted - 7/23/2007 11:24:56 PM | Alright kiddies, it's time for another episode of... Apoch's Pet Peeve Corner! Yayy!
This is the segment of the show where Apoch picks some random thing and rants about it for a few minutes. Today, we're going to talk about: boolean valued functions.
(By the way, all of this discussion can be applied to any programming language; I'll be using C++ since it's a widely recognizable syntax. Feel free to adjust the lessons of this rant to the language of your choice.)
So let's say we have a complicated operation, called Frobnicate. This operation does something which may fail, and may succeed. We need to encapsulate the Frobnicate operation into a function call.
Let's write a prototype for our new function: bool Frobnicate();
That was easy, right? On to step two!
BZZZT - WRONG. Please turn in your programmer's license and shuffle away in shame. You have failed.
Frobnicate is not a boolean function. But Apoch, you protest, surely it can either succeed (true) or fail (false)! Yeah, but it can also succeed (false) or fail (true) depending on your style.
A boolean function should only be used for a predicate - a function which asks a yes/no question. So if we have a function IsBork, it can be boolean: either we're Bork, or we are not Bork. But Frobnicate does not ask a yes/no question. Therefore we should not use a boolean-valued function.
There are two ways to handle this situation and make Frobnicate correct:
Error Returns
If your code style uses error returns (that is, functions return whether or not they succeed, and possibly return special constants that indicate the result of the function call) then your solution is simple.
You should have, as part of your standard header package (and please tell me you have a standard set of precompiled headers/boilerplate code/etc. for your projects!) a simple enumeration which handles success/fail returns. Instead of returning true/false, return Success or Fail. This is extremely common in C and C++ land; Win32 and DirectX use this style, for instance.
The solution here looks like this: ResultCode Frobnicate();
Exceptions
If your coding style is to use exceptions, Frobnicate should probably not return a value. Either it throws an exception (something is wrong) or it does not (everything is fine).
The solution looks like this: void Frobnicate();
There you go. Now please, stop creating bool functions for success/fail results. Also, please don't bother assigning a return type to the function if all it ever does is return true;.
Thank you. This has been Apoch's Pet Peeve Corner.
Up next: dancing clowns juggling rabid hyenas! Don't go away!
| |
In locus hic, omnes res dementes sunt.
|
| S | M | T | W | T | F | S | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | |
OPTIONS
Track this Journal
ARCHIVES
July, 2009
June, 2009
May, 2009
April, 2009
March, 2009
February, 2009
January, 2009
October, 2008
September, 2008
August, 2008
July, 2008
June, 2008
May, 2008
April, 2008
March, 2008
February, 2008
January, 2008
December, 2007
November, 2007
October, 2007
September, 2007
August, 2007
July, 2007
June, 2007
May, 2007
April, 2007
March, 2007
February, 2007
January, 2007
December, 2006
November, 2006
October, 2006
September, 2006
August, 2006
July, 2006
June, 2006
May, 2006
April, 2006
March, 2006
February, 2006
January, 2006
December, 2005
November, 2005
October, 2005
|