MacRumors Forums programming ifs help.

 Nov 9, 2012, 05:01 PM #1 farmerdoug macrumors 6502a   Join Date: Sep 2008 programming ifs help. I'm trying to figure this out but if someone can tell me in the meantime I'd appreciate it. Can I do this without using rule1 and rule2? Code: ```rule1 = rule2 = 0 if (obufs[1][row*FOCPLWIDTH + col]/obufs[0][row*FOCPLWIDTH + col] > 1.5) rule1 = 1; if (rule1 == 1) { sum = 0; for ( i = -1; i < 2; i++) for ( j = -1; j < 2; j++) if (!(i == 0 && j == 0)) sum += obufs[1][(row - i)*FOCPLWIDTH + col - j]; if (2 * sum/8 < obufs[1][row*FOCPLWIDTH + col]) rule2 = 1; } if (rule1 == 1 && rule2 == 1) obuf[row*FOCPLWIDTH + col] = 0; else for (i = 0; i < no_reads; i++) ...``` 0
 Nov 9, 2012, 07:02 PM #2 elppa macrumors 68040     Join Date: Nov 2003 The mixture of indents/braces make it a pain to read. I think people who have never programmed in their lives could simplify this: Code: ```if (obufs[1][row * FOCPLWIDTH + col] / obufs[0][row * FOCPLWIDTH + col] > 1.5) { rule1 = 1; } if (rule1 == 1) {``` to this: Code: ```if (obufs[1][row * FOCPLWIDTH + col] / obufs[0][row * FOCPLWIDTH + col] > 1.5) {``` As for the rest of it, I guess you want: Code: ```if (obufs[1][row * FOCPLWIDTH + col] / obufs[0][row * FOCPLWIDTH + col] > 1.5) { sum = 0; for ( i = -1; i < 2; i++) { for ( j = -1; j < 2; j++) if (!(i == 0 && j == 0)) { sum += obufs[1][(row - i)*FOCPLWIDTH + col - j]; } } } if (2 * sum/8 < obufs[1][row*FOCPLWIDTH + col]) { obuf[row*FOCPLWIDTH + col] = 0; } } else { for (i = 0; i < no_reads; i++) }``` 0
 Nov 9, 2012, 07:12 PM #3 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 The way you have it, if the first condition is true and the second condition is false, the code exits the if statement and never gets to the else. If has to do the else if both conditions are false. 0
Nov 10, 2012, 04:50 AM   #4
elppa
macrumors 68040

Join Date: Nov 2003
Quote:
 Originally Posted by farmerdoug The way you have it, if the first condition is true and the second condition is false, the code exits the if statement and never gets to the else. If has to do the else if both conditions are false.
Good point.

Will
Code:
`obuf[row * FOCPLWIDTH + col]`
ever evaluate to 0 under normal circumstances? Or will it only get set to 0 if rule1 + rule2 are true?

If so, you can do:

Code:
```if (obufs[1][row * FOCPLWIDTH + col] / obufs[0][row * FOCPLWIDTH + col] > 1.5)
{
sum = 0;
for ( i = -1; i < 2; i++)
{
for ( j = -1; j < 2; j++)
if (!(i == 0 && j == 0))
{
sum += obufs[1][(row - i) * FOCPLWIDTH + col - j];
}
}
}

if (2 * sum / 8 < obufs[1][row * FOCPLWIDTH + col])
{
obuf[row * FOCPLWIDTH + col] = 0;
}
}

if(obuf[row * FOCPLWIDTH + col] != 0)
{
for (i = 0; i < no_reads; i++)
}```
Otherwise you can still reduce it to one variable:

Code:
```bothRulesMatch = 0;

if (obufs[1][row * FOCPLWIDTH + col] / obufs[0][row * FOCPLWIDTH + col] > 1.5)
{
sum = 0;
for ( i = -1; i < 2; i++)
{
for ( j = -1; j < 2; j++)
if (!(i == 0 && j == 0))
{
sum += obufs[1][(row - i) * FOCPLWIDTH + col - j];
}
}
}

if (2 * sum / 8 < obufs[1][row * FOCPLWIDTH + col])
{
obuf[row * FOCPLWIDTH + col] = 0;
bothRulesMatch = 1;
}
}

if(bothRulesMatch == 0)
{
for (i = 0; i < no_reads; i++)
}```
0
 Nov 10, 2012, 05:33 AM #5 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 Neat. Thanks a lot. And next time don't be just a smart ass. :-) ---------- obufs is initialized to zero so there is no need to reassign it to zero. Code: ```BothConditionsMeet = 0; if (obufs[1][row*FOCPLWIDTH + col]/obufs[0][row*FOCPLWIDTH + col] > 1.25) { sum = 0; for ( i = -1; i < 2; i++) for ( j = -1; j < 2; j++) if (!(i == 0 && j == 0)) sum += obufs[1][(row - i)*FOCPLWIDTH + col - j]; if (2 * sum/8 < obufs[1][row*FOCPLWIDTH + col]) BothConditionsMeet = 1 } if (BothConditionsMeet == 0) { for (i = 0; i < no_reads; i++)``` 0
Nov 10, 2012, 08:19 AM   #6
elppa
macrumors 68040

Join Date: Nov 2003
Quote:
 Originally Posted by farmerdoug obufs is initialized to zero so there is no need to reassign it to zero.
That is fair enough, the only reason I did the assignment is because you do that in your original code (It was "obuf" not "obufs").

Quote:
 Originally Posted by farmerdoug Neat. Thanks a lot. And next time don't be just a smart ass. :-)
And next time don't write crap code. Indentation and consistency is important for legibility.

I am going to make a couple more comments to try and help - take them on board if you like:

[1] You have eliminated the two variables, but this calculation is still evaluated 3x times, even though it will never change as row, col and FOCPLWIDTH are never modified in the snippet.

Code:
`row*FOCPLWIDTH + col`
I'd be tempted to put this in a variable. Not really for performance, but for readability.

[2] Breaking the code down, the purpose seems to be to determine whether or not to run this for loop at the end.

As we've established, it should only be run if one of the conditions is not met.

So firstly I'd tackle this by writing two methods which are just concerned with just evaluating the conditions:

Code:
```private boolean evaluateConditionOne(int index)
{
return obufs[1][index] / obufs[0][index] > 1.25;
}```
Code:
```private boolean evaluateConditionTwo(int index)
{
return 2 * doSum() / 8 < obufs[1][index];
}```
I am not sure what language your code is in, but it looks C like. The exact syntax for creating a method/function/subroutine may differ, but the principle is the same.

[3] This means you are left with a much simpler if statement:
Code:
`if(!(evaluateConditionOne(index) && evaluateConditionTwo(index)))`
So as you can see, by breaking things down it is simpler, increased readable (less nesting) and as a bonus we have increased reusability.

Programming is about breaking down problems into smaller problems. If something is unwieldy/confusing, then generally it is time to refactor.
1

 MacRumors Forums