Does anyone else finds attribute query methods semantically confusing?
Hi,
Consider this code:
post = Post.new(:visible => true, :url => “http://com”)
if post.visible?
puts “ima visible!”
end
if post.url?
puts “ima url! (wait wat? o_0)”
end
Does this feel right to you? In case with post.url? i read it as “Hey post object, are you an url?” which is apparently not what the code will do.
So it seems like semantically perfect flag checks go together with totally confusing(for a reader) way of checking whether an attribute is present or not.
I would generate attribute query methods only for boolean attributes.
I am not sure that I understand your point, but in Ruby anything that
is neither nil nor false is true. Thus returning the url string, if
not nil, is the same as saying that it is true but also allows access
to the actual data without having to send another message.
If we want to kill ? on non boolean attributes we should encourage standard @post.url.blank? and @post.url.present? be used instead. The originally proposed change makes sense to me. The question mark character adds nice semantics to boolean attributes. I agree it’s not clear what exactly that would do on non-boolean attributes. Removing the questionable methods would help decrease the method count.
Actually the query attribute method does more than check if the value is present. It is very useful for legacy databases where the user persist boolean values as [0, 1], [“t”, “f”], [“true”, “false”] and more.
I don’t see this being removed from rails or changing your behavior.
Also removing it will not decrease the methods count because we still need to generate it for booleans.
Actually the query attribute method does more than check if the value is present. It is very useful for legacy databases where the user persist boolean values as [0, 1], ["t", "f"], ["true", "false"] and more.
I don't see this being removed from rails or changing your behavior.
I agree with you on that
Also removing it will not decrease the methods count because we still need to generate it for booleans.
It would decrease because they would *only* be generated for boolean fields.
but if we decide that there doesn't have to be semantical contribution to
your code from using attribute query methods then #{attribute}? is just
a shortcut for !!#{attribute} - so why do we need it in the first place?
saving 1 character is weird goal here.
It's not saving a character. It's that !! is programmer talk, and ? is
human talk. Ruby prefers human talk.
I guess Rafael meant another thing. You have boolean fields in PostgreSQL but someone might prefer to use some old ANSI types, like INTEGER for storing such booleans for some reason.
And in these cases you don’t know if these fields are boolean or not. If you are choose to store the visible column as string [“yes”, "no] so you will not have the query method.
I like this method as it is. For me it is very useful and give me the possibility to not care about what is the datatype of the field.
It’s not saving a character. It’s that !! is programmer talk, and ? is
human talk. Ruby prefers human talk.
well your kung-fu beats my kung-fu here
i just don’t think post.url? is talking proper human talk to me, i would
expect answer to that question to be “no, post is not a url” instead i
will get “yes, url attribute is not empty”.
I guess Rafael meant another thing. You have boolean fields in
PostgreSQL but someone might prefer to use some old ANSI types, like
INTEGER for storing such booleans for some reason.
i think that first of all this is rather an edge-case that shouldn’t force
us to go against least surprise principle
second - this directly goes against Steve’s assumption that
post = Post.new
post.id = 0
assert post.id?
should not fail (it fails because 0 is apparently false… since when?)
i personally think this smells a lot like PHP and it’s non-logical,
generally ugly and wrong truthiness/falseness checks
Maybe it is just me, but I don’t use features that I don’t like in rails, just like I avoid calling private methods. In that sense, there’s nothing that is preventing anyone from using post.body.present? instead of post.body?. My argument would be to let rational programmers decide what is best for their specific case. If you’re on a team, set a style guide that picks a winner.