From: Justin Collins Date: 2013-01-07T11:34:10+09:00 Subject: Re: Best practice for &&, ||, and, or On 01/05/2013 04:35 AM, sto.mar@web.de wrote: > Hi group, > > I really would be interested in your thoughts about this > > a. in the context of your own projects, and > b. with regard to teaching programming newbies > > The GitHub Ruby Styleguide [1] states: "The and and or keywords are > banned. It's just not worth it. Always use && and || instead". > > So what's your practice/recommendation: > > 1. use only &&, || > 2. use only `and', `or' > 3. distinguish between boolean expressions (if this && that) > and control flow (loot = give_money or die) > 4. ??? > > 1. and 2. might require extra parentheses that often could be > avoided with 3., but using parentheses might be clearer anyway. > Especially for students who might have a hard time coping with > the different precedences. > > I tend to 1., and for own projects maybe to 3. > > Regards, > Marcus > > > [1] https://github.com/styleguide/ruby/ > I think I'm in the minority here, so I'll give my minority opinion: For my own projects I prefer and/or and only use &&/|| for assignments. Looking through one of my codebases, I see 312 uses of "and" and 8 uses of "&&" (mostly from other contributors). Why? Because I also dislike using parentheses for method calls, which again seems to be becoming a minority preference. (Also, by default vim highlights and/or but not &&/|| and I like pretty colors.) irb(main):001:0> def x *args; true; end => nil irb(main):002:0> def y *args; true; end => nil irb(main):003:0> if x 1 and y 2 irb(main):004:1> puts "all good" irb(main):005:1> end all good => nil irb(main):006:0> if x 1 && y 2 irb(main):007:1> puts "not all good" irb(main):008:1> end SyntaxError: (irb):6: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' (irb):8: syntax error, unexpected keyword_end, expecting $end from /home/justin/.rvm/rubies/ruby-1.9.3-p327/bin/irb:12:in `
' I haven't spent much time teaching others, so I don't really have an opinion on that. I assume those coming from other languages would be more comfortable with &&/||. I also prefer to use "not" because that stands out more to me than "!". -Justin