Why is 'Character#' not a valid mathlab variable name?

Answers (3)

From the documentation:
"A valid variable name starts with a letter, followed by letters, digits, or underscores."

2 Comments

But that doesn't explain why ... ;-)
According to inside sources, it's because a very tall hamster wanted it that way.

Sign in to comment.

Walter Roberson
Walter Roberson on 17 Feb 2023
Edited: Walter Roberson on 18 Feb 2023
I looked up several different lists of the most popular programming languages, and checked reference documentation for each .
# is not a valid character in variable names in:
  • Ada 95 (but might be possible in Ada 2003)
  • Algol
  • C
  • C++
  • C#
  • fortran
  • Go
  • HCL (permits $ but not as first character)
  • java
  • javascript
  • Kotlin
  • Lua
  • Makefile (‘:’, ‘#’, ‘=’, and whitespace are specifically prohibitted in variable names)
  • pascal
  • php
  • python
  • R (permits periods in identifiers)
  • Ruby
  • Shell
  • Swift (permits $ and a number of Unicode characters in variable names, but not # )
  • typescript (permits $ including as first character)
Ones that do permit #:
  • Ada 2003 (permits many unicode characters)
  • Common Lisp (requires special syntax) (permits many unicode characters)
  • Maple (requires special syntax) (permits many unicode characters)
  • Mathematica (permits many unicode characters)
  • Rust (permits many unicode characters)
it is difficult to answer exactly "why" any of these languages made the choices they did.
I suspect in the case of MATLAB it is because the permitted identifiers are the same as the identifier forms permitted by the linkers on the operating systems of implementation. (In some cases the $ is also permitted by the linkers, but it tends to have special meaning when it is permitted.)
MATLAB itself can tell you. Just ask.
why
The terrified and bald and not excessively rich hamster suggested it.
why
Because he wanted it that way.
why
How should I know?
why
Some bald bald and young and not excessively good and young and young and very terrified system manager helped some good and not very bald and not very good mathematician.
why
The smart system manager helped some good and rich and very terrified tall system manager.
Do you want a better reason? Just because. Seriously, does there need to be a better reason? Sometimes you just need to accept something as it is.
You should understand that special characters in names often may create subtle problems. For example, why would MATLAB not allow you to start a variable name with a number? That is, why can x5 be a variable name, but not 5x? This may get into parser issues, where by removing the possibility of a number at the beginning of the name, it made the parser code far easier to write. Eliminating that possibility reduces the chances for bugs in your own code too.
But special characters often have specific uses. For example, the @ symbol has specific uses in MATLAB. They could not allow you to use it. As well symbols like ~ also have their own uses. And who knows when next year, they might find a real need for some other special character as a new operator of some sort? This means the language would need to exclude certain specific special characters from use in variable names. So instead of giving the user a list of which special chacracters you could use, it is far easier and better to just declare ALL special characters as illegal in a variable name. That makes the rules for a variable name quite simple. And in turn, that is a good thing.
By the way, it is MATLAB, not mathlab.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 17 Feb 2023

Edited:

DGM
on 20 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!