public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Changing "and" to && whereever I catch it
David Heinemeier Hansson (author)
Sun Apr 27 15:15:50 -0700 2008
commit  162c7c1908946cfb48c201cfc5a4976a33c8bff1
tree    f09f5e887bedb4d44a7c6884d471b0bdcc34293c
parent  a37546517dad9f6d9a7de6e1dba4d960909d71e8
...
165
166
167
168
 
169
170
171
...
165
166
167
 
168
169
170
171
0
@@ -165,7 +165,7 @@ module ActionController #:nodoc:
0
       # method if you wish to redefine the meaning of a local request to
0
       # include remote IP addresses or other criteria.
0
       def local_request? #:doc:
0
-        request.remote_addr == LOCALHOST and request.remote_ip == LOCALHOST
0
+        request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST
0
       end
0
 
0
       # Render detailed diagnostics for unhandled exceptions rescued from

Comments

klondike Sun Apr 27 16:13:59 -0700 2008

Oh, boo, booooo.

timocratic Sun Apr 27 17:21:52 -0700 2008

It makes sense to be consistent, Klondike. Due to the low precedence of “and” and “or” they work best as postfix jump statements/control flow like “and return” or “or raise.” While && and “and” are equivalent in this usage, it’s good to stick to ones own conventions, as there are other times it can bite you (like in assignments).

anildigital Sun Apr 27 21:01:58 -0700 2008

Hi David, Could you justify please why ‘&&’ and not ‘and’?

radar Sun Apr 27 23:21:53 -0700 2008

Maybe this might give you a clue: http://www.pjhyett.com/posts/201-using-or-and-in-ruby

dhh Mon Apr 28 07:28:41 -0700 2008

PJ Hyett’s rundown is good. The 95% case is to want the && behavior and when it doesn’t matter it’s better to use ‘&&’ for consistency.

klondike Tue Apr 29 08:00:07 -0700 2008

I get the argument for consistency, but in this case consistency isn’t worth it to me, ‘and’ and ‘or’ are so much more readable that I’ll use the && alternative only when I need to. Obviously it’s subjective, but I think it’s one of the nicer syntactic sugars in Ruby and I’m surprised to see the Rails community pass it by because someone finally pointed out a pitfall associated with it.

josh Tue Apr 29 10:15:37 -0700 2008

Klondike, you’re free to use either in your Rails project. We just prefer && and || in the Rails source because it is easier for use to understand what is going on and avoid any unintended consequences.

defunkt Wed Apr 30 13:05:19 -0700 2008

Inconsistent code is less readable than consistent code. Hurray &&.

stephencelis Wed Apr 30 13:20:48 -0700 2008

Truly.

I like throwing “ands” around and dropping as many parentheses as possible when I’m feeling why-the-lucky-stiff-ish, but I don’t want that stuff in code I want to maintain.

elliottcable Wed Apr 30 13:52:55 -0700 2008

Awesome new word: whytheluckystiffish d-:

radar Thu May 01 07:55:02 -0700 2008

Klondike:

Some people replace the words ‘you’, ‘are’ and ‘two’ with ‘u’, ‘r’, and ‘2’. Surely you can read && as and and || as or.