Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

201–210 of 367 posts

Re: Why Does “=” Mean Assignment?

#201

Earlier quoted context omitted.

Common in Pre-Python pseudo code and algorithms.

Are you making an argument that Python has caused us all to start writing pseudo-code using `=` for assignment?

I think he's making the argument that when we write "pseudo-code" these days we're just writing python

Re: Why Does “=” Mean Assignment?

#202
Another question is why the assignment is from right to left when the natural direction would rather be left to right, like 2+2 => x to store the value 4 in x.

I was told once by a math professor that it is a habit inherited because we use Arabic numbers/maths which were really meant to be read from right to left. Don’t know if the theory has any merit.

Re: Why Does “=” Mean Assignment?

#203
post #23

Is it really worth allowing assignment in places so weird that you also need ==? Wouldn't it be easier to use = for everything and get rid of those weird edge cases?

It is extremely useful for all statements to be able to be used as expressions, to chain and nest values. It's purely syntactic clumsiness on the part of those language families that assignment and comparison are so close to the notion of "equals".

Re: Why Does “=” Mean Assignment?

#204

I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"

This is why I'm a big fan of yoda notation where applicable

if (2 == x)

Re: Why Does “=” Mean Assignment?

#205
post #164
post #161

= has contextual meaning in math, why not programming? It's not like we can fool ourselves into thinking that tests for equality don't also have wildly different meaning depending on context. No operator has a universal meaning, nor should they.

Can you provide an example where equals sign is not equality I'm curious.

When you aren't strictly referring to an equation.

Re: Why Does “=” Mean Assignment?

#206

My first programming teacher insisted we call the single = sign the 'gets' operator. So int x = 10 int y = x would read in English as x gets 10, y gets x. Shortly after that class I took a break from any coding. When I went to another school I saw Softmore and Junior level programmers still struggling with this. I retained the habit of using 'gets'and never had a problem with this.

Something I've noticed is that I don't tend to subvocalise when reading code, but if I do, I generally read "x = 10" as "x e. 10" rather than "x equals 10" (so basically just the first syllable). I will read "if x = 10" as "if x equals 10".

Not sure why/how I picked it up, but similar principle, I suppose.

Re: Why Does “=” Mean Assignment?

#207
post #133

most of this article is covered in the Wikipedia page: https://en.wikipedia.org/wiki/Relational_operator#Confusion_... but Wikipedia says: "The reason for all this being unknown. [footnote] Although Dennis Ritchie has suggested that this may have had to do with "economy of typing" as updates of variables may be more frequent than comparisons in certain types of programs" while the OP says: "As Thompson put it: Since…

Economy of typing definitely shouldn't be the #1 concern when designing a language. Look at APL.

Re: Why Does “=” Mean Assignment?

#209

Earlier quoted context omitted.

It's better to use -Wall -Werror options for C compiler.

That won't always catch the error and even if it did, why is it better?

-Wall can almost always catch the problem as it will insist on redundant parens surrounding an assignment. So you can only screw it up if you typed extra parens for an equality expression.

Variable on left is conventional, it's how people sound it out in their head, so it's just easier to process mentally when the expression becomes more complex.

Yoda syntax catches that single error at the expense of making all equality expressions harder to write, read and maintain.

And, besides, if you're going to use Yoda syntax, you're going to want your linter to enforce it... at which point you may as well turn on -Wall anyway.

Re: Why Does “=” Mean Assignment?

#210
post #164
post #161

= has contextual meaning in math, why not programming? It's not like we can fool ourselves into thinking that tests for equality don't also have wildly different meaning depending on context. No operator has a universal meaning, nor should they.

Can you provide an example where equals sign is not equality I'm curious.

Most equivalence relations eventually drop any special notation in favor of =

In general, = is not rigorous. It's used as a stand-in for the word "is".

Another example is when someone writes x = 1, ..., N, meant to imply x is changing, not that x is equal to a tuple. Also the index in summation notation, which really is an assignment as in programming. I could go on.

Post reply on HN