Spot any errors? let me know, but Unleash your pedant politely please.

Saturday, 21 February 2009

'==' = '='; '=' = ':='.

Or: One of the things I hate about C

If you started out with a language that uses this syntax, you probably think I'm nuts. Well, you're wrong. Just because you're used to the convention doesn't mean the convention makes any fucking sense.

Q. What is "=" ?

It's a equals sign, right ? Remember it from mathematics ? It's a sort of point of. Everything on the left amounts to the same as everything on the right. So in a condition, we're testing whether two expressions equal, whether they balance. So for reasons I simply cannot fathom, C-like languages use "==".

I think it's like this simply because whoever first defined the syntax didn't want to refactor the work he'd already done. Or it didn't occur to them, because they had Aspergers Syndrome or something. One of the first things you want to do when defining a language, after you've declared a variable, is assign something to it. This probably comes before testing for equality. There's not much point testing for equality until you've got things to test. So the language developer thinks to himself : "I've allocated a variable to be used. How shall I assign something to my variable ?", and he comes up with this…
x = 10
…and there's nothing wrong with this. Yet.

A bit later, he thinks to himself : "I need to check the content of my variables", and he comes up with this…
if x = 10
…and there's a problem because '=' has already been used. The next question he asks himself is "What can I use instead ?", but this is a mistake. the question should be "Should I use '=' for assignment or equality ?", and the right answer is "Equality".

BASIC uses '=' for both. Assignment should be preceded by the 'Let' keyword, but because assignment and equality can be contextually detected by the interpreter, 'Let' is optional these days.

Algol-like languages use '=' correctly and have ":=" for assignment. This is what I prefer, but anything except '=' would be fine. Just a ':' would probably do (but that might be better used elsewhere!).

No comments:

Post a Comment