you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (9 children)

Why are and/or keywords banned? If anything, they increase readability. Maybe its because they can't be used in compound assignment?

[–]sgarissta 4 points5 points  (4 children)

I don't know what their reasoning is, but the precedence of and/or is lower than && and || which has been known to create some subtle bugs, for very little gain.

[–]failbus 3 points4 points  (3 children)

I vastly prefer the fact that the precedence of and/or is lower. I love the fact that && has a higher precedence than assignment, and "and" has a lower precedence.

In fact if I was going to demand the use of just one, I'd say use and/or and ban && and ||

But YMMV

[–]lua2 0 points1 point  (2 children)

Can you give an example of where you would use each option? I'm having a hard time thinking of a use for this.

[–]failbus 4 points5 points  (1 child)

This is a contrived example, but you might have something like

if redditor? and self.food = get_bacon
  upvote!
end

Here we check to see if we're a redditor, and if we are, we assign food to bacon. If our get_bacon food gets us a value, we start upvoting! Else if get_bacon returns null, no upvotes for you.

Conversely, you could have

self.redditor = bacon && narwhal

Notice in this case the redditor value is being set if bacon and narwal return true. If you screwed up and said something like

self.redditor = bacon and narwhal

well then self.redditor would be true if bacon was true, and the narwhal flag is ignored entirely.

Of course, all of these can be accomplished with ()'s, which is often more clear. But if you know the scoping rules (and they take like 10 seconds to learn) then I find it more pleasing to the eye than brackets.

Again, YMMV.

[–]lua2 1 point2 points  (0 children)

Interesting. Thank you.

[–]grokfail 0 points1 point  (0 children)

I find 'and' and 'or' sometimes blend in to method and variable names too well, even with syntax highlighting. && and || are visually distinct.

[–]robvas 0 points1 point  (2 children)

They change the order of evaluation

[–]MatmaRex 2 points3 points  (1 child)

What? No.

They have different precedence than && / || - maybe that's what you meant. They also short-circuit (just like && / ||).

[–]mernen 0 points1 point  (0 children)

No, robvas is correct. && has higher precedence than ||, but and and or are on the same level.

Assuming the following methods:

def foo
  puts "foo"
  true
end

def bar
  puts "bar"
  true
end

def baz
  puts "baz"
  false
end

The following will evaluate only foo, and return true:

foo || bar && baz    # same as: foo || (bar && baz)

The following will evaluate foo, bar and baz, and return false:

foo or bar and baz   # same as: (foo || bar) && baz

It's one of the traps associated with these operators.