5 ms·
I completely agree with the assertion and the benefits that ensue, but my attention is always snagged by the nomenclature. I know there are alternate names ava
by anymouse123456 1y ago
I completely agree with the assertion and the benefits that ensue, but my attention is always snagged by the nomenclature.
I know there are alternate names available to us, but even in the context of this very conversation (and headline), the thing is being called a "variable."
What is a "variable" if not something that varies?
- deleted 1y ago[deleted]
- deleted 1y ago[deleted]
- tialaramex 1y agoIn the cases we're interested in here the variable does vary, what it doesn't do is mutate. Suppose I have a function which sums up all the prices of products in a cart, the total so far will frequently mutate, that's fine. In Rust we need to mark this variable "mut" because it will be mutated as each product's price is added. After calculating this total, we also add $10 shipping charge. That's a constant, we're (for this piece of code) always saying $10. That's not a variable it's a constant. In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild. However for each time this function runs we do also need to get the customer ID. The customer ID will vary each time this function runs, as different customers check out their purchases, but it does not mutate during function execution like that total earlier, in Rust these variables don't need an annotation, this is the default. In C you'd ideally want to label these "const" which is the confusing name C gives to immutable variables.
- ajross 1y ago> In the cases we're interested in here the variable does vary, what it doesn't do is mutate. Those are synonyms, and this amounts to a retcon. The computer science term "variable" comes directly from standard mathematical function notation, where a variable reflects a quantity being related by the function to other variables. It absolutely is expected to "change", if not across "time" than across the domain of the function being expressed. Computers are discrete devices and a variable that "varies" across its domain inherently implies that it's going to be computed more than once. The sense Carmack is using, where it is not recomputed and just amounts to a shorthand for a longer expression, is a poor fit. I do think this is sort of a wart in terminology, and the upthread post is basically right that we've been using this wrong for years. If I ever decide to inflict a static language on the masses, the declaration keywords will be "def" (to define a constant expression) and "var" (to define a mutable/variable quantity). Maybe there's value in distinguishing a "var" declaration from a "mut" reference and so maybe those should have separate syntaxes.
- IshKebab 1y agoWell maybe global constants shouldn't be called "variables", but I don't see how your definition excludes local immutable variables from being called "variables". E.g. fn sin(x: f64) -> f64 { let x2 = x / PI; ... Is x2 not variable? It's value varies depending on how I assign x. Anyway this is kind of pointless arguing. We use the word "variable". It's fine.
- zahlman 1y ago> Those are synonyms, and this amounts to a retcon. The point is that it varies between calls to a function, rather than within a call. Consider, for example, a name for a value which is a pure function (in the mathematical sense) of the function's (in the CS sense) inputs.
- ajross 1y agoOr between iterations of the loop scope in which it's defined, const/immutable definitions absolutely change during the execution of a function. I understand the nitpicky argument, I just think it's kinda dumb. It's a transparent attempt to justify jargon that we all know is needlessly confusing.
- tialaramex 1y agoAh! Actually this idea that the immutable variables in a loop "change during execution" is a serious misunderstanding and some languages have tripped themselves up and had to fix it later when they baked this mistake into the language. What's happening is that each iteration of the loop these are new variables but they have the same name, they're not the same variables with a different value. When a language designer assumes that's the same thing the result is confusing for programmers and so it usually ends up requiring a language level fix. e.g. "In C# 5, the loop variable of a foreach will be logically inside the loop"
- ajross 1y agoSeems like you're coming around to my side of the fence that calling these clearly distinct constant expressions "variables" is probably a mistake?
- barisozmen 1y agoEven if the term 'variable' has roots in math where it is acceptable that it might not mutate, I think for clarity, the naming should be different. It's uneasy to think about something that can vary but not mutate. More clear names can be found.
- tredre3 1y ago> In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild. I get that you're not very familiar with C? Because in C we'd use const as well. const int x = 2; x = 3; // error: assignment of read-only variable 'x'
- superblas 1y agoPerhaps they’re conflating how you can’t use “const” as a compile time constant (e.g., you can’t declare the size of an array with a “const” variable). If so, C23 solves this by finally getting the constexpr keyword from c++
- tialaramex 1y agoThat's not a constant, that's an immutable variable which is why your diagnostic said it was read-only. const int x = 2; int *p = &x; *p = 3; // Now x is 3 And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago, I think we can rule out "unfamiliar with C" as a symptom.
- tredre3 1y agoNow x is 3 but you also get a compiler warning telling you not to do that. In my opinion it's a bit disingenuous to argue that it isn't a const just because you can ignore the compiler and shoot yourself in the foot. If you listen to the compiler, it is reflected in the assembly that it is a constant value the same as #define x 2. Is Rust better at enforcing guarantees? Of course. Is `const` in C `const` if you don't ignore compiler warnings and errors? Also of course. > And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago Ditto!
- garethrowlands 1y agoThe term 'variable' is from mathematics. As others have said, the values of variables do vary but they do not mutate.
- astrobe_ 1y agoYes, and math has the notion of "free variable" and "bound variable" [1]. [1] https://en.wikipedia.org/wiki/Free_variables_and_bound_variables https://en.wikipedia.org/wiki/Free_variables_and_bound_varia...
- 1-more 1y agoThey are often called bindings, not variables, so as to make it clear that they are just a name for a thing that will not change.
- throwaway_7274 1y agoRight, yeah, it’s a funny piece of terminology! The sense in which a ‘variable’ ‘varies’ isn’t that its value changes in time, but that its value is context-dependent. This is the same sense of the word as used in math!
- jayd16 1y agoA common naming is value. You can call them immutable values and mutable variables. Another way to look at it is a variables are separate from compile time constants whether you mutate them or not.
- nayuki 1y ago> What is a "variable" if not something that varies? If I define `function f(x) { ... }`, even if I don't reassign x within the function, the function can get called with different argument values. So from the function's perspective, x takes on different values across different calls/invocations/instances.
- didibus 1y agoThat's why in some languages they don't call them variables, but bindings instead. (let [a 10] a) Let the symbol `a` be bound to the value `10` in the enclosing scope.
- Warwolt 1y agoIt's a variable simply because it doesn't refer to a specific object, but any object assigned to it as either function argument or by result of a computation. It's in fact us programmers who are the odd ones out compared to how the word variable has been used by mathematics and logicians for a long time
- usrusr 1y agoSome languages like Kotlin have var and val introducing the distinction between variables (that are expected to get reassigned, to vary over time, and values, which are just that, a value that has been given a name. I like these small improvements. (unfortunately, Kotlin then goes on and introduces "val get()" in interfaces, overloading the val term with the semantics of "read only, but may very well change between reads, perhaps you could even change it yourself through some channel other than simple assignment which is a definite no")
- hannasm 1y agoYou could always interpret a variable from the perspective of it's memory address. It is clearly variable in the sense that it can and will change between allocations of that address, however an immutable variable is intended to remain constant as long as the current allocation of it remains.
- layer8 1y agoVariables are called variables because their values can vary between one execution of the code and the next. This is no different for immutable variables. A non-variable, aka a constant, would be something that has the same value in all executions. Example: function circumference(radius) return 2 * PI * radius Here PI is a constant, while radius is a variable. This is independent of whether radius is immutable or not. It doesn’t have to be a function parameter. If you read external input into a variable, or assign to it the result of calling a non-pure function, or of calling even a pure function but passing non-constant expressions as arguments to it, then the resulting value will in general also vary between executions of that code. Note how the term “variable” is used for placeholders in mathematical formulas, despite no mutability going on there. Computer science adopted that term from math. https://en.wikipedia.org/wiki/Variable_(mathematics) https://en.wikipedia.org/wiki/Variable_(mathematics)
- ychen306 1y agoI try to avoid this ambiguity by calling such variables "values".
- ordu 1y agoIt leads to a further ambiguity, because "value" is something that is assigned to a variable (or whatever we call it). For example some Rust code: let v1 = Vec::new(); let v2 = v1; The first line creates value of type Vec and places it into variable v1. The second line moves the value from variable v1 to variable v2. If we rename "variable" to "value" then my description of the code will become unreadable. If I was as pedantic as the OP, I'd use "lexical binding" instead of "variable". But I'm not sure how it will work with C and C++, because their semantics assumes that a variable has a memory associated with it that can hold a value of a given type. Modern compilers are smarter than that, but still they try hard to preserve the original semantics. The variable in C/C++ is not just a name that ceases to exist after compiler have done its work. It creates a possibility that calling C/C++ variables "lexical bindings" we'll get more pedants accusing us of improper use of words, even if we never change values of those variables.
- MetaWhirledPeas 1y ago> What is a "variable" if not something that varies? Really it's a constant. But they are referenced like variables, so people just get a little lazy (or indifferent) talking about it.