Re: Avoiding Booleans & Danny Thorpe



Andre,

> I think that
>
> CreateFile(cmCreateNew);
>
> is much more readable than
>
> CreateFile(true);
>

1. NON-CONSTANT PARAMETERS

If the values aren't constants, the function is still unreadable.
CreateFile(MyVariable,MyFunctionCall)

and you still can't tell what the parameter are without:
CreateFile({New File} MyVariable, {Logging} MyFunctionCall)

Variables, properties, function calls, etc. all negate any advantage
for readability if the enumerated constant isn't visibile.


2. PARAMETERS BASED ON CONDITIONS

Compare
CreateFile( {New File} FileExists(MyFile), {Logging} not
(FileDate(MyFile) > Date) )

and have to code it like:
if FileExists(MyFile)
then
begin
if not (FileDate(MyFile) > Date)
then CreateFile(cfaNewFile,cfaLogging)
else CreateFile(cfaNewFile,cfaNoLogging)
end
else
if not (FileDate(MyFile) > Date)
then CreateFile(cfaExistingFile,cfaLogging)
else CreateFile(cfaExistingFile,cfaNoLogging);

One line is always more readable than 6+. It is also far
less error prone.


3. BOOLEAN UNROLLING

As in above, if you have a complex boolean statements, you
force the programmer to un-roll the statements to get cases
for the call with only enumerators. Boolean logic is tricky and
error prone already. Unrolling (possibly multiple) boolean
statements to reform them into If ... Then's is asking for trouble.


4. ENUMERATED TYPES AREN'T TRUE/FALSE

Authorization("foo", AuthorizationCompletion.Pending)
Authorization("foo", AuthorizationCompletion.Finished)

Are both wordy and deceiving. Is there some state other
than Pending or Finished? Why not Pending and NotPending
which is more accurate? Why would everyone assume that
..Pending means Not .Finished? If I only see:
Authorization("foo", AuthorizationCompletion.Pending)

why would I assume that if I didn't want pending that
Authorization("foo", AuthorizationCompletion.Finished)

would be the alternative? Why not .Stopped or .DoLater,
or .Paused, etc.


5. ENUMERATED TYPES AREN'T CLEAR EITHER

What does AuthorizationCompletion.Pending mean anyway?
How does Authorization use it anyway?


6. OTHER TYPES AREN'T SPELLED OUT FOR YOU
ANYWAY

If you use:
Authorization("foo",AuthorizationCompletion.Pending)

then what is the "foo" parameter anyway? How about:
Authorization("foo", 5, 0.245, AuthorizationCompletion.Finished)

i.e. you are going to have to look at Authorization to figure out
what it does or include:
Authorization( {Caption} "foo", {Record No} 5, {Discount %} 0.245,
{Completion} AuthorizationCompletion.Finished )


Thanks,

Brett







.



Relevant Pages

  • Re: [PATCH] drivers/scsi/aic7xxx_old: Convert to generic boolean-values
    ... There's always an issue when two people work on the same driver ... ... I'm used to register layouts, and those are simple bitfields, so I don't ... It is a fundamental difference between an integer and a boolean. ... I don't quite see how this is relevant to the readability issue? ...
    (Linux-Kernel)
  • Re: Coding Horrors, Cargo Cult Programming, and other Ghoulish Things
    ... So Ralf, if you are interested in readability and would like to remove ... a redundant test, then why not: ... If it's already boolean you don't need to check for "true". ...
    (borland.public.delphi.non-technical)
  • Re: [PATCH] drivers/scsi/aic7xxx_old: Convert to generic boolean-values
    ... I'm used to register layouts, and those are simple bitfields, so I don't ... But on the note of readability; there is in no way any constraints to only use 'false'/'true'. ... But I think it all boils down to what people are used to (I don't think there are any C++/Java-developers who complains about boolean and false/true). ... not have confusion over what types to use in the driver. ...
    (Linux-Kernel)
  • Re: Deleting an element from a list in a loop
    ... boolean so in order to keep to your own standards, ... If, on the other hand, your code suffers in readability without the ...
    (microsoft.public.vc.stl)