views:

115

answers:

5

Traditionally most programming languages have priority of AND higher than priority of OR so that expression "a OR b AND c" is treated as "a OR (b AND c)". Following that idea search engines and query/addressing languages (css,xpath,sql,...) used the same prioritization. Wasn't it mistake ?

When dealing with large enough data, that prioritization is inconvenient because it makes it impossible to create reusable query context without use of parentheses. It is more convenient to create context by using AND and then union results within that context by using OR. It is even more convenient if space is used as AND operator and comma is used as OR operator.

Examples: When searching the internet for airline tickets to bahamas in november or december it would be more convenient to type "airline ticket bahamas november,december" instead of "airline ticket bahamas november", "airline ticket bahamas december" or "airline ticket bahamas (november,december)"

In CSS if we need to set style red of 2 elements, we have to do that: body.app1 div.d1 td.phone span.area, body.app1 div.d1 td.fax span.area{color:red} essentially duplicating prefix body.app1 div.d1 and suffix span.area

If priority of OR was higher than AND we would write this in CSS: body.app1 div.d1 td.phone,td.fax span.area{color:red}

Of course, this idea can be developed into having 2 operators OR one with higher priority than AND and one with lower, for example ',' is higher, ';' is lower, but in many cases languages don't have spare symbols to extend that way and also existing priority of "," where it's used is low.

+8  A: 

I'd rather have consistency everywhere, in code, SQL, search queries, so that I won't have to remember which way it goes in this particular situation.

Developer Art
I agree that consistency has higher priority than many other even good improvements. This is why when they invent new languages or technologies, any mistake becomes permanent.By the way, when programming in SQL did you notice that when you use OR you almost always have to put parentheses, but not when you use AND ?
alpav
+10  A: 

Considering the fact that the background of OR and AND is from mathematical logic where they have well defined precedence, you can not violate that precedence in your design without confusing a VAST majority of the users.

DVK
alpav
There's a major difference between a change in notatio and a change in logic.
DVK
+3  A: 

I think that you are missing the point of the priority of operators. They are there merely as a convenience to the programmer/book writer. The use of the order of operations makes it so that certain clauses can be written without parens, but the use of parens makes sure the reader knows exactly what the code is doing, especially when different languages have different order of operations.

Milhous
+3  A: 

I don't know of any language which does this, but perhaps it should be an error to have AND and OR combined without parentheses. This is a very common cause of stupid errors.

erikkallen
May be those stupid errors are consequence of wrong prioritization.
alpav
A: 

If language creators thought more about "convenience to the programmer", they would make priority of OR above AND. Of course, now it's probably too late to change things because too many people used wrong prioritization for too long and are not willing to learn anything better, but it's not too late to acknowledge that someone made big mistake.

alpav