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:

2 Answers
2

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

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 can remove it, and later in your code use the double bang to get a boolean value

return {:success => !!success, :message => message}

I mean get rid of lines that didn't provide any functionality, in this case you will save a line, remaining the reability of the code.

And when you set success to true, always write the condition for it, when it should be true.
In your second example, you mark your method as sucessfull in an else block. That means for me: "If none of the previous conditions met, I'm sucesful", but what happens, when aditional failure scenario will be written above the else block, or other stuff happen above the else block ?

You definately should precise describe the conditions for a sucess state (like example 3)