def following?(followee)
return true if followees.find_by_followee_id(followee.id)
return false
end
def following?(followee)
!followees.find_by_followee_id(followee.id).nil?
end
Refactorings
No refactoring yet !
Adam
April 8, 2009, April 08, 2009 15:10, permalink
"The Rails Way" is to do this:
def following?(followee) followees.find_by_followee_id(followee.id) end
Tj Holowaychuk
April 8, 2009, April 08, 2009 15:19, permalink
If you actually required a bool for whatever reason I personally just double bang it, however usually nil from the method call does just fine without explicitly doing this like this
# unless find_by_followee_id returns false already ... I am not a rails guy I dont know def following?(followee) !! followees.find_by_followee_id(followee.id) end
imnotquitejack
April 9, 2009, April 09, 2009 13:28, permalink
My preference is toward ternaries to keep things concise, readable, and boolean.
def following?(followee)
followees.find_by_followee_id(followee.id) ? true : false
end
Tj Holowaychuk
April 9, 2009, April 09, 2009 15:20, permalink
No need for the ternary operator, double bang will give you a bool
Tj Holowaychuk
April 9, 2009, April 09, 2009 15:21, permalink
Personally the only time you should explicitly return a bool is when the method uses the question mark, otherwise a simple nil is fine IMO as that is the result of most failing iterators etc, so it is more natural
Tj Holowaychuk
April 9, 2009, April 09, 2009 15:31, permalink
haha article from a rails guy... wooo.. thats lots of help. Do you not agree that when you are 'asking' a method foo? that it should not return a bool? hence the question mark, I dont know thats my take on english.
Adam
April 9, 2009, April 09, 2009 15:44, permalink
No, it's a pointless conversion. You will never explicitly compare a question mark method to a boolean. That is bad style. Keep the code as simple as possible.
Tj Holowaychuk
April 9, 2009, April 09, 2009 16:15, permalink
Perhaps not with Ruby since people just kinda fling things around
Sebastian
April 10, 2009, April 10, 2009 00:12, permalink
Basically, I try to return an object or nil if I set or get something, and just boolean values when it is a validator/predicate. If a method returns just as you need, you shouldn't bloat the code with extra returns or if-then-ends.
Sam
August 1, 2009, August 01, 2009 16:14, permalink
@Adam wrote, "You will never explicitly compare a question mark method to a boolean. That is bad style."
No, but I quite often compare the results of two predicates. And, this is *NOT* bad style. How many languages do you know have a logical-XNOR operator?
Cry me a river. Predicates are intended to return true or false. I've (fortunately) never had the utter misfortune to experience otherwise while coding in Ruby. The whole reason the "?" character is allowed in symbol names to begin with is to encourage a widespread coding convention where predicates return true booleans. Please don't break the language by not doing so.
Any thoughts on this refactoring? I know it's a personal preference. Just curious on how people lean on short circuiting with 'returns' vs a reversed boolean.