Why SSI condition != || != doesn't work? - include

I can't get working this conditional expression
<!--#if expr="$DOCUMENT_NAME!=index.html || $DOCUMENT_NAME!=links.html" -->
while this one without ! before = works perfect
<!--#if expr="$DOCUMENT_NAME=index.html || $DOCUMENT_NAME=links.html" -->
What's the problem? I get no error simply != doesn't work though || with other condition but works for single condition.

This is because = and != are hardly the same operator. Note that, by De Morgan's law (which I also explained in this old post),
a != b || c != d
is equivalent to
a = b && c = d
which is never true for x = a && x = b where a != b.
Changing the binary operator requires changing the conditionals as well to be equivalent.
Thus, by the above logic,
$DOCUMENT_NAME!=index.html || $DOCUMENT_NAME!=links.html
is equivalent to
$DOCUMENT_NAME=index.html && $DOCUMENT_NAME=links.html
which cannot be true as $DOCUMENT_NAME can be "index.html" or "links.html" but not both.
However, the 2nd snippet,
$DOCUMENT_NAME=index.html || $DOCUMENT_NAME=links.html
"works" because there is not the logical never-true fallacy mentioned above. It will be true when $DOCUMENT_NAME is either "index.html" or "links.html".
Some languages/values will violate the above equivalency .. but that is another topic.

Related

Until loop keeps runing even when the phrase is true?

I have the following code:
until (#world.exists? decision || decision == '')
UiHandler.print_error(UiHandler::NO_TILE)
UiHandler.print_turn_message
decision = gets.chomp
end
which should allow the player to skip a turn by entering an empty line. But for some reason the until loop keeps running even when the condition is true
i.e. passing in '1 1' does work and stop the loop, since it exists in world, but passing nothing doesn't, even though puts (#world.exists? decision || decision == '') gives 'true'
What would cause an until loop not to stop even when the condition is met?
Fix is
(#world.exists?(decision) || decision == '')
Otherwise - #world.exists? decision || decision == '' is being treated as #world.exists?(decision || decision == ''), which is not correct expression, you intended to write.
As decision is a string object, which in Ruby is considered as truth value, decision || decision == '' (in the code written by you) will be evaluated as true too. This decision will be passed as a method argument to the method #world.exists? always.

BizTalk - Multiple Conditions in Decide Shape

In my BizTalk Decide shape, How can I handle below conditions in one decide shape node?
code == 'code1'
code == 'code2'
code == 'code3'
code == 'code4'
I'm trying to do, this by
(code == 'code1' Or code == 'code2' Or code == 'code3' Or code == 'code4' )
But It is not working, please suggest.
The Decide Shape takes any (mostly) C# formatted condition so you would use:
(code == "code1") || (code == "code2") || (code == "code3") || (code == "code4")
You have to use the double quote since the single quote specifically means char data, not string.

How to I set a ruby variable based on an if-then decision tree

I want to write the following:
variable = if x
a
else
if y
b
else
c
end
end
Where if x is true, variable equals a. If x is false and y is true, variable equals b. If x and y are both false, variable equals c.
I thought this was valid ruby syntax but when I try it, the variable is always set to nil. Why is this and how do I set it correctly?
I am using ruby 1.9.3 btw
Edit: FALSE ALARM. In my example, c was set to nil which I thought was erroneous. My original syntax worked fine. I'm not sure of the StackOverflow etiquette on whether I should delete this question, advise is welcome.
Thanks for the quick responses.
Do not make it complicated! There is nothing wrong with writing
variable =
if x then a
elsif y then b
else c
end
No one will look at this code and not figure out what has happend.
There two main variants:
With a if-else-end tree:
variable =
if x
a
else
if y
b
else
c
end
end
and as a logical boolean algebra expression:
variable = x && a || ( y && b || c )
That is the same as:
variable = x && a || y && b || c
The first variant is usually preferred, then you are using complex code or calculation inside the if-else-end blocks. The second, when the simple read/logical constructions is used.
If you have an error or get an invalid result in your expression, just check your logical expression.

Binary tree recursive problem

I found this problem and I am not very sure if my approach approach is right:
"A binary tree can be encoded using
two functions l and r such that for a
node n, l(n) gives the left child of
n(or nil if there is none) and r(n)
gives the right child(or nil if there
is none).Let Test(l,r,x) be a simple
recursive algorithm for taking a
binary tree encoded by the l and r
functions together with the root node
x for the binary tree, and returns
"yes" if no node in the tree has
exactly one child. Give the pseudocode
for this algorithm."
I tried this:
test(l,r,x)
if((l(x)!=null && r(x)!=null) || (l(x)==null && r(x)==null))
return "yes"
else return "no"
if(test(l,r,l(x))=="yes") test (l,r,l(x)) else return "no"
if(test(l,r,r(x))=="yes") test (l,r,r(x)) else return "no"
return "yes"
Is it correct? If l and r are functions, why are they passed as normal parameters when the function is called?
Thank you in advance for your answers!
You have three basic conditions: both children are null, one child is null, or neither child is null. I'd test them in that order as well (because it simplifies the logic a bit):
if l(x) == null && r(x) == null
return true;
if l(x) == null || r(x) == null // only need inclusive OR, due to previous test.
return false;
return test(l, r, l(x)) && test(l, r, r(x))
As far as passing l and r as parameters goes, it all depends: some languages (e.g., functional languages) allow you to pass a function as a parameter. Others (e.g., C, C++, etc.) have you pass a pointer to a function instead -- but it's pretty much irrelevant. A few won't let you pass anything like a function, in which case you'd have to hardwire it in. In this case, you're not really gaining much (anything?) by passing l and r as parameters anyway. Passing a function as a parameter is useful primarily when/if the receiving function doesn't know what function it's going to receive a priori (which it does here).
the first thing you do is either return yes or no, so the last part is unreachable.
I would change it so that if you have one child, you return no, otherwise you return yes or no depending on if your children meet the criteria.
test(l,r,x)
if((l(x)!=null && r(x)==null) || (l(x)==null && r(x)!=null))
return "no"
if(l(x) == null && r(x) == null)
return "yes"
return test(l,r,l(x)) && test(l,r,r(x))
I don't think this is correct. The problem I see is in the first step:
if((l(x)!=null && r(x)!=null) || (l(x)==null && r(x)==null))
return "yes"
else return "no"
The problem is that you cannot determine the 'yes' for the entire tree at the first step. What you have to do is break it up into components:
if this node has both children
return the result of test(l,r,l(x)) && (test(l,r,r(x))
if this node has no children
return true
if this node has 1 child
return false
as per your last question ("If l and r are functions, why are they passed as normal parameters when the function is called?"), the answer is that they don't have to be passed as parameters. That's just the notation they chose when they said "A binary tree can be encoded using two functions l and r [...]"

IF statement formatting best-practise, what's your style?

Looking to improve my IF statement, and I want to keep my code looking pretty
This is what I am currently doing, is it readable, any room for improvement?
SomeObject o = LoadSomeObject();
if( null == o
||
null == o.ID || null == o.Title
||
0 == o.ID.Length || 0 == o.Title.Length
)
I don't have anyone to ask around. That's why I came here in first place. Please don't close my question :(
I always try and avoid complex boolean expressions for the sake of the next guy, but if I had to write an expression that didn't easily go on one line I would format it as follows:
if (value1 == value2 ||
value3 == value4 ||
value5 == value6 ||
value7 == value8) {
executeMyCode();
}
Your verbosity is leading to a less readable code, I think the following format is best:
if ( null == o || null == o.ID || null.Title || 0 == o.ID.Length || 0 == o.Title.Length )
{
// do stuff
}
We all have high resolution/widescreen displays for a reason, there's no reason to lock your code at some horribly short syntax. Also, I would simply create a function named IsIDEmpty so that the code could look like
if ( IsIDEmpty(o) )
{
// do stuff
}
to keep the code simpler & cleaner. The function would perform the actual checks and return a boolean. I'm sure this is something you might have re-use for anyways, plus it serves as a simple way for code to be more self documenting/commenting.
For the simplest formatting of what you have, I would go with one per line.
if(null == o
|| null == o.ID
|| null == o.Title
|| 0 == o.ID.Length
|| 0 == o.Title.Length)
Even better would be if you could refactor the condition such that it fits on one line. I find that a large number of || or && is usually difficult to read. Perhaps you can refactor it out into a function and be left with:
if(myFunction(...))
My rule of thumb: Avoid any format that a semi-smart auto-formatter can't reproduce.
Having a defined set of formats and a automated tool/template/configuration that actually produces code in that format is a big plus, in my opinion.
And if your code is still unreadable after it has been auto-formatted, then chances are that you need to refactor anyway.
I would either put it all on one line if it fits (which this one clearly doesn't). With this, I would put the || consistently at the start or end of line:
if( null == o ||
null == o.ID ||
null == o.Title ||
0 == o.ID.Length ||
0 == o.Title.Length
)
or
if( null == o
|| null == o.ID
|| null == o.Title
|| 0 == o.ID.Length
|| 0 == o.Title.Length
)
You could have >1 condition on a line, the positioning of || is more important I think.
I'm ignoring the fact that null.Title doesn't seem to make much sense
I find it quite distracting, to be honest. Mostly because the '||' start making funny patters.
I much rather prefer something like
if ( o == null || o.ID == null || null.Title ||
o.ID.Length == 0 || o.Title.Length )
or even better, keep it in a single line if possible.
I think it's pretty unreadable.
The affection of putting the constant first always seems a bit odd to me - most compilers can be persuaded to warn if they find an assignment in a conditional.
Then you're testing for null for two different things, then for zero length for two different things - but the important thing is not the length check, but the member you're checking. So I'd write it as
if (o == null ||
o.ID == null || o.ID.length == 0 ||
o.Title == null || o.Title.Length == 0)
Rather than making up a standard for just this one question - I would suggest adopting an existing coding standard for whatever language you are using.
For example:
GNU Coding Standards
http://www.gnu.org/prep/standards/
Code Conventions for the Java Programming Language
http://java.sun.com/docs/codeconv/
.NET Framework General Reference Design Guidelines for Class Library Developers
http://msdn.microsoft.com/en-us/library/czefa0ke.aspx
Generally I'm with TravisO on this, but if there are so many conditions in your if() statement that it just gets crazy long, consider putting in its own little function instead:
bool wereTheConditionsMet()
{
if( NULL == 0 )
return true;
if( NULL == o.ID )
return true;
: : // and so on until you exhaust all the affirmatives
return false;
}
if ( wereTheConditionsMet() )
{
// do stuff
}
It is a lot easier to convey the intent of a well-named predicate function than an endless string of ||s and &&s.
It is not readable.
This is how I do Really Long Ifs(or those I have to twiddle a lot).
if(
o == null ||
o.ID == null ||
o.Title == null ||
o.ID.Length == 0 ||
o.Title.Length == 0
)
For yours, I would do a single line.
if(o == null || o.ID == null || o.Title == null || o.ID.Length == 0 || o.Title.Length == 0)
Or, if you are using C++, I'd do something like this:
if(!o)
{}
if(! (o.ID && o.Title && o.Length))
{}
...since it separates creation from correctness.
However, caveat emptor, I've been accused of bloated LOC due to my fondness for newlines.
Use an automated code formatter, and configure it appropriately.
Write a method like isPresent(String) that checks the String argument for not null and for not empty (zero length).
Rewrite the original conditional to use the new isPresent(String) method, probably all on one line.
I usually do something like:
if(x < 0 || x >= width
|| y < 0 || y >= height)
{
/* Coordinate out of range ... */
}
The first y and x line up in a monospace font, which is nice, and I'm not confused by half-indentions.
This method works best when doing similar comparisons. Otherwise, I usually split up my if's.

Resources