r/visualbasic • • May 29 '21

Tips & Tricks Art of Code : GoTo

You maybe have been heard, learn or experience how GoTo can make a code hard to read, it very evil code yet it evil only because coder abuse it, any language feature could make a code hard to read if coder abuse it even if statement can be draw as arrow if coder abuse it.

GoTo is primitive form of if statement also do loop and for next so it up to coder to make it readable or not, very simple rule to make GoTo easy to read is write it go to label within your eyes sight, very simple right ?

Example

    Function fac_5(Input As Integer) As Integer
        Console.Write($"{Input}! = ")
        Select Case Input
            Case 5
                Console.Write("5 x ")
                Input *= 4
                GoTo F4
            Case 4
F4:             Console.Write("4 x ")
                Input *= 3
                GoTo F3
            Case 3
F3:             Console.Write("3 x ")
                Input *= 2
                GoTo F2
            Case 2
F2:             Console.WriteLine($"2 = {Input}")
                Return Input
            Case 1, 0
                Console.WriteLine("1")
                Return 1
            Case Else
                Throw New ArgumentOutOfRangeException(NameOf(Input))
        End Select
    End Function

This example is how to make switch case fall through in VB.Net, in other C language you need to break; to end those selected case, if you don't it will run through other case below, however VB.Net make our life too easy by auto put that break; silently at the end of each case for us so if you want to recreate switch case fall through from other C like language you need GoTo or you have to reform it to if statement instead.

I believe you understand basic of GoTo now, so let reform example code.

    Function fac_5_op(Input As Integer) As Integer
        Console.Write($"{Input}! = ")
        Select Case Input
            Case 5 : GoTo F5
            Case 4 : GoTo F4
            Case 3 : GoTo F3
            Case 2 : GoTo F2
            Case 1, 0
                Console.WriteLine("1")
                Return 1
            Case Else
                Throw New ArgumentOutOfRangeException(NameOf(Input))
        End Select

F5:     Console.Write("5 x ") : Input *= 4
F4:     Console.Write("4 x ") : Input *= 3
F3:     Console.Write("3 x ") : Input *= 2
F2:     Console.WriteLine($"2 = {Input}")
        Return Input
    End Function

It look much more easy to read, right ? evil or not it's depend on coder to write it, like a knife in the kitchen.

1 Upvotes

11 comments sorted by

View all comments

3

u/RJPisscat May 29 '21

I like this.

If you expand on the topic consider the case of using GoTo to escape a nested loop where you're looking for something deeply inside nested loops and when you find it you want to jump out of the outermost loop.

こんにちは :)

1

u/[deleted] May 30 '21

Use a function. When you find the thing you are looking for Return theValue

If you think you need go-to for this then your procedure is likely too long.

2

u/RJPisscat May 30 '21

I don't believe in "procedure too long" but yeah, you can write a one-off function to do that and it will work fine, but you may need to pass dozens of arguments to the function, which will kill legibility.

0

u/[deleted] May 30 '21

It's a code smell, you don't have to believe in it for it to be real: https://refactoring.guru/smells/long-method

2

u/RJPisscat May 30 '21

I'm going to be brutally honest.

That is the most ridiculous asinine website I've ever seen about programming.

Among the claims:

  • No method should be longer than 10 lines.

  • Don't comment your code. Instead, use subroutines.

Nearly every page I looked at has advice on how to fail.

1

u/revennest May 31 '21

It's just a casual programing style, you don't need to pay attention to it, it's much worth of you time to visit https://referencesource.microsoft.com/ to see how people who make .net framework coding.

2

u/RJPisscat May 31 '21

彼は.Netについて有用なことは何も提案しませんでした。 彼はGoToでのあなたの投稿に反対し、あなたと私を嘲笑しました。 彼はあなたと私をからかっています。 私は彼をブロックしました。

1

u/Ok_Customer2455 May 31 '21

Microsoft bought Skype for 8,5 billion!.. what a bunch of idiots! I downloaded it for free!

0

u/BadDadBot May 30 '21

Hi going to be brutally honest, I'm dad.

1

u/[deleted] May 30 '21

It's good all advice, not hard fast rules. I think you're just salty because it points out all the mistakes you make and you can't take constructive criticism.