Scripting Bugs

Things you’d like to miss in the future...
Forum rules
:warnred20: :warnred20: :warnred20: :warnred20: :warnred20: READ THIS AND DO IT!!! :warnred20: :warnred20: :warnred20: :warnred20: :warnred20:

:info: Please include the following information:
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".

:warnred20: :warnred20: :warnred20: :warnred20: :warnred20: READ THIS AND DO IT!!! :warnred20: :warnred20: :warnred20: :warnred20: :warnred20:
admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

Whoops, confirmed! Yep, coming...

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Scripting Bugs

Post by nerdweed »

It appears that the second condition is unnecessarily evaluated even if not required

if (1==2 && property("#11", "<curitem>") == "Picture") {
echo "Should never execute - but I checked both" ;
} else {
echo "I took a little longer to come here" ;
}

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting Bugs

Post by PeterH »

I would not see this as a (kind of) bug.

I think XY does what's written: it calculates the value of the boolean expression - and then decides the 'if'.
Though it might be a bit more performant, if the 2nd operand of an 'and' will not be calculated if the first is '0'. But bad (for you), if the operands are calculated right to left :shock: (Don't say this is not allowed... :lol: )

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Scripting Bugs

Post by nerdweed »

If it's right to left, I can simply switch them. But it doesn't appear to be that way either

admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

This optimization could be too smart. Look at this (air code). You might want to try the second write even if the first failed.

Code: Select all

if (writefile($file1, $data1) == 0 && writefile($file2, $data2) == 0) {
  echo "Both failed!";
}

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Scripting Bugs

Post by nerdweed »

That air code would say both failed even if one succeeded. If I would want one stuff to be completed regardless of other, I wouldn't put it in relation to the first.

It would be good if that smartness can be put in place - this would avoid expensive operations to the right if the success/failure of the condition has already been decided.

Edit: We can split the operation in two (but that requires copying the else as well)

admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

No. Try this:

Code: Select all

$file1 = "a*.txt"; $file2 = "b.txt"; $data1 = "X";
  if (writefile($file1, $data1) == 0 && writefile($file2, $data1) == 0) {
   echo "Both failed!";
  }

admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

This is what I meant by "This optimization could be too smart.". I cannot know what the code intended or expects. And I think the default assumption is that both parts of the AND-term are processed.

You can easily optimize your code by nesting:
Instead of

Code: Select all

if A and B then
you do:

Code: Select all

if A then
 if B then

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Scripting Bugs

Post by nerdweed »

Correct. After numerous edits.
1 and 1 = 1
0 and 0 = 1

I should use the Or operator.

Edit: Note to self - Read the truth tables again.
Last edited by nerdweed on 18 May 2014 21:50, edited 1 time in total.

admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

0 and 0 = 1 ???

In which part of the universe do you live?

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: Scripting Bugs

Post by nerdweed »

Oh, I was correct earlier. I thought I boo-ed earlier but boo-ed on the second instance.
In which part of the universe do you live?
Gravity pushed me at that instance.

But if first is false, why should the second be checked - hasn't the failure been determined.

If (1==0 && 1==1) {
;
}

When first is true, why check for the second.
if (1==1 || 1==0) {
;
}

Shouldn't this be short circuit operators like in PHP

The problem with nesting is the Else part.

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting Bugs

Post by PeterH »

nerdweed wrote:But if first is false, why should the second be checked - hasn't the failure been determined.
Basically this is a bool expression - and is simply calculated by bool logic. I.e. it is and AND instruction - and is executed even if the first operand of it is false.

For your wish the evaluation logic would have to be expanded to test the 1st operand for false, and in this case abend the bool calculation. This would be analog to the "nested if" Don gave as an example. And would have the implications Don mentioned.

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Scripting Bugs

Post by LittleBiG »

I have a real life example where I could have used what nerdweed suggests. In a script I get the recent paths and I would like to create a list about the existing folders. If the actually checked folder is a network folder, I want to add it to my list, without further check, assuming that it is existing. (because the exists(network_folder) slows the script down significantly) I tried this:

Code: Select all

if (substr($NextPath,0,2) == "\\" or exists($NextPath) > 0) {add the path to my list}
If the first part is true, no need the run the second part, the whole expression will be true, because of the "or". But XY runs the second part anyway, slowing down the script significantly. I had to rewrite the script in an unfortunate way:

Code: Select all

if (substr($NextPath,0,2) == "\\") {add the path to my list}
elseif (exists($NextPath) > 0) {add the path to my list}
else {not accessible path, go to the next one}
As you can see I had to double the adding part. :veryconfused: The "add the path to my list" part is not as simple as I show it in my example, so I have to keep the two part in sync manually.

admin
Site Admin
Posts: 65456
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting Bugs

Post by admin »

No, what you did is good coding. To avoid the doubling of code simply use a little local flag:

Code: Select all

if (substr($NextPath,0,2) == "\\") {$add = 1;}
elseif (exists($NextPath) > 0) {$add = 1;}
else {$add = 0;}

if ($add == 1) {add the path to my list}

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Scripting Bugs

Post by LittleBiG »

admin wrote:To avoid the doubling of code simply use a little local flag
Why is it so obvious after you have written it down? And why not before? :biggrin: Thanks!

Post Reply