zuhao / plotrb

A plotting library in Ruby built on top of Vega and D3.
Other
42 stars 12 forks source link

Use attr_accesor when posible #9

Closed clbustos closed 11 years ago

clbustos commented 11 years ago

In many classes, you use methods like

def name; @name; end

In Ruby, Is conventional to replace all getters and setters with attr_reader :name attr_writter :name

So, please use the first form and document it.

zuhao commented 11 years ago

Yes I am aware of attr_*, and I have thought about using attr_reader + custom setter for attributes. However, the rationale for not doing so was that it might give the wrong impression of the code.

For example, when I see attr_reader, the first thought is that the attribute is read-only by outsiders. But it's actually not the case, and not until you find the corresponding setter in the code you'll realize the attribute is both readable and writable.

Is there other drawback, perhaps performance-related, besides slightly longer code?

zuhao commented 11 years ago

Or maybe I can use attr_accessor and override the setter? This sounds like a better approach.

clbustos commented 11 years ago

Zuhao: Yes, I think it could be better, only if you delegate the responsability of the validation in the object that you uses as an input.

On Sun, Jul 7, 2013 at 9:33 PM, Zuhao Wan notifications@github.com wrote:

Or maybe I can use attr_accessor and override the setter? This sounds like a better approach.

— Reply to this email directly or view it on GitHubhttps://github.com/zuhao/plotrb/issues/9#issuecomment-20581768 .

Claudio Bustos Psicólogo clbustos@gmail.com

translunar commented 11 years ago

I agree with @clbustos. I think you should use attr_reader and write your own setter. That's a very common use-case. I believe attr_reader may in fact be faster.

zuhao commented 11 years ago

Thanks @clbustos and @mohawkjohn for reminding me. I apparently overlooked this issue in the first place. I've written a summary on this in my new blog post.