7 Replies - 1125 Views - Last Post: 18 November 2008 - 07:09 PM

#1 TonicX57   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 13-September 08

Labels

Posted 16 November 2008 - 01:02 PM

Why is everyone against the use of labels in programs? I find them to be very useful, especially in my termination segments that I use in practically every program in practically every language I use. For example, here's a termination lsegment I wrote in Epic using labels:

[[Use this in your program to give the user the option of 
restarting the program at the end, rather than just quitting.]]
[label=beginning]
[[Put all your code here.]]
[say]\n[/say]
[label=terminationLine]
[say]"Press X to quit, or R to restart..."[/say]
[if=read=="X"]
[goto=quit]
[elseif=read=="R"]
[goto=beginning]
[/elseif]
[else]
[goto=error]
[/else]
[/if]
[label=quit]
[exit]
[label=error]
[say]"Invalid input, try again..."[/say]
[goto=terminationLine]



Here's one in C#:

            //Put this at the beginning of the program:
            beginning:
            //
            //Put your code here
            //
            //This is the beginning of the termination process.
            termination:
            //Asks user to either terminate or restart the program.
            Console.WriteLine("Press X to terminate, or R to restart the program...");
            //Declares a string to represent if the user wants to exit or restart.
            string qr = Console.ReadLine();
            //If the user chooses to restart...
            if (qr.ToUpper() == "R")
            {
                //...go back to the beginning...
                goto beginning;
            }
                //...otherwise...
            else if (qr.ToUpper() == "X")
            {
                //...quit, but...
                Console.Read();
            }
            //...if the user presses something else...
            else
            {
                //...give an error message...
                Console.WriteLine("Invalid input, please try again...");
                //...and restart the termination process.
                goto termination;
            }



And I have written some in other languages that I don't have handy right now.

How else would you do something like this without labels? What's so bad about labels? Everyone always says that you shouldn't use labels. I just don't get it.

Thanks,
TonicX57

Is This A Good Question/Topic? 0
  • +

Replies To: Labels

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Labels

Posted 16 November 2008 - 01:52 PM

Coders are against labels (I am as well) because labels cause abnormal break in logic flow. That is, it causes the control of execution of a program to jump around and often time done inappropriately. The use of labels typically demonstrates a problem in program design. Whatever can be done with a label can be accomplished through normal control statements and usually done safer.

What do I mean by safer? I mean that for instance if I was to declare a label inside the middle of a function and then made the goto in another function (preferably in a different scope) I could jump right to the middle of another loop which may cause problems if that function relies on code in the beginning of that function for execution in the loop. It would bypass that, could cause errors or unintended code to run.

Most of the time you see labels being used to mimic loops where if one condition is true it would goto a label defined at the top and reiterate. This can be accomplished by using a typical loop and executing a continue or break statement accordingly.

But like I said, they are dangerous in that it alters the flow of execution and it is very easy to make a program jump all over the place. You will hear the term "spaghetti code" to refer to this. Computers typically don't care but coders and others that read your code will often see labels abused and jumping here and then there, then back to the beginning or jump into the middle of functions etc.

Because of this usual abuse, it has been advised in many publications and programming theorists (like myself) that goto statements and labels be simply avoided. They are no longer needed and their effects can be accomplished through more proper and structured (controlled) means.

Hope that helps explain why. :)
Was This Post Helpful? 0
  • +
  • -

#3 TonicX57   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 13-September 08

Re: Labels

Posted 16 November 2008 - 01:55 PM

Yes, I see why people do not like labels, but then how else would you do something like one of my termination segments?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Labels

Posted 16 November 2008 - 02:09 PM

Your example would be to read in the console info, go into a while loop if there was content, execute the beginning code if they typed "R" and terminate if they typed "X". If they typed "R" and that finished, it would then go ahead and reprompt. One way may be something like this...

//Asks user to either terminate or restart the program.
Console.WriteLine("Press X to terminate, or R to restart the program...");
//Declares a string to represent if the user wants to exit or restart.
string qr = Console.ReadLine();
			
while (qr != null) {
     //If the user chooses to restart...
     if (qr.ToUpper() == "R")
     {
          // Code that would follow your beginning label
     }
     //...otherwise...
     else if (qr.ToUpper() == "X")
     {
          // Termination code
     }
     //...if the user presses something else...
     else
     {
          //...give an error message...
          Console.WriteLine("Invalid input, please try again...");
     }
				
     // Reprompt to see if user wants to restart or terminate.
     Console.WriteLine("Press X to terminate, or R to restart the program...");
     string qr = Console.ReadLine();
}



If a programmer was to run across this they would see a standard priming prompt (asking for input) and then a while loop which makes a decision based on the input. It would then reprompt at the end or have executed termination code. This termination code could be a simple break statement to jump out of the loop, it could be a harsh stop of the entire program, or it could be that this loop is inside a try catch block and so the termination statements simply throw an exception.

:)
Was This Post Helpful? 0
  • +
  • -

#5 TonicX57   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 13-September 08

Re: Labels

Posted 16 November 2008 - 04:27 PM

You have // Code that would follow your beginning label in there. So does that mean that there would need to be at least one label? So wouldn't the whole thing look something like the following?

 //Mark the beginning of the program.
 Beginning: 
 //
 //Your code goes here.
 //
 // Prompt to see if user wants to restart or terminate.  
 Console.WriteLine("Press X to terminate, or R to restart the program...");  
 //Declares a string to represent if the user wants to exit or restart.  
 string qr = Console.ReadLine();  
  
 //Make a loop for as long as something is contained in string qr.             
 while (qr != null) {  
      //If the user chooses to restart...  
      if (qr.ToUpper() == "R")  
      {  
           //...go to the beginning... 
           Goto Beginning; 
      }  
      //...otherwise...  
      else if (qr.ToUpper() == "X")  
      {  
           //...exit the loop and quit, but...
           break;  
      }  
      //...if the user presses something else...  
      else  
      {  
           //...give an error message.  
           Console.WriteLine("Invalid input, please try again...");  
      }  
       
      // Reprompt to see if user wants to restart or terminate.  
      Console.WriteLine("Press X to terminate, or R to restart the program..."); 
      //Give qr a new user input.             
      qr = Console.ReadLine();  
 }  


This post has been edited by TonicX57: 16 November 2008 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 AdaHacker   User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: Labels

Posted 16 November 2008 - 08:10 PM

View PostTonicX57, on 16 Nov, 2008 - 05:27 PM, said:

You have // Code that would follow your beginning label in there. So does that mean that there would need to be at least one label?

No, it means that there would be absolutely no labels. What Martyr2 was trying to say is that you'd take whatever code you had after the "beginning" label in your original example (where you had the "Your code goes here" comment) and move it to that position. The last thing you would do is put a goto there and have it jump back to before the beginning of the loop. Not only is that an abuse of goto that gratuitously mangles the logical flow of the program, but it completely defeats the purpose of using the while loop in the first place.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Labels

Posted 17 November 2008 - 10:18 AM

View PostTonicX57, on 16 Nov, 2008 - 06:27 PM, said:

You have // Code that would follow your beginning label in there. So does that mean that there would need to be at least one label? So wouldn't the whole thing look something like the following?

 //Mark the beginning of the program.
 Beginning: 
 //
 //Your code goes here.
 //
 // Prompt to see if user wants to restart or terminate.  
 Console.WriteLine("Press X to terminate, or R to restart the program...");  
 //Declares a string to represent if the user wants to exit or restart.  
 string qr = Console.ReadLine();  
  
 //Make a loop for as long as something is contained in string qr.             
 while (qr != null) {  
      //If the user chooses to restart...  
      if (qr.ToUpper() == "R")  
      {  
           //...go to the beginning... 
           Goto Beginning; 
      }  
      //...otherwise...  
      else if (qr.ToUpper() == "X")  
      {  
           //...exit the loop and quit, but...
           break;  
      }  
      //...if the user presses something else...  
      else  
      {  
           //...give an error message.  
           Console.WriteLine("Invalid input, please try again...");  
      }  
       
      // Reprompt to see if user wants to restart or terminate.  
      Console.WriteLine("Press X to terminate, or R to restart the program..."); 
      //Give qr a new user input.             
      qr = Console.ReadLine();  
 }  




more like....

string qr = string.Empty;

do
{
    // Your beginning code ---
    // This will get run everytime the program loops

    Console.WriteLine("Press X to terminate, or R to restart the program...");
    qr = Console.ReadLine();

    if (qr == "X")
    {
          //To terminate
          qr = null;
    }
    else
    {
          Console.WriteLine("Invalid input.  Please try again...");
    }
} while(qr != null);

Console.ReadLine();


Was This Post Helpful? 0
  • +
  • -

#8 TonicX57   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 13-September 08

Re: Labels

Posted 18 November 2008 - 07:09 PM

Ah, now I see how it works. Alright. Thanks, everybody. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1