Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

option 1

data = 
  if page <= total_pages
    collection[((page - 1) * limit)...(page * limit)]
  else
    []
  end

option 2

data = []
if page <= total_pages
  data = collection[((page - 1) * limit)...(page * limit)]
end

option 3

if page <= total_pages
  data = collection[((page - 1) * limit)...(page * limit)]
else
  data = []
end

option 4

data = collection[((page - 1) * limit)...(page * limit)] || []

I personally prefer option 1, but, unfortunately, it doesn't look as nice. I was just wondering if this was addressed in some ruby style guide.

share|improve this question

closed as off topic by Yannis, Corbin, Brian Reichle, Jeff Vanzella, James Khoury Oct 11 '12 at 1:03

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

up vote 5 down vote accepted
  • Option1. That's the one I'd use, although I usually prefer a different layout:

    data = if page <= total_pages
      collection[((page - 1) * limit)...(page * limit)]
    else
      []
    end
    
  • Option2: I'd strongly advise against it. When you write x = 1 in math you don't expect x to change its value afterwards, do you? the same applies when writing software (aka functional programming). This way the code becomes more clear.

  • Option3: Not bad, though if is an expression in Ruby, no need to use it as a statement (problems: 1) you repeat the variable name, 2) you have to inspect each branch to see what's been done)

  • Option4: it's orthogonal to your question. If you can write it, cleanly, as a one-liner without a conditional, do it. Here IMO it's on the edge, the expression is a bit too complicated.

  • Option5: the obvious, if it fits in one line, use condition ? value_true : value_false.

share|improve this answer

There's also

data = page > total_pages ? [] : collection[((page - 1) * limit)...(page * limit)]

... which I think I'd prefer. It's just option 1 as a ternary, but still.

Or, you could toss it in a method, and do

return [] if page > total_pages
collection[((page - 1) * limit)...(page * limit)]

Otherwise I'd probably say option 3, for it's readability and straightforwardness.

Lastly, as in your option 4, an out-of-bounds range will return nil. You could just let that nil happen. If page is larger than total_pages it seems appropriate enough.

share|improve this answer
One would hope an out-of-bounds range would [], but it returns nil. [1, 2, 3, 4, 5][500...1000] returns nil. – Bradford Oct 7 '12 at 2:04
@Bradford Whoops, yeah, you're right. I'll remove that bit my answer. Must've done something wrong when I messed around in irb – Flambino Oct 7 '12 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.