Skip to content

Inspection for verbose boolean assignment within If Else condition block expression #3363

Open
@ThunderFrame

Description

@ThunderFrame

It's common to see boolean assignments like this on SO:

  Dim dayOfMonthIsEven As Boolean
  If Day(Now()) Mod 2 = 0 Then
    dayOfMonthIsEven = True
  Else
    dayOfMonthIsEven = False
  End If

But that's 5 lines of code to resolve the boolean condition and assign a boolean value, that can be achieved more efficiently, concisely and clearly, in a single line assignment:

  Dim dayOfMonthIsEven As Boolean
  dayOfMonthIsEven = Day(Now()) Mod 2 = 0

RD should be able to identify these long-winded assignments, and rewrite them more efficiently as a boolean expression.

We'll also need to identify the equivalent single line If statement:

  If Day(Now()) Mod 2 = 0 Then dayOfMonthIsEven = True Else dayOfMonthIsEven = False

And also need to identify the equivalent IIf statement:

  dayOfMonthIsEven = IIf(Day(Now()) Mod 2 = 0, True, False)

RD would also need to account for the reversed boolean logic that might require the expression to be flipped with a Not operator (or some other expression adjustment):

  Dim dayOfMonthIsEven As Boolean
  If Day(Now()) Mod 2 = 1 Then
    dayOfMonthIsEven = False
  Else
    dayOfMonthIsEven = True
  End If

Could become:

dayOfMonthIsEven = Not Day(Now()) Mod 2 = 1 

Or better still (although this would be much harder):

dayOfMonthIsEven = Day(Now()) Mod 2 = 0 

Metadata

Metadata

Assignees

No one assigned

    Labels

    code-path-analysisInvolves simulating execution paths / interpreting the user code ..to an extent.enhancementFeature requests, or enhancements to existing features. Ideas. Anything within the project's scope.feature-inspections

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions