Hacker News new | ask | show | jobs
by enriquto 1077 days ago
> do you think of addition when you see `string + "concatenate"`

Yes. And it tortures me every time.

I religiously avoid string concatenation in Python for this very reason. It's not that "+" necessarily means addition; it's that it always means a commutative operation (to somebody who has learned some algebra). String concatenation is notoriously non-commutative, thus it is extremely disturbing to write it using a visibly commutative operator. Any other operator except "+" would be better. For example, a space, or a product, or a hyphen. Whatever. But please, not a commutative operator. It breaks my brain parser.

2 comments

It's also one of the biggest sources of bugs when dealing with loose types around strings and numbers.

When it comes to languages that let you mix strings and numbers, Lua has it right. + always adds, and accepts numbers and strings that can cleanly convert to numbers. .. always concatenates, and accepts numbers and strings.

This is a really good point. It bothers me too, but I hadn't really articulated why.