programming ifs help.

Discussion in 'Mac Programming' started by farmerdoug, Nov 9, 2012.

  1. macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    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++) ...
    
     
  2. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #2
    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++)
    }
    
     
  3. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    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.
     
  4. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #4
    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++)
    }
    
     
  5. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    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++)
    				
    
     
  6. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #6
    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").

    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.
     
  7. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    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." :)
     
  8. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    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!
     
  9. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #9
    I haven't suggested anywhere to remove the nested for loop. I just omitted the doSum() method for brevity.

    This code does as you describe - evaluateConditionTwo will only be called if evaluateConditionOne returns true. It is correct.

    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.
     
  10. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #10
    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
     
  11. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #11
    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.
     
  12. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #12
    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.
     
  13. macrumors 603

    Joined:
    Aug 9, 2009
    #13
    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?
     
  14. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #14
    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.
     
  15. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #15
    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.
     
  16. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #16
    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
     
  17. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #17
    Bad assumption!

    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.
     
  18. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #18
    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.
     

Share This Page