Re: Avoiding Booleans & Danny Thorpe
- From: Andre Kaufmann <andre.kaufmann.bei@xxxxxxxxxxx>
- Date: Mon, 31 Oct 2005 06:23:15 +0100
Brett Watters wrote:
Andre,
[...]
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.
Why should they negate the advantage ? They don't have the advantage, that's true, since they don't have a single state which can be expressed by proper naming, but they must have a state assigned, where you will have an advantage.
And you can still can name the variables according to their function:
CreateFile(newFileState, loggingState)
2. PARAMETERS BASED ON CONDITIONS
Compare
CreateFile( {New File} FileExists(MyFile), {Logging} not (FileDate(MyFile) > Date) )
and have to code it like:
> [...]
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.
I agree. But I don't have to code it with 6 lines that way.
CreateFile( ShallFileBeCreated(MyFile), LogginState());
function ShallFileBeCreated(FileName : string) : CreateType .....
And by the way. I wouldn't suggest enumeration types in any case. If they don't enhance readability in a single case don't use them.
3. BOOLEAN UNROLLING
See above.
4. ENUMERATED TYPES AREN'T TRUE/FALSE 5. ENUMERATED TYPES AREN'T CLEAR EITHER
As I stated above use enumerations where they are appropriate and where they make code more readable. The rest is appropriate naming of variables.
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 )
It's not forbidden to combine comments and enumerations ;-). But I would prefer something like:
Authorization( "foo", // Caption
5, // Record No
0.245, // Discount
Completion, // AuthorizationCompletion.Finished)Thanks, Brett
Thanks too ;-) Andre .
- References:
- Avoiding Booleans & Danny Thorpe
- From: Kyle A. Miller
- Re: Avoiding Booleans & Danny Thorpe
- From: Brett Watters
- Avoiding Booleans & Danny Thorpe
- Prev by Date: Re: To JK - Upgrading to BDS2006
- Next by Date: Re: JK in Sydney!
- Previous by thread: Re: Avoiding Booleans & Danny Thorpe
- Next by thread: Re: Avoiding Booleans & Danny Thorpe
- Index(es):