Fog Creek Software
Discussion Board




Reading Code (Boolean Expressions)

Let’s say you are reading someone else’s code, you come across a Boolean Expression such as the following:

($cmsmode != 'Published')    or (((count(customproperty[name='showinprimarynavigation']) = 0) or (customproperty[name='showinprimarynavigation']/value != 'false')) and (((count(channel) > 0) and (count(.//channel[postingcount >0]) >0)) or (postingcount > 0) or (count(customproperty[name='externallinkurl']) > 0) or (count(.//channel/customproperty[name='externallinkurl']) > 0) or (count(customproperty[name='internallinkurl']) > 0) or (count(.//channel/customproperty[name='internallinkurl']) > 0)))


It’s XPath if you are wondering.

How do you read it and get a good understanding. I have been reading lots of these lately, and have devised a way to make it easer to read. Basically you just have to format it like you any other code, so that it looks like this:

(
$cmsmode != 'Published'
)
or
(
(
  (
  count(customproperty[name='showinprimarynavigation']) = 0
  )
  or
  (
  customproperty[name='showinprimarynavigation']/value != 'false'
  )
)
and
(
  (
  (
    count(channel) > 0
  )
  and
  (
    count(.//channel[postingcount >0]) >0
  )
  )
  or
  (
  postingcount > 0
  )
  or
  (
  count(customproperty[name='externallinkurl']) > 0
  )
  or
  (
  count(.//channel/customproperty[name='externallinkurl']) > 0
  )
  or
  (
  count(customproperty[name='internallinkurl']) > 0
  )
  or
  (
  count(.//channel/customproperty[name='internallinkurl']) > 0
  )
)
)

Much better.

Gary van der Merwe
Monday, August 02, 2004

Damm - not all my spaces are showing. Anyway - I think you get the picture.

Gary van der Merwe
Monday, August 02, 2004

Placing the parentheses on separate lines doesn't really help the way it looks - but, having it all run togehter isn't the most readable thing either - maybe this:

((condition1) or
(condition2) or
(condition3))
and
((condition4) or
(condition5) or
(condition6))

etc.

Greg Hurlman
Monday, August 02, 2004

I find that I would have to draw a k-map (karnaugh map) to make heads or tails out of it.

Quickie article that may help:

http://www.embedded.com/showArticle.jhtml?articleID=16100908

If I have to make something tangled like that, I usually put the k-map in a comment near the iffy statement. So if I (or someone else) has to decode my upper left hieroglyphics, then I/they don't have to spend all week at it.

Greg's idea is good too. Although I prefer to put the and/or at the beginning of the line.

Peter
Monday, August 02, 2004

I'd probably prefer it if the logic were encapsulated in well-named functions, like this:

if (suppress_primary_nav() and channel_exists() and posts_exist() and links_exist()) ...

That has the benefit of being both more readable and of localizing the logic (so if 'externallinkurl' later changes to 'external_link_url', you only change it in the function body, not everywhere it might occur).

Of course, I haven't used XPath enough to know whether such a thing would be possible or practical.

John C.
Monday, August 02, 2004

reformat it AND refactor it with variables that show the meaning of subsections:

fDontShowInNav = (count(customproperty[name='showinprimarynavigation']) = 0)
  or
  (customproperty[name='showinprimarynavigation']/value != 'false');

Dennis Atkins
Monday, August 02, 2004

To me it stands as another example of why XSLT shouldn't be used for anything but toy pages.

Egor
Monday, August 02, 2004

If you indent well you can essentially draw the parse tree sideways.
Better yet, get a pen, get paper (or a marker and a whiteboard, or your
brain with good visual short-term memory) and draw the parse tree.

Thomas E. Kammeyer
Monday, August 02, 2004

Indent.

I don't know if this will work in the forum, but this (type of thing) seems a little clearer to me.

($cmsmode != 'Published')   
or
(
    (
        (count(customproperty[name='showinprimarynavigation']) = 0) or
        (customproperty[name='showinprimarynavigation']/value != 'false')
    )
    and
    (
        (
            (count(channel) > 0) and
            (count(.//channel[postingcount >0]) >0)
        )
        or
        (postingcount > 0)
        or
        (count(customproperty[name='externallinkurl']) > 0)
        or
        (count(.//channel/customproperty[name='externallinkurl']) > 0)
        or
        (count(customproperty[name='internallinkurl']) > 0)
        or
        (count(.//channel/customproperty[name='internallinkurl']) > 0)
    )
)

S. Tanna
Monday, August 02, 2004

Egor burped:

"To me it stands as another example of why XSLT shouldn't be used for anything but toy pages."

a) It wasn't written in XSLT, you nit. it was written in XPath; they're different things. It helps to know what something is before you disregard it.

b) If all it takes is one example of badly written code to convince you not to use a language, what languages *do* you use? I can come up with the exact same example in any language which supports AND.

Aaron
Tuesday, August 03, 2004

>I can come up with the exact same example in
>any language which supports AND.

<pedant>
No you can't.  You could do it with AND and OR, or with just NAND, but not with AND alone (unless you allow NOT, but then that's just an obscure way of saying that you'd do it with NAND).
</pedant>


Tuesday, August 03, 2004

*  Recent Topics

*  Fog Creek Home