tags:

views:

512

answers:

12

Or are we all sticking to our taught "&&, ||, !" way?

Any thoughts in why we should use one or the other?

I'm just wondering because several answers state thate code should be as natural as possible, but I haven't seen a lot of code with "and, or, not" while this is more natural.

+11  A: 

I like the idea of the not operator because it is more visible than the ! operator. For example:

if (!foo.bar()) { ... }

if (not foo.bar()) { ... }

I suggest that the second one is more visible and readable. I don't think the same argument necessarily applies to the and and or forms, though.

Greg Hewgill
Yes, I agree with with this. I go even further and create a Not() function, because not only do I have trouble seeing the critter, I also have problems remembering its precedence!
anon
Function is a little bit overkill. Use parentheses instead!
inazaruk
Valid point. Although syntax hightlighting comes to the rescue, we do not have to rely on that.
Gamecat
I use `== false` instead of `!`, as its not visable enough for me.
billpg
If you don't find !foo.bar() visible enough you could also write !!!!!foo.bar(). Since 5 is an odd number, it is equivalent to !foo.bar().
Daniel Daranas
yeah, that's the only one I'm a bit queasy with.
peterchen
@Daniel, you must be joking. I wouldn't start counting the amount of "!"'es in that. I would just go to my fellow and ask him to remove that.
Johannes Schaub - litb
@litb: Yes, I was joking :)
Daniel Daranas
I think Daniel was !!!!!!!!joking
Draemon
+6  A: 

"What's in a name? That which we call &&, || or ! By any other name would smell as sweet."

In other words, natural depends on what you are used to.

Gamecat
ilivewithian
David Thornley
+3  A: 

Those were not supported in the old days. And even now you need to give a special switch to some compilers to enable these keywords. That's probably because old code base may have had some functions || variables named "and" "or" "not".

inazaruk
Dan
A: 

I like the idea, but don't use them. I'm so used to the old way that it provides no advantage to me doing it either way. Same holds true for the rest of our group, however, I do have concerns that we might want to switch to help avoid future programmers from stumbling over the old symbols.

Brian Knoblauch
+1  A: 

I personally like operators to look like operators. It's all maths, and unless you start using "add" and "subtract" operators too it starts to look a little inconsistent.

I think some languages suit the word-style and some suit the symbols if only because it's what people are used to and it works. If it ain't broke, don't fix it.

There is also the question of precedence, which seems to be one of the reasons for introducing the new operators, but who can be bothered to learn more rules than they need to?

Draemon
+3  A: 

Although I've been programming C++ from quite some time, I did not know that the keywords "and" "or" and "not" were allowed, and I've never seen it used.

I searched through my C++ book, and I found a small section mentioning alternative representation for the normal operators "&&", "||" and "!", where it explains those are available for people with non-standard keyboards that do not have the "&!|" symbols.

A bit like trigraphs in C.

Basically, I would be confused by their use, and I think I would not be the only one. Using a representation which is non-standard, should really have a good reason to be used. And if used, it should be used consistently in the code, and described in the coding standard.

Johan
+1  A: 

In cases where I program with names directly mapped to the real world, I tend to use 'and' and 'or', for example:

if(isMale or isBoy and age < 40){}
Dykam
A: 

So to summarize: it's not used a lot because of following combination

  • old code where it was not used
  • habit (more standard)
  • taste (more math-like)

Thanks for your thoughts

stefaanv
+1  A: 

The digraph and trigraph operators were actually designed more for systems that didn't carry the standard ASCII character set - such as IBM mainframes (which use EBCDIC). In the olden days of mechanical printers, there was this thing called a "48-character print chain" which, as its name implied, only carried 48 characters. A-Z (uppercase), 0-9 and a handful of symbols. Since one of the missing symbols was an underscore (which rendered as a space), this could make working with languages like C and PL/1 a real fun activity (is this 2 words or one word with an underscore???).

Conventional C/C++ is coded with the symbols and not the digraphs. Although I have been known to #define "NOT", since it makes the meaning of a boolean expression more obvious, and it's visually harder to miss than a skinny little "!".

Tim H
+1  A: 

It's nice to use 'em in Eclipse+gcc, as they are highlighted. But then, the code doesn't compile with some compilers :-(

MadH
A: 

I wish I could use || and && in normal speech. People try very hard to misunderstand when I say "and" or "or"...

Alex Feinman
+2  A: 

One problem with using them (for me anyway) is that in MSVC you have to include iso646.h or use the (mostly unusable) /Za switch.

The main problem I have with them is the Catch-22 that they're not commonly used, so they require my brain to actively process the meaning, where the old-fashioned operators are more or less ingrained (kind of like the difference between reading a learned language vs. your native language).

Though I'm sure I'd overcome that issue if their use became more universal. If that happened, then I'd have the problem that some boolean operators have keywords while others don't, so if alternate keywords were used, you might see expressions like:

if ((x not_eq y) and (y == z) or (z <= something)) {...}

when it seems to me they should have alternate tokens for all the (at least comparison) operators:

if ((x not_eq y) and (y eq z) or (z lt_eq something)) {...}

This is because the reason the alternate keywords (and digraphs and trigraphs) were provided was not to make the expressions more readable - it was because historically there have been (and maybe still are) keyboards and/or codepages in some localities that do not have certain punctuation characters. For example, the invariant part of the ISO 646 codepage (surprise) is missing the '|', '^' and '~' characters among others.

Michael Burr