3

I see, C++ compilers support && and its textual equivalent and operator (Similar to || operator to or). I tend to like its textual form compare to special character form. But, recently I am discouraged from using textual form of such operator without any concrete reasoning. I don't seriously think, textual form will require more typing compare to special character form. Also, there won't be much on compiler overhead on parsing part. So why major C++ community tend towards && instead and. While I think, textual form is much clear while stating a condition to beginner. It also promote more poetry style coding rather than bombarding code with gibberish character.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
user3401643
  • 110
  • 1
  • 7
  • They [are functionally the same thing](http://stackoverflow.com/questions/17365271/and-and-operator/18186331#18186331) ... although as pointed out in my answer most people will probably find `and` confusing since it is rarely used. – Shafik Yaghmour Mar 10 '14 at 12:18
  • The main purpose of the alternate tokens was once upon a time to support non-English programmers whose keyboards were lacking some of the characters required for C++. Nowadays you can use them to sneak rvalue references past overly strict reviewers who don't like any of "that modern stuff". – Kerrek SB Mar 10 '14 at 12:20
  • 2
    absolutely no overhead on either choice – bolov Mar 10 '14 at 12:20
  • I know they are the same. My question is related to reasoning behind coding style. – user3401643 Mar 10 '14 at 12:20
  • 1
    @user3401643 then it's not relevant on stackoverflow, as you're asking for opinions, and there is no one good answer to that kind of questions. cf http://stackoverflow.com/help/dont-ask – zmo Mar 10 '14 at 12:22
  • so that means, There is no concrete reasoning behind widespread use of '&&' operator rather than 'and' operator. – user3401643 Mar 10 '14 at 12:25
  • 2
    Once you have larger bodies of code you'll see that the "gibberish" characters actually make the code _more_ readable. It helps you separate operations from names. Code like `assign foo to bar` requires you to read and parse the code in your head. `foo = bar` provides you with a shortcut. The same is true for `if (something and aNumber >= 0)` instead of `if (something && aNumber >= 0)`: the `&&` is separating the conditions visually. – DarkDust Mar 10 '14 at 12:28
  • @DarkDust Syntax highlighting does the same thing. The only reason for `&&` rather than `and` today is historical. But since its use is pretty much universal... – James Kanze Mar 10 '14 at 12:56

2 Answers2

5

Just my personal preference here; visually the && breaks up two identifiers better than and does. So at a glance it is a bit quicker to mentally parse the expression.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 2
    I believe, if you were start using 'and' instead, it will make more sense for its usage rather that '&&' – user3401643 Mar 10 '14 at 12:27
  • 3
    @user3401643: I've started with BASIC 25 years ago and I've found that too much "prose" code (like BASIC is trying) is actually _way harder_ to quickly parse. The use of characters that cannot be part of a name (type, function, namespace, whatever) stand out like beacons and aid you in breaking it down into pieces. – DarkDust Mar 10 '14 at 12:33
  • 2
    As an example, try to find the error: `if color is blue and day is friday and month is february and day is monday` vs. `if (color == blue && day == friday && month == 3 && day == monday)`. The later is easily visually divided into different parts that you can parse on their own. The former requires you to remember the _context_: by the time you hit the second `day` it's harder to go back to the first `day` without losing your reading position. – DarkDust Mar 10 '14 at 12:41
  • Even more so with assignment operators. `a |= b` clearly has a side effect; `a or_eq b` not so much. – Mike Seymour Mar 10 '14 at 12:41
  • @DarkLust: That made perfect sense to me. Thanks for sharing your insight here. – user3401643 Mar 11 '14 at 08:28
0

This is kind of a standard in most of the major languages.. You got to accept it as syntax and should not argue about how it should be. Language isn't designed according to individual liking or disliking. You're going to find many somewhat confusing things in C/C++ which were taken care of in modern languages like python etc. But C, Java are still most widely used languages so you must follow their syntax if you wish to program in these languages.

Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
  • I am accepting both operator as valid syntax and i dont believe they are confusing. I was just curious enough to know why one style got so super-used compare to other (which is non-existent). – user3401643 Mar 10 '14 at 12:29
  • The very fact that you got curious about it makes it confusing.. And I guess it's just the personal preference of most people that made && more widely used. – Abdul Jabbar Mar 10 '14 at 12:32
  • `&&` is certainly more widely used because it was there first. We had C for years and years, and we had non-standardized C++ for years, before the alternative tokens were added. Then people write code based on what they saw someone else write, not what the standard says (this explains why so many people cast malloc in C, 25 years after it was rendered unnecessary) – M.M Mar 10 '14 at 12:43
  • 1
    "You got to accept it as syntax"... but both forms are syntactically valid. The question is, is there any reason to prefer one valid form to the other? – Mike Seymour Mar 10 '14 at 12:45
  • I never said other form is syntactically incorrect.. And yes you pretty much accept what's their in the language. Why'd you argue about it in the first place. Use the one which you like but mostly people use && because of course it automatically takes a programmer's mind towards 'and' condition. – Abdul Jabbar Mar 10 '14 at 12:50