开发者

In environments that take Boolean arguments, is it a good idea to wrap all functions instead of allowing them to be implicitly coerced?

Take the String#=~ function for instance. It will return the index of the first match if the match is found, which, as a Fixnum will always act as true in boolean environments. If the match isn't found, it returns null, which acts as false.

Now suppose I have a class:

class A
  attr_accessor :myprop

  # prints "I am awesome" if #myprop matches /awesome/
  # and "I am not awesome" otherwise
  def report_on_awesomeness!
    puts "I am #{myprop =~ /awesome/ ? 'awesome' : 'not awesome'}."
  end
end

This code will pretty much work just as expected, but the first element in the trial conditional operator is the subject of my question.

Is it a good idea not to wrap myprop =~ /awesome/? I'm not talking about abstracting it into another method like def is_awesome?; myprop =~ /awesome/; end but rather whether my current convention, which forces Ruby to implicitly casts Fixnums to true and nils to false, is preferable over wrapping the condition into something I cast myself. I could easily do this:

class A
  attr_accessor :myprop

  # prints "I am awesome" if #myprop matches /awesome/
  # and "I am not awesome" otherwise
  def report_on_awesomeness!
    puts "I am #{(myprop =~ /awesome/).nil? ? 'not awesome' : 'awesome'}."
  end
end

Pros I see for the first style:

  • Most maintainers (including future me) are used to the implicit type
  • It's shorter

Pros I see for the second style:

  • It's more obvious exactly what the relationship is between the result of the =~ method and its boolean interpretation
  • It gives you more freedom to use more creative explicit casting

I suspect that there might be some middle ground, where you leave implicit type conversions in cases where it's idiomatic (e.g., regular expression matching using =~) and do it explicitly when it's not (e.g., your own propert开发者_运维技巧ies, especially if they have multiple return types).

I would appreciate any insights or experiences the community can share on this issue.


IMHO that's a personal choice. You can take any style, since you feels better by working with that.

Once I defined true? on Object to get its boolean value (another name could be to_bool):

class Object
  def true?
    !!self
  end
end

But the double bang (!!) is simpler to convert anything to Boolean and I prefer to use it - but not everywhere. I use it only when I need explicity a boolean value (I wouldn't use it in the case of this question).

BTW, false.nil? == false; it could lead to confusion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜