←back to thread

498 points azhenley | 1 comments | | HN request time: 0.207s | source
Show context
anymouse123456 ◴[] No.45771965[source]
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?

replies(14): >>45772017 #>>45772042 #>>45772062 #>>45772077 #>>45772262 #>>45772625 #>>45773368 #>>45773945 #>>45774039 #>>45775594 #>>45775669 #>>45775698 #>>45775911 #>>45776839 #
tialaramex ◴[] No.45772062[source]
In 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.

replies(3): >>45772342 #>>45773930 #>>45776499 #
ajross ◴[] No.45772342[source]
> 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.

replies(2): >>45772976 #>>45773131 #
1. IshKebab ◴[] No.45772976[source]
Well 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.