[haiku-development] Re: Launchbox 80 char limit fail

  • From: John Scipione <jscipione@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Sat, 14 May 2011 15:06:27 -0400

>
> > I think one could remove the parenthesis and get the same result but I
> > wasn't sure if there is some sort of short circuiting behavior that was
> > being relied on here so I left the parens alone.
>
> How would the parens change the short circuit behavior? The ||
> operator is guaranteed left to right, even with parens.


Hike Danakian,

After writing a test program I now agree with you:

#include <stdio.h>

int main(void)
{
    int i, j, k;

    for (i = 0; i <= 1; i++) {
        for (j = 0; j <= 1; j++) {
            for (k = 0; k <= 1; k++) {
                if (i || (j || k)) {
                    printf("case (i || (j || k)) i:%d, j:%d, k:%d,
result:true\n", i, j, k);
                } else {
                    printf("case (i || (j || k)) i:%d, j:%d, k:%d,
result:false\n", i, j, k);
                }
                if (i || j || k) {
                    printf("case (i || j || k)   i:%d, j:%d, k:%d,
result:true\n", i, j, k);
                } else {
                    printf("case (i || j || k)   i:%d, j:%d, k:%d,
result:false\n", i, j, k);
                }
            }
        }
    }

    return 0;
}

Results:

$ ./test
case (i || (j || k)) i:0, j:0, k:0, result:false
case (i || j || k)   i:0, j:0, k:0, result:false
case (i || (j || k)) i:0, j:0, k:1, result:true
case (i || j || k)   i:0, j:0, k:1, result:true
case (i || (j || k)) i:0, j:1, k:0, result:true
case (i || j || k)   i:0, j:1, k:0, result:true
case (i || (j || k)) i:0, j:1, k:1, result:true
case (i || j || k)   i:0, j:1, k:1, result:true
case (i || (j || k)) i:1, j:0, k:0, result:true
case (i || j || k)   i:1, j:0, k:0, result:true
case (i || (j || k)) i:1, j:0, k:1, result:true
case (i || j || k)   i:1, j:0, k:1, result:true
case (i || (j || k)) i:1, j:1, k:0, result:true
case (i || j || k)   i:1, j:1, k:0, result:true
case (i || (j || k)) i:1, j:1, k:1, result:true
case (i || j || k)   i:1, j:1, k:1, result:true

Same result, so you are right, the parenthesis are superfluous.

With this confirmed I have refactored the patch to remove the unnecessary
parens. While doing so, I also collapsed the nested ifs into a single
if/else if block. Patch below:


--- src/apps/launchbox/LaunchButton.cpp
+++ src/apps/launchbox/LaunchButton.cpp
@@ -196,20 +196,18 @@ void
 LaunchButton::MouseMoved(BPoint where, uint32 transit,
  const BMessage* dragMessage)
 {
- if ((dragMessage && (transit == B_ENTERED_VIEW || transit ==
B_INSIDE_VIEW))
- && ((dragMessage->what == B_SIMPLE_DATA
- || dragMessage->what == B_REFS_RECEIVED) || fRef)) {
- if (!fAnticipatingDrop) {
- fAnticipatingDrop = true;
- Invalidate();
- }
- }
- if (!dragMessage || (transit == B_EXITED_VIEW || transit ==
B_OUTSIDE_VIEW)) {
- if (fAnticipatingDrop) {
- fAnticipatingDrop = false;
- Invalidate();
- }
+ if (fAnticipatingDrop && (!dragMessage || transit == B_EXITED_VIEW
+ || transit == B_OUTSIDE_VIEW)) {
+ fAnticipatingDrop = false;
+ Invalidate();
+ } else if (!fAnticipatingDrop && ((dragMessage
+ && (transit == B_ENTERED_VIEW || transit == B_INSIDE_VIEW))
+ && (dragMessage->what == B_SIMPLE_DATA
+ || dragMessage->what == B_REFS_RECEIVED || fRef))) {
+ fAnticipatingDrop = true;
+ Invalidate();
  }
+
  // see if we should create a drag message
  if (_HasFlags(STATE_TRACKING) && fRef) {
  BPoint diff = where - fDragStart;


Thank you,

John Scipione

Other related posts: