Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic vs word operators eg ( '||' vs 'or' and '!' vs 'not')

Tags:

c++

I think or is keyword in c++.

It might be that I've been doing too much python code recently but I find or is more readable than || and xor much more readable than ^.

Is it a good idea to use the word alternatives to the symbolic operators?
Why don't I see them used more?

like image 806
deft_code Avatar asked Jan 12 '11 19:01

deft_code


2 Answers

The unsatisfying answer is that you should use symbolic operators because everyone else does.

An arguably more sensible reason is that they stand out more from the rest of the code.

like image 53
suszterpatt Avatar answered Sep 22 '22 05:09

suszterpatt


Is it a good idea to use the word alternatives to the symbolic operators?

Completely depends on the target audience for your code ­– both people and tools. People can be unused to them, and some tools don't recognize them. (Sometimes those tools use <ciso646> to define them as macros.)

I've started to use "and" and "or" more, especially when switching between C++ and Python, and it has been more readable. The bit of extra consistency between languages matters more than I first thought it would, but more importantly, && and || are control structures and not operators (i.e. short-circuiting), thus making them words differentiates from operators.

(Yes, technically they're operators in C++, but they're more similar to if, else, return, while, and so forth than +, -, *, and other operators. The comma and conditional operators are similarly control structures, and it probably isn't a coincidence they are often found confusing, or at least less readable than separate statements and if/else, respectively.)

However, I very rarely use them in new code written for SO, for example, because I've not yet encountered a question where bringing up this side issue was more important than being readable to SO's C++ audience.

like image 27
Fred Nurk Avatar answered Sep 22 '22 05:09

Fred Nurk