Skip to content

Files

Latest commit

 

History

History
40 lines (30 loc) · 790 Bytes

Naming-MemoizedInstanceVariableName.md

File metadata and controls

40 lines (30 loc) · 790 Bytes

Pattern: Memoized variable not matching method name

Issue: -

Description

This rule checks for memoized methods whose instance variable name does not match the method name.

Examples

# bad
# Method foo is memoized using an instance variable that is
# not `@foo`. This can cause confusion and bugs.
def foo
  @something ||= calculate_expensive_thing
end

# good
def foo
  @foo ||= calculate_expensive_thing
end

# good
def foo
  @foo ||= begin
    calculate_expensive_thing
  end
end

# good
def foo
  helper_variable = something_we_need_to_calculate_foo
  @foo ||= calculate_expensive_thing(helper_variable)
end

Further Reading