1
if !store.url.nil? and store.url != store.new_data.url

OR

if ! store.url.nil? && store.url != store.new_data.url

OR

if (!store.url.nil? && store.url != store.new_data.url)

Main items I'd like to get advice on:

  1. Is it better to put a space after the bang?

  2. && -or- and

  3. || -or- or

Hopstream
  • 6,391
  • 11
  • 50
  • 81
  • 1
    @Close voter: `&&` is not synonymous with `and`, nor `||` synonymous with `or`, so this is not a "not constructive" question. – Andrew Grimm Nov 02 '11 at 11:46
  • @Andrew. I beg to differ. They may not be synonymous but still I think it's a valid doubt. It looks more like a partly duplicated: http://stackoverflow.com/questions/2083112/ruby-difference-between-and-or – tokland Nov 02 '11 at 16:55

3 Answers3

4

You must always use "&&" or "||" in an boolean statement.

"and" and "or" should be used for control flow like if or unless.

If you're working in Rails, you can also do

if store.url.present? && store.new_data.url != store.url

Do not put spaces after the negation exclamation. Using brackets willy nilly is frowned upon in the general ruby coding conventions, so avoid if you can.

Aditya Sanghi
  • 13,370
  • 2
  • 44
  • 50
3

I'd write:

if store.url && store.url != store.new_data.url

(Write store.url.present? if a blank URL string is to be considered a "nil" value)

Notes:

  • Don't use nil? unless you explicitly want to distinguish between nil and false (which are the only false values in Ruby). These cases are rare.

  • No spaces after the bang.

  • Use the more idiomatic &&/|| over and/or.

  • Don't write parentheses covering a complete if/unless expression, they are unnecessary (and not idiomatic) in Ruby.

tokland
  • 66,169
  • 13
  • 144
  • 170
  • No, no, no. The higher precedence of `&&` will usually necessitate *more* parentheses in statements like this. `and` is the correct choice here. – Marnen Laibow-Koser Nov 02 '11 at 14:17
  • @Marnen: you're right, a higher precedence will require more parentheses in some situations, answer edited. However, the idiomatic usage is overwhelming, people uses &&, ||. It's my understanding that core Ruby core devs even reject patches containing and/or for this usage. – tokland Nov 02 '11 at 14:26
  • No. It's more idiomatic to use `and` in almost all cases (it's more English-like and therefore more readable). I believe Matz himself has suggested that, though I could be wrong. This is the first I've ever heard of the core team rejecting `and`. Your source? – Marnen Laibow-Koser Nov 02 '11 at 15:15
  • I had to search because I didn't remember where I had read it, it was a statement by @Jörg W Mittag here http://stackoverflow.com/questions/2083112/ruby-difference-between-and-or. BTW, you say that "and" is more idiomatic because is more English-like, but "idiomatic" is what the community uses more. For the code I've read, programmers overwhelmingly prefer &&/|| over and/or. – tokland Nov 02 '11 at 16:50
  • This programmer does not. `&&` should be used for complex Boolean math, not simple conditionals. And the Rails core team does lots of things that I'd consider poor practice. Jörg is just wrong, I think. – Marnen Laibow-Koser Nov 02 '11 at 18:38
0

I'd do: if store.url and store.url != store.new_data.url or (if you're using Rails or Ruby 1.9) if store.try(:url) != store.new_data.url

Remember, nil is falsy in Ruby.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33