On-the-floor

On the Floor

Operator Precedence and Code Readability in Ruby

I was reading about operator precedence in ruby the other day and noticed that the "and" and "or" operators are NOT the same as the && and || operators.  They are actually about 6 levels of precedence below && and ||, meaning that there is a lot of stuff that gets processed before "and" and "or" become relevant.

This means that your logical expressions can become a little more readable by using a mix of the two kinds of logical operators.  Compare this:

(var1 || var2) || (var3 && var4)

with:

var1 || var2 or var3 && var4

Comments

There are no comments to display.

Add a Comment

Your Comment:

Your Name:

Your Email Address: (Won't be published)

Your Website: (If you've got one)