I found that code in the actual codebase of a web app I’m working on.
What do you think about that?
def is_a_feature?
self.featured?
end
def is_not_a_feature?
!self.featured?
end
I just want to know who written the second method, just for curiosity…
El
On Fri, Feb 15, 2013 at 12:25 PM, Elr0ndK Asda [email protected]
wrote:
I just want to know who written the second method, just for curiosity…
Why do you ask here who wrote the second method? How could we possibly
know?
Cheers
robert
Elr0ndK Asda wrote in post #1097056:
I found that code in the actual codebase of a web app I’m working on.
What do you think about that?
I don’t know - you might expect something from the below to explain
you,which is not clear to us. Please make it specific …
def is_not_a_feature?
!self.featured?
end
I just want to know who written the second method, just for curiosity…
El
On 15.02.2013 16:22, Elr0ndK Asda wrote:
featured is a boolean field of the related database table and in my
thought, adding an attr_reader for the featured field, there are no
needs of other methods.
It’s quite common in Ruby to alias methods if that improves
readability. Whether you consider the additional code worthwhile seems
to me a pretty subjective call. Generally I would agree with you though
that generating a whole new method just for negation seems a bit OTT. I
would also rewrite the first method using alias to make it even more
obvious that that is all it is.
On Fri, Feb 15, 2013 at 5:46 PM, Alex G.
[email protected] wrote:
It’s quite common in Ruby to alias methods if that improves readability.
Whether you consider the additional code worthwhile seems to me a pretty
subjective call. Generally I would agree with you though that generating a
whole new method just for negation seems a bit OTT. I would also rewrite the
first method using alias to make it even more obvious that that is all it
is.
Absolutely agree! I also find the “is_a_” prefix ugly - usually where
in Java you have isFeature() in Ruby you just have feature?. And
given that doing
alias feature? featured?
doesn’t really sound worthwhile I’d probably scrap both methods.
Kind regards
robert
The second one has been written only for a conditional validation (in
the same model), example:
- validate_presence_of :field, :if => :is_not_a_feature?
But Ruby have also the unless, so it seems to me absolutely unuseful.
In addition, I’m not expert of Rails and Ruby, but I think also that the
first method is redundant:
featured is a boolean field of the related database table and in my
thought, adding an attr_reader for the featured field, there are no
needs of other methods.