Bf4d36351bb61a46ff5ce24337816183

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.

1
2
3
4
5
6
7
8
  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 !

A8d3f35baafdaea851914b17dae9e1fc

Adam

April 8, 2009, April 08, 2009 15:10, permalink

No rating. Login to rate!

"The Rails Way" is to do this:

1
2
3
def following?(followee)
  followees.find_by_followee_id(followee.id)
end
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 8, 2009, April 08, 2009 15:19, permalink

No rating. Login to rate!

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

1
2
3
4
5
# 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
Avatar

imnotquitejack

April 9, 2009, April 09, 2009 13:28, permalink

No rating. Login to rate!

My preference is toward ternaries to keep things concise, readable, and boolean.

1
2
3
  def following?(followee)
    followees.find_by_followee_id(followee.id) ? true : false
  end
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 9, 2009, April 09, 2009 15:20, permalink

No rating. Login to rate!

No need for the ternary operator, double bang will give you a bool

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 9, 2009, April 09, 2009 15:21, permalink

No rating. Login to rate!

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

A8d3f35baafdaea851914b17dae9e1fc

Adam

April 9, 2009, April 09, 2009 15:26, permalink

No rating. Login to rate!
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 9, 2009, April 09, 2009 15:31, permalink

No rating. Login to rate!

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.

A8d3f35baafdaea851914b17dae9e1fc

Adam

April 9, 2009, April 09, 2009 15:44, permalink

No rating. Login to rate!

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.

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 9, 2009, April 09, 2009 16:15, permalink

No rating. Login to rate!

Perhaps not with Ruby since people just kinda fling things around

Ae1e46c702ceb87f8c4ee37f96d4df87

Sebastian

April 10, 2009, April 10, 2009 00:12, permalink

No rating. Login to rate!

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.

932264dd4560a4fc6f3100be6a9b6612

Sam

August 1, 2009, August 01, 2009 16:14, permalink

No rating. Login to rate!

@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.

Your refactoring





Format Copy from initial code

or Cancel