They should have made some kind of loop construct for this



  • Too bad the only C# loop constructs introduced in C# were the for-loop and the goto statement. 

     

            public bool reAquireHardware()
            {
                _settings._experiment.unlockRequiredHardware();
                string error = string.Empty;
            tryAgain:
                if (!_settings._experiment.lockRequiredHardware(ref error))
                {
                    if (MessageBox.Show(this, error, "Hardware Error(s)", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                        goto tryAgain;
                    else
                        return false;
                }
                return true;
            }

     

    Losing my sanity as I maintain this dirty-gas-station-bathroom of a code base...



  • While using goto is a dangerous WTF since it can summon velociraptors, I don't see it as a big deal in this code. Just substitute a while loop there and you're done.



  • That's exactly the point. How could someone writing this code not notice that they just made the ugliest variant of a while loop?



  • [quote user="Renan "C#" Sousa"]Just substitute a while loop there and you're done.[/quote]

    And, if possible, arrange a blanket party for the guy who wrote that.




    edit: for the guy who wrote the code, not the comment I quoted. Sorry Renan. :D


  • BINNED

    The Meta WTF here is that your post count is 1 although I see 2 posts right in front of me.

     

    EDIT: Damn the forum, it fixed it right after I wrote that.



  • Lurk more and you might find worse. For example, recursive variations of the while loop, or variations where you have two applications call each other in turns.



  • [quote user="Renan "C#" Sousa"]

    Lurk more and you might find worse

    [/quote] 

     

    I don't doubt that for a second.  This is probably somewhere around just the third circle of hell, or so.  As bad as it is, it could be a lot worse.



  • @MarkR said:


    public bool reAquireHardware()
    {
    _settings._experiment.unlockRequiredHardware();
    string error = string.Empty;
    tryAgain:
    if (!_settings._experiment.lockRequiredHardware(ref error))
    {
    if (MessageBox.Show(this, error, "Hardware Error(s)", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
    goto tryAgain;
    else
    return false;
    }
    return true;
    }

    @MarkR said:
    That's exactly the point. How could someone writing this code not notice that they just made the ugliest variant of a while loop?

            public bool reAquireHardware()
            {
                _settings._experiment.unlockRequiredHardware();
                string error = string.Empty;
                while (!_settings._experiment.lockRequiredHardware(ref error))
                {
                    if (MessageBox.Show(this, error, "Hardware Error(s)", MessageBoxButtons.RetryCancel) == DialogResult.Cancel)
                        return false;
                }
                return true;
            }
    

    I don't think it's actually any more readable this way. The goto was making the action of retrying more explicit here which may be desired given it's handling explicit user action.

    IMHO the most readable implementation would be using perl's redo statement and the goto is just direct translation of that to language that does not have redo.



  • [quote user="Renan "C#" Sousa"]

    While using goto is a dangerous WTF since it can summon velociraptors, I don't see it as a big deal in this code. Just substitute a while loop there and you're done.

    [/quote]Even worse you could summon SpectateSwamp and be jammed by his noodley appendage!



  • Tali, have you been getting pregnant again? WTF are we going to call this one.



  • @MarkR said:

    Losing my sanity as I maintain this dirty-gas-station-bathroom of a code base...


    DUDE!!! That's my new IM "away" message! FTF'ingW!



  • @SuperJames74 said:

    @MarkR said:

    Losing my sanity as I maintain this dirty-gas-station-bathroom of a code base...


    DUDE!!! That's my new IM "away" message! FTF'ingW!



  • @blakeyrat said:

    Picture of a really dirty toilet...
    Ouch! Dear god almighty! I only hope I don't have to be near this place anytime soon.



  • That toilet looks more like burned down. I think they just tried to disinfect it with fire (doesn't seem to help, though).



  • @SEMI-HYBRID code said:

    That toilet looks more like burned down
     

    That shit in the left back corner is cobwebs.



  • @dhromed said:

    @SEMI-HYBRID code said:

    That toilet looks more like burned down
     

    That shit in the left back corner is cobwebs.

    Maybe the spiders arrived after the fire? 'Cuz I know I sure as hell wouldn't want to use a loo after it had to be sterilized by burning.


  • Garbage Person

    @MarkR said:

    Too bad the only C# loop constructs introduced in C# were the for-loop and the goto statement. 
     

    C# has goto?

    .... *clicky* NOOOOOOOOOOOOOOOO!

    [img]http://pic.phyrefile.com/e/ey/eyemwing/2010/08/02/noooooo.png[/img]

     

     

     

     

    <font size="1">(And by nature of that powerful remark against the existence of a language feature, this thread is now about people talking about the actual merits of goto)</font>




  • void GotoExample()

    {

      while( outerLoop )

      {

        while( innerLoop )

        {

          if( conditionRequiringAbortOfBothLoops )

          {

            goto validUseOfGoto;

          }



          DoOtherStuff();

        }

      }



    validUseOfGoto:

      ThisIsTheOnlyValidUseOfGotoInCTypeLanguages();

      IfYoureUsingGotoForAnythingElseYouAreDoingItWrong();

    }

    Even JavaScript includes a Goto-alike for the express purpose of exiting a loop within a loop. They don't call it that, of course (because, gasp, Goto is so eviiil!), but it's got one.

    Or from the more Microsoft-esque pragmatic angle, they need the bytecode to support Goto for VB anyway, so they might as well shove it in C# as well, right?



  • From some dungeon generation code I wrote:

    
    
    		for(int x = 0; x < w; ++x)
    		{
    			for(int y = 0; y < h; ++y)
    			{
    				for(x2 = 0; x2 < w2; ++x2)
    				{
    					x3 = x + x2;
    					curX = squares[x3];
    					for(y2 = 0; y2 < h2; ++y2)
    					{
    						switch(curX[y+y2].centerState)
    						{
    							case CentralState::CentralOpen:
    								c += 3;
    								break;
    							case CentralState::CentralRoom:
    								c = -1;
    								y2 = h2;
    								x2 = w2;
    								break;
    						}
    					}
    				}
    
    				//Snipped some code here...
    			}
    		}
    

    I avoid gotos whenever possible (I haven't used one outside a switch/case block yet...)



  • @MarkR said:

    Too bad the only C# loop constructs introduced in C# were the for-loop and the goto statement.
     

    Even if you're working within some ridiculous coding standard that disallows while-loops for some reason, isn't while(condition) equivalent to for(;condition;) ?



  • @scgtrp said:

    @MarkR said:

    Too bad the only C# loop constructs introduced in C# were the for-loop and the goto statement.
     

    Even if you're working within some ridiculous coding standard that disallows while-loops for some reason, isn't while(condition) equivalent to for(;condition;) ?

     

    Yes.

    A lot of beginner C# and Java programmers are taught how to create a for loop, but not that all three parts of the statement are optional.


Log in to reply