rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.91k stars 300 forks source link

Operand order, an inspection for #2219

Open ThunderFrame opened 8 years ago

ThunderFrame commented 8 years ago

In comparison conditions, code is more readable when the operands are in a readable order, unlike the following:

    Dim widget As Long
    widget = 5

    If 5 = widget Then
        Beep
    End If

    If 4 + 2 = widget Then
      Beep
    End If

    While 10 > widget
      widget = widget + 1
    Wend

    Select Case True
      Case 15 > widget
         Beep
      Case Else
    End Select

Granted, some might argue that this is a matter of personal preference, and there are some special situations where both operands are suitable for either side of the comparison.

Dim j
Dim i
For i = 1 to 10
  For j = 1 To 10
    'Does i come before j because i is assigned before j, or does j come before i because j is declared before i?
    If i = j Then
      Beep
    End If

    'What happens when there's a calculation on both sides of the comparison?
    If i + 2 = j + 3 Then
      Beep
    End If
  Next j
Next i
retailcoder commented 8 years ago

In other words, a Yoda Condition inspection?

Vogel612 commented 8 years ago

Shouldn't be too hard to implement, I'd say it's nothing we should be interfering with though. The only thing where this could be useful IMO is in some "inconsistent conditional ordering" inspection, which would evaluate all conditionals and flag all that don't follow the majority. That's something I could get behind