Difference between revisions of "Boolean constants"

From Dragon Age Toolset Wiki
Jump to: navigation, search
(added detail)
(spelling)
Line 40: Line 40:
 
In this case the return value of the function (TRUE/FALSE) has the same logic characteristics as the sub-expression (x == y) in the example above. So you can treat the function call as a boolean sub-expression just like (x == y) and therefore the same logic leads to the same conclusion concerning the overall expression.
 
In this case the return value of the function (TRUE/FALSE) has the same logic characteristics as the sub-expression (x == y) in the example above. So you can treat the function call as a boolean sub-expression just like (x == y) and therefore the same logic leads to the same conclusion concerning the overall expression.
  
However, testing for FALSE or not TRUE is perfectly acceptable:
+
However, testing for FALSE or NOT TRUE is perfectly acceptable:
 
<pre>
 
<pre>
 
if( IsObjectValid( oObject) == FALSE) ... <-- this is not redundant
 
if( IsObjectValid( oObject) == FALSE) ... <-- this is not redundant

Revision as of 23:57, 24 October 2009

This page was generated by Sunjammer's Dragon Age Script Parser.

The parser extracted and matched all the information required to the best of its ability however the parser relies on the source file, and especially a function's comments, to be correctly formatted. If the source file was not correctly formatted the information presented may be incomplete.

This page should be reviewed by a knowledgeable scripter as it may require updating. If an issue with the source file is identified it should be reported to BioWare.

Please remove the {{Generated}} tag once the page has been confirmed or corrected.

Source: script.ldf
Constant name Type Value Description Source
FALSE int 0 script.ldf
TRUE int 1 script.ldf

Remarks

If you test for a variable's truth value by testing if it is equal to TRUE you may not get the correct result, since TRUE == 1 is a true expression but TRUE == 2 is not. You may wish to instead test if your variable is not equal to FALSE.

Note also that it is never necessary, and furthermore wasteful, to explicitly "test for TRUE" or "test for NOT FALSE" in a boolean expression anyway. One would never write, for example:

if( (x == y) == TRUE ) ...  <-- redundant comparison with TRUE
if( (x == y) != FALSE ) ...  <-- redundant comparison with FALSE

because the sub-expression (x == y) will evaluate to TRUE or FALSE already. Additionally comparing the result to TRUE (or NOT FALSE) gives the exact same result for the entire expression as leaving the extraneous comparison out completely does. Thus the comparison with TRUE (or NOT FALSE) is redundant, unnecessary and a wasteful computation.

The same logic applies to expressions involving function calls. If a function returns a TRUE/FALSE value, it would be a redundant waste of CPU cycles to test if the returned value was TRUE.

if( IsObjectValid( oObject) == TRUE) ... <-- this is redundant as well
if( IsObjectValid( oObject) ) ... <-- same thing, one less comparison

In this case the return value of the function (TRUE/FALSE) has the same logic characteristics as the sub-expression (x == y) in the example above. So you can treat the function call as a boolean sub-expression just like (x == y) and therefore the same logic leads to the same conclusion concerning the overall expression.

However, testing for FALSE or NOT TRUE is perfectly acceptable:

if( IsObjectValid( oObject) == FALSE) ... <-- this is not redundant
if( IsObjectValid( oObject) != TRUE) ... <-- this is not redundant either
if( !IsObjectValid( oObject) ) ... <-- but this might be the best method

The identifier TRUE is only really useful for defining default values of boolean type parameters in a function definition, passing a true value to a function with a boolean parameter when you call it, or setting the value of a boolean type variable. There is no other place where you need it.

void Dance( int bChaCha = TRUE)... <-- default value in function def
SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call
int bStillLooking = TRUE; <-- set a variable value

Some will probably take the position that having the extra comparison in there makes the code more readable and obvious. It would be difficult to argue that point especially in some more complex expression situations. But it is functionally unnecessary and adding it does waste CPU cycles in exchange for that marginal increase in readability. Learning to read experessions without it having to be there would benefit anybody.