-6

Now that we have const implemented javascript we can use it to declare variables. But unlike let or var it is 5-character long and I believe it is a big deal actually. Something that even slightly takes longer to type (and read) affects our coding experience.

My question would be - say, one is trying to come up with some completely new programming language (so, no backwards compatilibity burden) and you still wan't save let for mutable variables, what shorter keyword will you use for consts?

UPD: an answer "I would still go with const because..." is a perfectly valid answer as well.

UPD: people, come on. The very existence of const (not constant) as a keyword is an evidence of that length really matters. The very existence of var and val in Scala is an evidence of trying to rethink what keywords for what should be used.

  • 3
    5 letters or not, const is used in a lot of other languages, so I think I'd still push for going with that. Most people new to the language would see const and know right away what it meant. Same goes for final. – neilsimp1 Jun 8 '17 at 11:47
  • I would think using 'const' for a variable would be a bigger deal... – mickeyf_supports_Monica Jun 8 '17 at 11:49
  • 2
    @shabunc How about this for an alternative. In this new language, all variables are const by default. You need to declare a variable that can be reassigned. If immutability is an important aspect of your language, say if it were a functional language of some sort, then there is absolutely no better way to make const shorter than to require the programmer to type 0 characters, would you agree? – Neil Jun 8 '17 at 12:03
  • 2
    I'm curious as to why 2 more keystrokes is "a big deal". You read code far more than you write it. I'd rather have something that's clearer to read than slightly easier to write. – Becuzz Jun 8 '17 at 12:16
  • 1
    If const vs var seems such a bother, I shudder to see your reaction to a type name called TRS_DynamicallyTypedLowFidelityLeafComponentRepresentationTranslator -- something that we have used at my work. – R Sahu Jun 9 '17 at 20:37
7

Proposing my suggestion in the comments section, consider the possibility of making all variables const by default. Variables which need to be reassigned must be purposefully declared as such, which if immutability is an important aspect of your language (for instance as in a functional language), then entirely avoiding the necessity to declare a variable const is as short as you can get, no?

Responding to your follow-up question, is it okay to declare local variables implicitly without a keyword? I suppose it entirely depends on your language. If there are no types, then you are not strictly required to declare variables. Your compiler or parser simply creates new variables as it finds them. Though if all variables are const by default, this would also entail that reassigning a variable that already exists, unless it is otherwise declared as non-const, would result in an error of some sort.

Does that answer your question?

  • 1
    let's wait for another answers coming but this is definitely an answer worth to have, thank you for posting this. – shabunc Jun 8 '17 at 12:14
  • @shabunc Happy to help. – Neil Jun 8 '17 at 12:14
5

Kotlin and Scala both use the val keyword. This leaves you with var for variables, and val for values.

The downside here I suppose is that var and val look extremely similar so it's harder to tell them apart when scanning code.

If you were to design a new language, using let and val could alleviate this. This might be a bit weird as let and val are kind of unrelated abstractions, but from a practical point of view this would solve both your problem and the scanning problem that var/val has.

2

You could invent a new short keyword e.g. set or def or use a different approach.

Like in Ruby where constants are all variables starting with a capital letter or Pascal where constants are assigned with = and variables with := (e.g. PI = 3.141592 but a := b + c)

2

I would still rather go with const honestly. I think it's wonderful from a readability perspective. It's different enough from let or var (if we use the JavaScript example) to show "Oh hey this is a special kind of variable". As well as that, it expresses well the idea that this won't change, it's "constant".

But hey, why not indulge the idea? I'd probably go with something like fix (for fixed obviously) or con (for consistent).

0

I think it is a silly idea and none of your points make sense but I like the linguistic challenge. You want three letters that say constant... How about fix or pin ?

  • fix(ed) and pin(ned) are often used as keywords that "pin" a variables location in memory. (Garbage Collected languages can move things on you without these keywords.) That could be confusing. – RubberDuck Jun 10 '17 at 1:16
  • @RubberdDuck I do not see that as a big issue because garbage collection is a different domain/context. Pin also means something different in bowling and fix is a repair but this is hardly confusing. Every keyword is a metaphore and memory management is meaningless in a script language. – Martin Maat Jun 10 '17 at 5:37
0

Something that even slightly takes longer to type (and read) affects our coding experience.

They call them programming languages for a reason. Programming languages are meant to be read and understood by humans. If that wasn't important, we'd all just be writing absolute machine code.

If you go down the path of defining your own idiom for doing things that everybody else does differently, then it'll be harder for them to understand your code. That won't be a problem...

...Until you want to collaborate with somebody. When that day comes, you can try to win them over to your side, which is where new programming idioms and new programming languages come from; or you can go with the flow, and focus all of your energy on solving the problem at hand, which is where short-term financial gain usually comes from.

The choice is yours.

Not the answer you're looking for? Browse other questions tagged or ask your own question.