Sha256: 6b4745421c22ef0494f6352aa6a3be019c245d872a7ca48aa860b763f63d63a3

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

# Instance Variable Assumption

## Introduction

Classes should not assume that instance variables are set or present outside of the current class definition.

Good:

```Ruby
class Foo
  def initialize
    @bar = :foo
  end

  def foo?
    @bar == :foo
  end
end
```

Good as well:

```Ruby
class Foo
  def foo?
    bar == :foo
  end

  def bar
    @bar ||= :foo
  end
end
```

Bad:

```Ruby
class Foo
  def go_foo!
    @bar = :foo
  end

  def foo?
    @bar == :foo
  end
end
```

## Example

Running Reek on:

```Ruby
class Dummy
  def test
    @ivar
  end
end
```

would report:

```Bash
  [1]:InstanceVariableAssumption: Dummy assumes too much for instance variable @ivar
```

Note that this example would trigger this smell warning as well:

```Ruby
class Parent
  def initialize(omg)
    @omg = omg
  end
end

class Child < Parent
  def foo
    @omg
  end
end
```

The way to address the smell warning is that you should create an `attr_reader` to use `@omg` in the subclass and not access `@omg` directly like this:

```Ruby
class Parent
  attr_reader :omg

  def initialize(omg)
    @omg = omg
  end
end

class Child < Parent
  def foo
    omg
  end
end
```

Directly accessing instance variables is considered a smell because it [breaks encapsulation](http://designisrefactoring.com/2015/03/29/organizing-data-self-encapsulation/) and makes it harder to reason about code.

If you don't want to expose those methods as public API just make them private like this:

```Ruby
class Parent
  def initialize(omg)
    @omg = omg
  end

  private
  attr_reader :omg
end

class Child < Parent
  def foo
    omg
  end
end
```


## Current Support in Reek

An instance variable must:

* be set in the constructor
* or be accessed through a method with lazy initialization / memoization.

If not, _Instance Variable Assumption_ will be reported.

## Configuration

_Instance Variable Assumption_ supports the [Basic Smell Options](Basic-Smell-Options.md).

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
reek-5.1.0 docs/Instance-Variable-Assumption.md
reek-5.0.2 docs/Instance-Variable-Assumption.md
reek-5.0.1 docs/Instance-Variable-Assumption.md
reek-5.0.0 docs/Instance-Variable-Assumption.md
reek-4.8.2 docs/Instance-Variable-Assumption.md