Use prefix of underscore for memoization

Let’s look at code.

class UsageReportCarrier

  attr_reader :health_agency

  def initialize health_agency
    @health_agency = health_agency
  end

  def managers
    @managers ||= health_agency.health_agency_users
  end

end

In the above code method managers is using memoization. However memoization like this makes code less confident since @ has a special meaning in ruby. When we see @managers we start thinkin where else it is being used. Is there an attr_reader for it or not. Is someone directly messing with this instance variable. Overall it gives impression that there might be more to it than just memoization.

In order to make the intent of memoization absolutely clear we prefer to use

def managers
  @_managers ||= health_agency.health_agency_users
end

This makes it absolutely clear that here the intent of using @ is just to do memoization and nothing else.