Symbolic dictionary operators, for example ('||' vs 'or' and '!' Vs 'not')

I think or is a keyword in C ++.

Maybe lately I've been doing too much python code, but I think that or more readable than || and xor , much more readable than ^ .

Is it good to use the word alternatives to symbolic operators?
Why can't I see them anymore?

+4
source share
6 answers

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

It completely depends on the target audience of your code - both people and tools. People may not use them, and some tools will not recognize them. (Sometimes these tools use <ciso646> to define them as macros.)

I started using "and" and "or" more, especially when switching between C ++ and Python, and it was more readable. A little extra coherence between languages ​​matters more than I thought at first, but more importantly, && and || control structures, not operators (i.e. short circuit), which makes their words different from operators.

(Yes, technically they are C ++ operators, but they are more like if, else, return, while, etc. than +, -, * and other operators. Commas and conditional operators are similar to control structures, and, this is probably not a coincidence, they are often confused or at least less readable than individual statements, and if / else respectively.)

However, I very rarely use them in new code written for SO, for example, because I have not yet encountered the question of why recreating this problem was more important than reading for an audience of SO C ++.

+4
source

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

Perhaps a more reasonable reason is that they stand out more from the rest of the code.

+7
source

Every C ++ programmer knows about && and || .

Not every C ++ programmer knows that and and or are legitimate alternatives.

For this reason alone, you are better off sticking to what is commonly used.

It’s pretty easy to get used to, so I would say that it’s not very important, and definitely not to confuse the reader with your code.

+3
source

These keywords exist only for terminals that cannot handle special characters | , & etc. Regardless of whether they are more readable code or not, this is possible.

If you know what || , then or not readable than || . And if you know the very basics of C ++, that is, the syntax, then, in my humble opinion, it is no more readable than the other.

In addition, C ++ programmers in most cases use special keyword characters. Therefore, as a rule, it is a good idea not to be an exception in a project if you do not start the project and you set the rules.

+1
source

My first question is: or a bit-wise or | or Boolean shortcut or || ?

I bet there are half a dozen people on my team who should have looked at him.

So I think it's better to stick to a standard agreement,
Because people are used to it. The whole point of programming is not to be ambiguous.

+1
source

|| is how you say "boolean" or "in C ++". If you actually write or , you are going to confuse the readers of your code, even if you can force the compiler to accept it.

I really have sympathy for your argument, deftly. Fair. C ++ - the code is really ugly (and IMHO hard to follow) because of its dependence on linear noise-like symbolism. But this is the philosophy of C ++. If you need good English-language code, C ++ is simply not your language. Try Ada .

I'm serious here. In many ways, Ada is the best language. This is one of them. But if you are going to stick with C ++, you need to fool it. Trying to write Ada (or Pascal) in C ++ is no better than trying to write C ++ or Fortran in Ada.

-2
source

Source: https://habr.com/ru/post/1335292/


All Articles