Tilo Sloboda
Aug 2, 2023

--

this approach is treating a, b, c as variables that are accessible like "global" variables throughout the code of the class... yes, it works.

Referencing a, b, c in that manner is OK if your class is really small, but if it is not, and you have more parameters, the code quickly becomes hard to read.

Referencing the variables like this in methods is basically using a side-effect to get to their values. Using attr_reader further obfuscates that these are instance variables.

For readability and testability it is better to pass all the parameters to each method call (think functional programming style — no side effects).
This way we can always reason about what each individual method does, even if there is a lot of code in the class.

--

--

Responses (1)