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
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections

Comments

@ThunderFrame
Copy link
Member

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)

@ThunderFrame ThunderFrame added enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections labels Nov 25, 2017
@bclothier
Copy link
Contributor

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
Copy link
Member

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
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections
Projects
None yet
Development

No branches or pull requests

3 participants