Brennan Dunn Web Application Developer

Thoughts, ramblings, and other sundry topics.


On comparisons, having 0 replies


Even some really great developers far superior to me make mistakes, I discovered. I was browsing through Github today, and came across the following:

def is_hd?  
  if self.is_hd == 1
    return true
  else
    return false
  end    
end

This wasn't by an amateur developer, in fact, this code comes from a really great Ruby developer (name withheld). The if condition, like any condition, is asserting something. The above is simply checking if is_hd is equal to the number 1. You know that, I know that.

By now you've discovered a pet peeve of mine. Why is the author of this code returning true if the condition evaluates to true? How should this code have been written?

def is_hd?
  is_hd == 1  # obligatory dropping of self when not needed
end
Name
Email
Website
 
Alert you when new comments are posted?