def sanity
@sanity
end
def sanity=(val)
@sanity = val
end
how are those six lines not boilerplate? def sanity
@sanity
end
def sanity=(val)
@sanity = val
end
how are those six lines not boilerplate?The benefit everything-is-a-message is that you can swap out the implementation of sanity (or sanity=) if needed, eg.
Before:
attr_accessor :radius, :diameter
After: # Refactored radius to be the new source of truth.
def diameter
radius * 2
end
def diameter=(val)
self.radius = val / 2
end
In PHP (for example), MyClass->diameter and MyClass->diameter() are completely different things. Any existing code (including code you don't control) needs updating to change references from the former to the latter when moving logic into something that was previously static.This article is flawed by holding up those six lines in themselves as something great, but I think the point the author is trying to get across is valid.
http://ruby-doc.org/core-1.9.3/Module.html#method-i-attr_acc...
The above can be written as
attr_accessor :sanity attr_accessor :sanity
def initialize
@sanity = 0
end
On that note I like Go's default values for primitive types. An int sanity would be zero by default.Edit: seeing five simultaneous posts saying the same thing makes me feel trolled a bit :)
attr_accessor :sanity
Would be a neater way to write this. attr_accessor :sanity