Quantcast
Channel: ArcherPoint, Inc. - Developer
Viewing all articles
Browse latest Browse all 388

Elegant Code Part II - Boolean Logic and Explicit Conditional Syntax

$
0
0

Elegant Code Part II - Boolean Logic and Explicit Conditional Syntax

In NAV only: Does this statement return True or False?
OK := (True = False AND True = False);

Think of the answer before reading on or testing in NAV.

Everybody knows that TRUE will never = FALSE. This is the essence of Boolean logic(data type that is denoted by one of two values, 1 or 0, true or false, yes or no). It is basically a bit. It's either ON or OFF. That's what makes computers compute.

So, what is going on here? Couple things: First, the AND operator. NAV evaluates the AND prior to "=" operator in the equation. So, NAV is evaluating FALSE prior to the last "=" sign, FALSE = FALSE, which returns TRUE. Second, this is poorly written code that can be easier read by explicit formatting, i.e., parenthesis.

Here's another similar but different equation that returns FALSE instead of TRUE. Correct, but, still poorly written.
TRUE = FALSE AND FALSE = TRUE

Here, NAV is again evaluating FALSE prior to the last "=" sign, FALSE = TRUE, which returns FALSE.

Explicitly formatting your code by using parenthesis will correct the logic so NAV evaluates the code the way humans would evaluate it off the top of their head.

So, this will return FALSE as expected:
(True = False) AND (True = False);

Same code, just parenthesis around each condition that you want to evaluate. The reason parenthesis work here is because NAV evaluates parenthesis before any other operator in the equation.

This is the order precedence that NAV uses.*
1> .(fields in records), [] (indexing), () (parentheses), :: (scope)
2> NOT, - (unary), + (unary)
3> *,/,DIV, MOD, AND, XOR
4> +, -, OR
5> >, <. >=, <=, = <>, IN
6> .. (range)

*Notice the "OR" is evaluated AFTER "AND".

The above list is in NAV Help(F1 from code window). Search word = Operator. It's at the bottom.

Here's a couple interesting equations that don't involve parenthesis:
TRUE?
No [ FALSE AND FALSE ]
Yes[ TRUE AND TRUE ]
No [ TRUE AND FALSE ]

Not So Elegant Code returns FALSE(This is the answer to the initial question. Hope you got it correct):
OK := (True = False AND True = False);

Elegant Code returns TRUE:
OK := (True = False) AND (True = False);

So, there you go. I hope you learned a bit.

Here’s a chunk of code you can use to return the result of many permutations of elegant and not so elegant:
MESSAGE(FORMAT(TRUE = FALSE AND TRUE = FALSE) + ' [ TRUE = FALSE AND TRUE = FALSE ]' + '\' +

               FORMAT(TRUE = FALSE AND FALSE = TRUE) + ' [ TRUE = FALSE AND FALSE = TRUE ]' + '\' +

               FORMAT(TRUE = TRUE AND FALSE = FALSE) + ' [ TRUE = TRUE AND FALSE = FALSE ]' + '\' +

               FORMAT(FALSE AND FALSE) + ' [ FALSE AND FALSE ]' + '\' +

               FORMAT(TRUE AND TRUE) + ' [ TRUE AND TRUE ]' + '\' +

               FORMAT(TRUE = FALSE = TRUE) + ' [ TRUE = FALSE = FALSE ]' + '\' +

               FORMAT(TRUE AND FALSE) + ' [ TRUE AND FALSE ]' + '\');

 

MESSAGE(FORMAT((TRUE = FALSE) AND (TRUE = FALSE)) + ' [ (TRUE = FALSE) AND (TRUE = FALSE) ]' + '\' +

               FORMAT((TRUE = FALSE) AND (FALSE = TRUE)) + ' [ (TRUE = FALSE) AND (FALSE = TRUE) ]' + '\' +

               FORMAT((TRUE = TRUE) AND (FALSE = FALSE)) + ' [ (TRUE = TRUE) AND (FALSE = FALSE) ]' + '\' +

               FORMAT(FALSE AND FALSE) + ' [ FALSE AND FALSE ]' + '\' +

               FORMAT(TRUE AND TRUE) + ' [ TRUE AND TRUE ]' + '\' +

               FORMAT(TRUE AND FALSE) + ' [ TRUE AND FALSE ]' + '\');

 

 

 


Viewing all articles
Browse latest Browse all 388

Trending Articles