How we work

Prefer using attr_readers, attr_writers

We at Bigbinary prefer using attr_readers, attr_writers, attr_accessors etc. instead of instance variables of the class wherever possible.

Consider the following examples

Approach #1

1class Calculator
2  def initialize(number)
3    @number = number
4  end
5  def increment
6    @number + 1
7  end
8  def decrement
9    @number - 1
10  end
11end

Approach #2

1class Calculator
2  attr_reader :number
3  def initialize(number)
4    @number = number
5  end
6  def increment
7    number + 1
8  end
9  def decrement
10    number - 1
11  end
12end

While both the approaches give us desired results, we advocate Approach #2.

Reasons by which we support our opinion are as follows -

  • We feel directly accessing instance variable

makes the code look dirty with less OO.

  • We also feel that having attr_reader for the variables supports more abstraction i.e., one can tell difference between two objects of a class by virtue of these simple attribute reader methods.
⌘K
    to navigateEnterto select Escto close
    Previous
    Next