14

So, the other day, my teacher told me not to use goto in C#, with only the reason that "it is bad".

Okay teacher, I'm going to continue using it until you give me a valid reason. For example for making one loop instead of two. .-.

Comments
  • 8
    Good way to teach, teacher!

    I've been taught that it should be avoided as much as possible, because it disturbs the flow of the program, decreases readability and increases the chance of introduces strange bugs.

    And to be honest: I haven't seen any c# code where it would be necessary/useful.
  • 1
    All these anti-goto evangelists are responsible for all the boolean vars with if section you could have avoided with a simple GOTO! They all just repeat a phrase someone told them like "no fart'n burp in public" without thinking about it! It is possible to abuze goto, but they are to dumb to explain.
  • 1
    Also... as a quick example based on your "making one loop instead of two":
    - Using goto requires conditions to determine if you need to go back to the loop. If you don't cover all the angles, then you've basically got an infinite loop. Covering all angles inside a large application can be quite a big task.
    - If an exception is triggered inside your loop, which iteration of the loop triggered the exception? The first time or the goto-one?
  • 0
    @Prutser You don't neccessarily need conditions for that, it depends on the loops though. And the debugging is fairly easy, if an exception is thrown, because of Checkpoints.
  • 0
    @Prutser Disturbs the flow? Not quite: Visual Studio autonatically indents them differently.
  • 0
    @alexanderholman I only use goto if it shortens my code by a few lines, and I also indent it. So far, there have been no problems in readability. Of course, you shouldn't overuse/abuse goto.
  • 1
    @filthyranter it sure depends on the situation if you need conditions.
    I assumed the next situation:
    -Loop
    -Some code
    -Goto Loop

    In that case you do need conditions.

    And I didn't mean indentation when I talked about the flow of the application. I meant the path the code follows during execution.

    Anyway... personally, in your given example, I would put the loop in a method and just call the method when the loop is needed
  • 5
    The one thing goto is good for, is for allowing you to go on a nice little adventure through your code. Reminds me of the text books at school... go to page 50, go to page 185, etc... (Maybe eventually leading back to page 50) 😱

    In my whole 10 years developing I have never been in a situation where I thought "goto" was the best solution 😬
  • 2
    If your teacher can't give you proper reasoning for anything then why don't you google and see what experienced devs say. That way you should be better prepared for the outside world.

    Sidenote: I've never saw a goto statement in production code, nor have I ever thought to use it. Most cases I'd imagine you're breaking SOLID in some way or another.
  • 2
    Not only goto should be avoided but loops too. In C# it's shorter and simpler to use IEnumerable extensions or linq. Much cleaner, shorter and more readable also it allows parallelism if you'd like. Loops are "low level" constructs that may help in optimizing some complex iteration, but even then there's probably a better way of doing that.
  • 0
    @StefanH I was about to say something similar. TL;DR, I think goto is an easy fix for bad design.
    I have used goto once in an FSM, where after implementing almost all of the state machine I realized that I needed to double the amount of states with nearly identical states. The proper way to fix it would have been to start over and find a better representation, but a simple goto saved my ass and let me continue on with the existing design without duplicate states. It was no longer an FSM and the logic behind everything became a lot more complex, but it saved me time.
  • 4
    I didn't even know goto existed in c#
  • 1
    @Prutser there's actually one, pretty rare, situation in C# where a goto statement is the recommended course of action, which is falling through switch cases.
  • 2
    I hesitate to flag this post as "offensive"...
  • 2
    If you meet this case, you have a structure problem @johnny-cache
  • 1
    I felt exactly the same for the first time, but functions are better and easier to follow than GOTO
  • 2
    @Faraaz I'm assembler and basic goto is fine
  • 4
    Goto gets a bad rap. It solved one of my biggest computer science problems: others messing with my code
  • 0
    Well, at least some of you guys had reasons behind why goto isn't too good for you, but for me, it's very useful.
  • 5
    @filthyranter can you show an example where? Maybe I can show you a better approach or maybe you'll enlighten me.
  • 1
  • 5
    @filthyranter

    It is okay to be wrong, you will learn soon, that GOTO is a construct inherited by many languages by older ones who did not have better systems in place to cope with such situations. Today, GOTO is NEVER a good option. Perhaps you feel emotionally negative towards this teacher thus attempt to rebel their advice - do not let it be so. See the truth fore it enlightens you and without it you will not grow.
  • 1
    @rmahey No, my teacher wasn't able to provide a valid reason for that.
  • 1
    @solocoder @siljamicke I'll show you as soon as I encounter it again.
Add Comment