MacRumors Forums programming ifs help.
 User Name Remember Me? Password

 Thread Tools Search this Thread Display Modes
 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
 Nov 10, 2012, 09:17 AM #7 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 Yes, I understand about why you assigned obufs to zero. I could replace the for loops with one loop for ( i = 0; i < FOCPLSIZE; i++) but I happen to prefer the double loop. Also some later loops require operations that are row and/or column specific. "As we've established, it should only be run if one of the conditions is not met." No both conditions and this is important. You don't want to bother with the second condition if the first isn't meet. So this is unnecessary: if(!(evaluateConditionOne(index) && evaluateConditionTwo(index))) I appreciate your help but I don't need lectures. The format may be messy but the code isn't crap. Try this: "I'm glad to help but would appreciate it if you would be more careful with indentations and in general neaten things up." :-) 0
 Nov 10, 2012, 11:05 AM #8 lloyddean macrumors 6502a   Join Date: May 2009 Location: Des Moines, WA Why bother helping at all we've gently chided your code formatting and style in order to help you to help yourself! And we've nearly laughed out-loud at your code formatting and style in order shame you into helping yourself! You're single mindidly (spelling?) determined to go after the promise of short term results over readable code which invariably leads you to loooong coding and debug sessions. You just don't seem to want learn - your method wastes your time and others! 3
Nov 10, 2012, 11:14 AM   #9
elppa
macrumors 68040

Join Date: Nov 2003
Quote:
 Originally Posted by farmerdoug I could replace the for loops with one loop for ( i = 0; i < FOCPLSIZE; i++) but I happen to prefer the double loop. Also some later loops require operations that are row and/or column specific.
I haven't suggested anywhere to remove the nested for loop. I just omitted the doSum() method for brevity.

Quote:
 Originally Posted by farmerdoug You don't want to bother with the second condition if the first isn't meet. So this is unnecessary: if(!(evaluateConditionOne(index) && evaluateConditionTwo(index)))
This code does as you describe - evaluateConditionTwo will only be called if evaluateConditionOne returns true. It is correct.

Quote:
 Originally Posted by farmerdoug I appreciate your help but I don't need lectures. The format may be messy but the code isn't crap. Try this: "I'm glad to help but would appreciate it if you would be more careful with indentations and in general neaten things up." :-)
It wasn't a lecture, it was help. I said "take them on board if you like:".

But now you've been rude – I'll be honest. Your code is poor, you don't know as much about programming as you think you do and that's the last time I help you.
3
 Nov 10, 2012, 11:31 AM #10 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 Funny, I thought you were being rude. This is what happens without face to face communication. Sorry, I no time did I intend to be rude. You don't know how much programming I think I know, so you really can't comment on it. But not being a programmer per se, it doesn't matter to me. I get the job done. And let me point out that twice you missed what I was trying to do. The first time you can attribute to my programming style but the second time? Who's a bad programmer? You want a really programming challenge? Take 4 megapixel images with poor signal to noise ratios and decide what's real and what's not. Let me know if you want to help. You don't want to help that's your choice. I have a nice day. ---------- http://adsabs.harvard.edu/abs/2011PASP..123..746Z 0
Nov 10, 2012, 12:22 PM   #11
robvas
macrumors 68000

Join Date: Mar 2009
Location: USA
Quote:
 Originally Posted by farmerdoug You don't know how much programming I think I know, so you really can't comment on it. But not being a programmer per se, it doesn't matter to me. I get the job done. And let me point out that twice you missed what I was trying to do. The first time you can attribute to my programming style but the second time? Who's a bad programmer?
My 2 cents that you probably don't want to hear: Every time you post a thread I click on it to see what cluster-**** of code you are posting and asking help on. Almost every time it looks like a first-semester programming student wrote it, not knowing how the language works.
0
 Nov 10, 2012, 12:33 PM #12 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 I must know something. See the link in my previous post. If you don't want to help, that's your choice. You guys seem more interested in neatness, than in content; how it looks rather than what it does. You're all beginning to sound a little anal retentive to me. 0
Nov 10, 2012, 01:34 PM   #13
chown33
macrumors 603

Join Date: Aug 2009
Quote:
 Originally Posted by farmerdoug I must know something. See the link in my previous post. If you don't want to help, that's your choice. You guys seem more interested in neatness, than in content; how it looks rather than what it does. You're all beginning to sound a little anal retentive to me.
I wonder why you asked the question at all.

Did your posted code not work correctly? Were you trying to make it faster? Smaller? Fewer variables? Exactly what is your goal? And why?

If it doesn't work, then you should say "It doesn't work", then go on to describe exactly what happens, and what you expected to happen.

If it's not fast enough, then you should say "It's not fast enough", then go on to explain how you've profiled it to determine that it's both (a) not fast enough, and (b) the actual bottle-neck.

If it already works, and it's already fast enough, why mess with it? That's not a purely rhetorical question. Rule 1 is "If it ain't broke, don't fix it". So in what way was the posted code "broke"?

Were you trying to make it clearer? Well, so were all the commenters who critiqued the clarity, and offered suggestions.

Who were you trying to make it clearer for? Yourself? Others? Did you say "I'm trying to make it clearer"? Did you identify who this was for?

You didn't even identify the language it was written in, so one commenter inferred it was Java, and posted code in Java.

Go reread your original post, and knowing nothing else about what you're trying to do, how is anyone here supposed to do anything but infer your goals by making assumptions and reading between the lines? If you don't tell us what the goal is, what other choice is there?
2
 Nov 10, 2012, 02:19 PM #14 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 It seemed perfectly clear to me but then its hard to put aside what you already know. (Mostly it was an academic exercise since it did run as written) But ellppa figured it out and provided help. Furthermore, the language is immaterial, I can translate from an object to a function or procedure. I'm sorry but I do not fit into your mold of what a programmer should be like. Maybe that's because I am not a programmer. I do program, obviously but for the most part I was not hired nor am I paid to program. If my skills as a programmer or my communication skills are lacking, so be it. I've been like this for 45 years; all attempts at reform have failed. Either you want to help or not. 0
Nov 10, 2012, 03:28 PM   #15
lloyddean
macrumors 6502a

Join Date: May 2009
Location: Des Moines, WA
Quote:
 Originally Posted by farmerdoug I've been like this for 45 years; all attempts at reform have failed. Either you want to help or not.
Doug,

Maybe it's a bad assumption on my part but ...

Wouldn't you rather have more time to get through your projects and reach your goals to get on the next one?

Learn something of the craft of good programming. Not as a goal in and of itself but so you can repurpose the time you currently find being consumed by debugging on something more useful to you!

Your time in this world is dwindling fast and your project list is likely longer than your current method of "programming" allows.
0
 Nov 10, 2012, 03:50 PM #16 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 Hey Lloyd! How are you doing? Your assumption is not a bad one at all. I would like to be neater and more organized both in programming and in other facets of my professional life. And I have tried many times to improve my work habits; but each time I meet with failure. If my bad habits were fatal to my career perhaps I would change but they're not so I muddle through. Fortunately, I can be a success and a disorganized slob at the same time. I bet you guys all have neat desks. I should send a picture of mine. LOL. Best Doug 0
Nov 10, 2012, 09:28 PM   #17
lloyddean
macrumors 6502a

Join Date: May 2009
Location: Des Moines, WA
Quote:
 Originally Posted by farmerdoug I bet you guys all have neat desks.

My computers desktop is littered with many an active project.

My physical desk has 3 embedded projects sitting out, a multi-meter, 2 24" monitors, a multi-function fax/scanner/printer, 4 external hard drives, a Apple Keyboard with Numeric Keypad, Magic Trackpad, single-button Apple Mouse, a good pair of Sony headphones and a near rats nest of cables.

The rest of the house on the other hand is neat and orderly.
0
 Nov 11, 2012, 04:31 AM #18 farmerdoug Thread Starter macrumors 6502a   Join Date: Sep 2008 Glad to hear it. A clean desk is a sign of a sick mind. But mine has stacks of falling over papers; several dirty coffee cups; empty sugar packets and more. The more unfortunate thing is that I get religion; buy new notebooks; and sometimes don't even start to use them; or start and never finish. Occasionally, when writing code, I clean up the brackets and make it structured but the bottom line is that I don't do it often enough and haven't been able to improve my work habits. As a freshman in college, written across my first physics test were the words" This is unreadable, A". Wish he had failed me. 0

 MacRumors Forums

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Similar Threads thread Thread Starter Forum Replies Last Post moonman239 Mac Programming 13 Feb 17, 2014 01:37 PM swimanole Mac Programming 10 Nov 6, 2012 11:55 AM Meitou Mac Programming 2 Sep 21, 2012 01:36 PM jmfel1926 Mac Programming 4 Sep 14, 2012 07:21 PM