quinta-feira, 26 de julho de 2007

Caché Operators

It's been a while since I found out something peculiar about Caché's language, Caché Object Script. It is not really a bug, since it's registered at the documentation, but I don't think it's very natural or straightforward.

If you remember your mathematic class when you were a child, you might give the correct answer for this expression: 1 + 2 * 3 (* means multiplication, if you don't already know =P). If your answer is 9, you probably skip some classes, since the correct answer is 7. That's because in math, multiplication operator has precedence over addition and subtraction operators (see this link). It means that if you don't put parentheses (which is the "right way" to express the order of operations, if you want), you must do multiplication before addition.

Well, most of programming languages I know, consider mathematics (what is absolutely normal, since science computer was born from maths, together with eletric engineerings).

Examples:
Oracle database, I can do: SELECT 1 + 2 * 3 FROM DUAL, and the answer will be 7. So Java, C/C++, Delphi/Pascal, gives us the correct answer, 7.

But with Caché Object Script, the answer is 9. Try open a terminal, and execute the following: WRITE 1 + 2 * 3, the answer will be 9.

This happens because this language doesn't follow mathematics pattern, neither most of language's. Fine, in fact, it's a matter of implementation, and it's in documentation, but I can't stop looking it badly. Even worse when I am used to the rest of world's convention, used to "right" operators precedence. Oh well, the language can choose if it will follow or not, any convention. But if you do not, especially math's conventions, you might cause some errors.

I just payed attention to this, because one loop of mine wasn't working. Being familiar with other programming languagens, I didn't think a simple expression could be wrong (the max boundary of my loop was the result of a expression like I showed). After check the rest of algorith twice, I was just blessed by a hunch, and after some tests and debugging, I realized that I should have put some parentheses in the expression. After doing that, I checked the documentation and it was really there: operators precedence is strictly left to right, when no parentheses are found.

So, I just start wondering why those language designers made it, why they made this choice? Did they want to be 'cool', not just like everyone else, or maybe were they rebels with or without a cause?

I believe it was not the case. I think it was just because the implementation of script's execution, which is easier to do when you don't consider math operators precedence. Laziness. Less effort law, the law that really moves this world, despite Newton's gravity =P.

Even being a script language is not really an excuse. Javascript is also an script. But it goes with the rest of the world. Just try document.write(1 + 2 * 3) and you'll get a nice 7.

u_u

0 comentários: