Skip to content

Files

Latest commit

 

History

History
117 lines (96 loc) · 1.47 KB

Style-ConditionalAssignment.md

File metadata and controls

117 lines (96 loc) · 1.47 KB

Pattern: Assignment in conditional

Issue: -

Description

Checks for if and case statements where each branch is used for assignment to the same variable when using the return of the condition can be used instead.

Examples

EnforcedStyle: assign_to_condition

# bad
if foo
  bar = 1
else
  bar = 2
end

case foo
when 'a'
  bar += 1
else
  bar += 2
end

if foo
  some_method
  bar = 1
else
  some_other_method
  bar = 2
end

# good
bar = if foo
        1
      else
        2
      end

bar += case foo
       when 'a'
         1
       else
         2
       end

bar << if foo
         some_method
         1
       else
         some_other_method
         2
       end

EnforcedStyle: assign_inside_condition
# bad
bar = if foo
        1
      else
        2
      end

bar += case foo
       when 'a'
         1
       else
         2
       end

bar << if foo
         some_method
         1
       else
         some_other_method
         2
       end

# good
if foo
  bar = 1
else
  bar = 2
end

case foo
when 'a'
  bar += 1
else
  bar += 2
end

if foo
  some_method
  bar = 1
else
  some_other_method
  bar = 2
end

Default configuration

Attribute Value
EnforcedStyle assign_to_condition
SupportedStyles assign_to_condition, assign_inside_condition
SingleLineConditionsOnly true
IncludeTernaryExpressions true

Further Reading