Skip to content
New issue

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

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

Already on GitHub? # to your account

Code Inspection for combining multiple For...Next variables on the same line #6249

Open
IvenBach opened this issue Feb 8, 2025 · 0 comments
Labels
difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky
Milestone

Comments

@IvenBach
Copy link
Member

IvenBach commented Feb 8, 2025

What
Combining multiple Next variables as in Next k, j, i is legally valid code, only if each counter variable is listed as shown. Removing the counter variable without each closing Next produces invalid code.

Why
Combining variables into a single Next messes with default indenting. It also can be easily overlooked leading to features bugs. We all strive for clear code.

Example

Public Sub JustBecauseYouCanDoItDoesntMeanYouShould()
    Dim i As Long
    Dim j As Long
    Dim k As Long
    For i = 0 To 9
        For j = 0 To 9
            For k = 0 To 1
                Debug.Print Space(i + j + k) & "But why?"
            Next k, j, i
End Sub

QuickFixes
Insert an ending Next, preferably including the counter variable, to show the ending of each For...Next statement.
Should Rubberduck offer one or more quickfix(es) for this inspection? Describe them here (note: all inspections allow for IgnoreOnceQuickFix, unless explicitly specified):

  1. QuickFix Name - SeparateEachCounterVariableFromACombinedForNextStatementQuickFix

    Example code, after quickfix is applied:

    Public Sub JustBecauseYouCanDoItDoesntMeanYouShould()
        Dim i As Long
        Dim j As Long
        Dim k As Long
        For i = 0 To 9
            For j = 0 To 9
                For k = 0 To 1
                    Debug.Print Space(i + j + k) & "But why?"
                Next k
            Next j
        Next i
    End Sub

Resources
Each inspection needs a number of resource strings - please provide a suggestion here:

  • InspectionNames: MultipleCounterVariablesOnSingleNextEndingStatement
  • InspectionInfo: Combining multiple Next statements visually breaks indenting. It can also lead to overlooking a variables inclusion leading to a subtle bug.
  • InspectionResults: The variables {0} {1} {2} should have their own ending Next statement.
@IvenBach IvenBach added enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky labels Feb 8, 2025
@retailcoder retailcoder added this to the 2.x Cycle milestone Feb 8, 2025
@retailcoder retailcoder added the difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. label Feb 8, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky
Projects
None yet
Development

No branches or pull requests

2 participants