1081
you are viewing a single comment's thread
view the rest of the comments
[-] SquishyPandaDev@yiffit.net 47 points 1 year ago

Obligatory, mutable global variables are evil.

[-] magic_lobster_party@kbin.social 32 points 1 year ago

The definition of a variable is that it’s mutable. If it’s immutable it’s constant.

[-] marcos@lemmy.world 15 points 1 year ago

There's no ISO standardized definition for variable. People use that word with all kinds of meaning.

[-] drcouzelis@lemmy.zip 18 points 1 year ago

Waaaait a minute... isn't it called a variable because the contents are, you know, variable?

[-] BassTurd@lemmy.world 4 points 1 year ago

It started as a variable, then ended as a constant.

[-] Yen@feddit.uk 11 points 1 year ago

This is needlessly obtuse. The definition of the word is that it's non-constant. There isn't an ISO definition of the word no, but there are many reputable dictionaries out there that will serve as an alternative.

[-] marcos@lemmy.world 3 points 1 year ago

Well, starting with the definition from algebra, where it's not something allowed to vary...

I guess more people know about math than use imperative programing languages.

[-] SkyeStarfall@lemmy.blahaj.zone 2 points 1 year ago* (last edited 1 year ago)

Except that's exactly what it is allowed to in algebra.

Sure, in most equations you solve in early algebra school there is only one possible value for the variables. But in many equations there can be multiple, or even infinite. It's an unknown, and the contents can vary (depending on other constraints, ie. The rest of the equation(s)).

[-] marcos@lemmy.world 1 points 1 year ago

There's no time in algebra for your variables to vary.

When you have a non-unitary set of solutions, you have a constant non-unitary set of solutions.

[-] Walnut356@programming.dev 5 points 1 year ago* (last edited 1 year ago)

I feel like it's like pointers.

"Variable" refers to the label, i.e. a box that can contain anything (like *ptr is a pointer to [something we dont know anything about])

Immutable describes the contents, i.e. the stuff in the box cant change. (like int* ptr describes that the pointer points to an int)

Rust makes it very obvious that there's a difference between constants and immutable variables, mainly because constants must be compile time constants.

What do you call it when a variable cant change after its definition, but isnt guaranteed to be the same on each function call? (E.g. x is an array that's passed in, and we're just checking if element y exists)

It's not a constant, the contents of that label are "changing", but the label's contents cant be modified inside the scope of that function. So it's a variable, but immutable.

[-] QuazarOmega@lemy.lol 10 points 1 year ago

As opposed to immutable variables

*confused screaming*

[-] yiliu@informis.land 5 points 1 year ago
[-] Eufalconimorph@discuss.tchncs.de 3 points 1 year ago* (last edited 1 year ago)
int const golden = 1.618;
int* non_constant = (int*)&golden;
golden = 1.61803399;

Casts are totally not a danger that should require a comment explaining safety…

And more generally mutable aliasing references of any sort are evil. Doesn't mean they're not useful, just that you need magic protection spells (mutexes, semaphores, fancy lock-free algorithms, atomics, etc) to use them safely. Skip the spell or use she wrong one, and the demon escapes and destroys all you hold dear.

this post was submitted on 08 Oct 2023
1081 points (96.6% liked)

Programmer Humor

19572 readers
1031 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 1 year ago
MODERATORS