I heard GOTO was bad, how about in this function?

ertaboy356b

Old School Gamer
Reaction score
86
Ok, so I was working on a program in C#. During the beta testing (which is composed of at least 80 participants), loads of problems arise. One problem is that sometimes the socket server was not able to send back the data to the client and because of this, I developed this function for sending out serialized network objects via sockets.

Code:
        private static void SendObject(UserData u,string packetType, object message)
        {
            var failCount = 0;
            RESEND:
            try
            {
                // Send Object to the User
                u.Connection.SendObject(packetType, message);
            }
            catch (Exception ex)
            {
                failCount++;
                Console.WriteLine(ex.Message + " Resending....");
               if (failCount < 10) {
                           goto RESEND;
                }
            }
        }

This code makes the socket sending mechanism persistent up to 10 times. Is it okay to use 'goto' or is their another way to code this function?
 

monoVertex

I'm back!
Reaction score
460
I have no experience in C#, but I think this should be alright:

Code:
private static void SendObject(UserData u, string packetType, object message) {
    for (int i = 0; i < 10; i++) {
        try {
            // Send Object to the User
            u.Connection.SendObject(packetType, message);
            return;
        } catch (Exception ex) {
            Console.WriteLine(ex.Message + " Resending....");
        }
    }
}

GOTO is bad and I've never experienced a situation where I couldn't write my code without GOTO. Just forget it exists. Unless you're coding in ASM you have no reason to use it.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
GoTo is equivalent to while / for loops in power.
But many say that GoTo is not as readable and maintainable then the latter.
 

monoVertex

I'm back!
Reaction score
460
Yes, it is equivalent, but it's bad exactly for the reasons you mentioned. It's way more readable to have a pair of curly braces holding your loop rather than being slapped with a GOTO at some point.

It also quickly becomes a nightmare when you start nesting them, which is a big temptation when using GOTOs.

Besides, with a GOTO you have to take care of incrementing / stopping manually. I've come to the conclusion that a FOR loop can almost always replace other looping structures and you've got the mechanisms for stopping / iterating right in the header of the structure.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
a FOR loop can almost always replace other looping structures
Not entirely correct. The correct answer would be:
a FOR loop can always replace other looping structures
Thats why I said they are equivalent.

But its personal preference. There are no factual arguments for or against using GoTo. Its just the general consensus that GoTo is not as easy to read and maintain.
 

monoVertex

I'm back!
Reaction score
460
Yes, I agree that it can always replace, I was actually thinking of the do { } while structure, which tests at the end. I agree you can do that with a for, but it's rather awkward and when you actually need to test at the end, it might be better to use the do structure.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Code:
do{
    actions
} while (condition);

===

while(true){
    actions
    if( !condition ) break;
}

:D
 

Varine

And as the moon rises, we shall prepare for war
Reaction score
805
I use GoTo as it is really easy, but I'm mediocre and mostly self-taught and no one else is going to read it anyway.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top