Spaghetti code or not?

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Is writing spaghetti code always bad?

Of the two approaches below, which is preferable, or should there be a better third approach?

Approach 1:

Rich (BB code):
If Condition1 Then

    Do something1

Else

    Do something2

End If

If Statement1 Then Do something1 Else Do something2 End If

Approach2:

Rich (BB code):
If Condition1 Then

   GoTo A

Else

   GoTo B

End If

If Statement1 Then

   GoTo A

Else

   GoTo B

End If

A:

    Do something1

B:

    Do something2

Thanks


 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
IMHO approach one is better, 2 many Goto's make the code code difficult to follow and debug.
 
Upvote 0
IMHO approach one is better, 2 many Goto's make the code code difficult to follow and debug.

Thanks.

I think there are pros and cons to both, hence can't decide.

Approach1

Pro: Easier to read
Con: Involves writing more of the same code

Approach2

Pro: Ability to write the relevant code only once and is re-used
Con: You aleady mentioned it!
 
Upvote 0
Wouldn't fancy having 20 Goto's :) also I would look at using Case statements.
 
Upvote 0
My opinion: Don’tuse if’s if you can use a “Select Case”

1. They are less mind boggling- lower spaghetti factor
2. Easier to maintain
3. I don’t know if it is faster or slower without testing

Here is some code as an example.
How many if statements would you use to do the following?

Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Option Explicit[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Sub tester()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    example 10[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Sub example(iValue As Integer)[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    Select CaseiValue[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]        Case Is>= 15, 10               'condition 1[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]            MsgBox"tada!"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]        Case Else[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]            MsgBox"not tada"           'not condition 1[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    End Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0
If "Something1" is only a line or two, then there's not much duplication of code to worry about. If there's more than a line or two, then you convert it into a subroutine:

Code:
If Condition1 Then
   Call Routine1
Else
   Call Routine2
End If

Passing parameters to the routines makes this idea even more flexible. Writing neat, maintainable code takes some effort and some practice, but is well worth it. I'm not a stickler who never uses GoTo, but they should be quite rare. Some languages don't even have that verb. In VBA there are situations that require it (On Error GoTo Somewhere). I'm a professional programmer with decades of experience, and I've seen a lot of different styles, and I can tell you that spending a little extra time up front designing a structured program is more than worth it when it comes to debugging and maintaining the program.

You should familiarize yourself with all the structured elements of the language you're using. In VBA, things like Select Case, Do, While, Loop, Call, functions, Iif, etc.
 
Last edited:
Upvote 0
I would also recommend against using GoTo when possible.
Depending on what you are doing inside the Ifs you could do assign a value to a variable & keep the code at the end of the ifs like
Code:
   If condition1 Then
      Sht = "Fluff"
      RngAddr = "10:20"
   ElseIf condition2 Then
      Sht = "Tiredofit"
      RngAddr = "30:35"
   End If
   Sheets(Sht).Range(RngAddr).EntireRow.Hidden = True
 
Upvote 0
Thanks for all the suggestions.

The problem is the requirements have not been completely defined at the start, hence Iconstantly find myself having to amend the code, so adding GoTo lines seems an easy solution.


On a similar note, I understand worksheets and userforms are a sort of class module and all code residing there should be self contained.

But suppose in Sheet1 and SHeet2 you have this code:

Code:
If Me.Range("A1").Value = 1 Then

    Range("B1").Value = 10

Else

    Range("B1").Value = 20

End If


Should the code be written in a standard module and the respective worksheets refer to it, ie:

Code:
If Me.Range("A1").Value = 1 Then

    Call Module1.ChangeVal(Me, 1)

Else

    Call Module1.ChangeVal(Me, 2)

End If

and Module1 contains this:

Code:
Sub ChangeVal(wks As Worksheet, v As Long)

    Select Case v

        Case 1

            wks.Range("B1").Value = 10
    
        Case 2

            wks.Range("B1").Value = 20
    
    End Select

End Sub

or is it better to repeat the code within each sheet because it's meant to contain "all its own code"?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top