Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspection for `Select Case` block that can be simplified to `If Then` block #3565

Open
ThunderFrame opened this issue Nov 25, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@ThunderFrame
Copy link
Member

commented Nov 25, 2017

Code like this:

Sub ComboBox1_Change()
    Application.ScreenUpdating = False
    Select Case Me.Range("code_plant")
        Case 1
            Me.Range("price_zero").Copy _
                Destination:=Me.Range("price_on_view")
    End Select
    Application.ScreenUpdating = True
End Sub

Can have the Select Case simplified to an If Then statement:

Sub ComboBox1_Change()
    Application.ScreenUpdating = False
    If Me.Range("code_plant") = 1 Then
            Me.Range("price_zero").Copy _
                Destination:=Me.Range("price_on_view")
    End If
    Application.ScreenUpdating = True
End Sub

\o/ - Saved one line of code! (and some nasty Select Case indentation)

@bclothier

This comment has been minimized.

Copy link
Contributor

commented Nov 27, 2017

Do note that this isn't equivalent, however....

Select Case True
  Case MyConnection Is Nothing, MyConnection.State = adStateClosed
    'Initialize the ADODB.Connection
End Select

Rephrasing this to an If as shown:

If MyConnection Is Nothing Or MyConnection.State = adStateClosed Then 
  'Initalize ADODB.Connection....
End If

Will result in a runtime error because the Or is not short-circuited as was in the Select Case expression.

With that in mind, I'm not sure if it's always safe to convert expression like Case 5, 8, Is >10, "abc" to an equivalent If without changing the behavior. If the inspection is implemented, it should avoid those cases.

@Hosch250

This comment has been minimized.

Copy link
Member

commented Nov 27, 2017

We'd have to start out with only doing it for cases with only one expression to match on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.