this post was submitted on 13 Dec 2025
648 points (97.6% liked)

Programmer Humor

28058 readers
945 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] spongebue@lemmy.world 2 points 2 weeks ago (16 children)

x = -i;

Do many languages let you do that? When it's in front of a variable I would've expected it to be a subtraction operator only and you would need to do x = -1 * i;

[–] quilan@lemmy.world 1 points 2 weeks ago (4 children)

But of course -- It's just flipping around the -= operator!

[–] squaresinger@lemmy.world 3 points 1 week ago (2 children)

Nope, it is not.

x = 5
i = 2
x -= i // x => 3

while

x = 5
i = 2
x = -i // x => -2

x=-i is the unary minus operator which negates the value right of it. It doesn't matter if that value is a literal (-3), a variable (-i) or a function (-f()).

x-=i is short for x = x-i, and here it's a binary subtraction, so x is set to the result of i subtracted from x.

[–] quilan@lemmy.world 2 points 1 week ago (1 children)

I need to append /s to my future silly replies I think... that said, I'll never pooh-pooh a well thought response, so thanks for the nice write-up!

[–] squaresinger@lemmy.world 2 points 1 week ago* (last edited 1 week ago)

Thanks, I totally missed your sarcasm :)

There's a couple people in this threat who seem to actually think that x = -i is some weird magic instead of a standard feature that's present in every major programming language.

load more comments (1 replies)
load more comments (12 replies)