Placement of success code in a conditional I'm not sure if there's a standard view where the placement of a success result should be in a conditional that can return multiple statuses. The success condition of this function is in the middle of the conditional block:

def redeem(pin)
  success = false
  message = nil
  if !self.date_claimed.nil?
    message = 'Reward already claimed'
  elsif self.reward.redemption_pin == pin
    success = true
    self.date_claimed = Time.zone.now
    self.save
  else
    message = "Wrong pin"
  end
  return {:success =&gt; success, :message =&gt; message}
end


I could rewrite it to be at the end:

def redeem(pin)
  success = false
  message = nil
  if !self.date_claimed.nil?
    message = 'Reward already claimed'
  elsif self.reward.redemption_pin != pin
    message = "Wrong pin"
  else
    success = true
    self.date_claimed = Time.zone.now
    self.save
  end
  return {:success =&gt; success, :message =&gt; message}
end


or I could rewrite it to be at the top but conditions become slightly more complex:

def redeem(pin)
  success = false
  message = nil
  if self.reward.redemption_pin == pin &amp;&amp; self.date_claimed.nil?
    success = true
    self.date_claimed = Time.zone.now
    self.save
  elsif date_claimed
    message = 'Reward already claimed'
  else
    message = "Wrong pin"
  end
  return {:success =&gt; success, :message =&gt; message}
end


I personally think I prefer it to be at the beginning or the end, but in this function the end seems to be the best place.
Some notes:


Imperative programming has its use cases (or so I've heard :-)) but implementing logic is definitely not one of them. Some thoughts on the matter. Use always expressions, not statements, to describe logic; that's it, don't begin with a x = 1 and then modify x somewhere else in the code. Write expression branches instead (conditionals are in expressions in Ruby). Don't think in terms of "how" but in terms of "what".
if !x.nil? -> if x.
obj.save may fail but you are not checking it.
Don't write explicit return (more on idiomatic Ruby).
As you say the order of checks is important. I usually check for "problems" first, and keep the "ok" scenario for the last branch.
You are using a value (a hash) instead of an exception to signal errors. It's slightly not idiomatical in Ruby (in the sense that people tend not to do it, not that there's anything wrong with it), but personally I like it (and use it).


You could write:

def redeem(pin)
  success, message = if date_claimed
    [false, 'Reward already claimed']
    ...
  end
  {:success =&gt; success, :message =&gt; message}
end


However, returning {success: ..., message: ...} on each branch, yet slightly more verbose, looks pretty nice and declarative:

def redeem(pin)
  if date_claimed
    {success: false, message: "Reward already claimed"}
  elsif reward.redemption_pin != pin
    {success: false, message: "Wrong pin"}
  else 
    update_attribute(:date_claimed, Time.zone.now)
    {success: true}
  end
end