Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable Names
#1
Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok.
Reply
#2
(11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok.

https://qb64phoenix.com/qb64wiki/index.php/Variable

Guess it's for historic reasons aka QB4.5 compatiblity.
Reply
#3
(11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok.

Not that I know a heck of a whole lot about interpreters/compilers parsing and tokenising (?) a program, but I imagine there's value in the process knowing immediately that:
  • it is dealing with a literal number (because of a digit)
  • versus it is dealing with a literal string (because of a double-quote)
  • versus it is dealing with an identifier (variable, constant, function ... because of an alphabetic character)

I think.
Reply
#4
It would make parsers explode.
Reply
#5
It's a text parsing problem.
Write your own compiler and see how difficult it becomes when what appears to be numbers at first are no longer numbers because it has some other non-number in it.

Especially problematic for a language like BASIC where a typo can bring another variable into existence to cause a difficult to track down bug.
Reply
#6
If I had to attribute the choice to anything, it'd probably be simply to the way that VAL behaves.

VAL("123abc") = 123
VAL("abc123") = 0

VAL takes all the numbers from the start and then assembles them into a valid numeric value, stopping whenever an unexpected character is found.

If I have to venture a guess, I'd say the original creators of BASIC used some logic, based around this concept, to parse what makes a number and what could represent a keyword or variable.  Since we tend to try and follow those some historical rules as what was set by QB45 and the folks previously, we don't tend to allow numbers before letters in variables either.

Which reminds me of this old article of interesting fact:

Quote:The Space Shuttle and the Horse's Rear End


Say friend, did you know that the US Standard railroad gauge (distance between the rails) is 4 feet, 8 1/2 inches.

That's an exceedingly odd number. Why was that gauge used?

Because that's the way they built them in England, and the US railroads were built by English expatriates.

I see, but why did the English build them like that?

Because the first railway lines were built by the same people who built the pre-railroad tramways, and that's the gauge they used.

Well, why did they use that gauge in England?

Because the people who built the tramways used the same jigs and tools that they used for building wagons, which used that wheel spacing.

Okay! Why did their wagons use that odd wheel spacing?

Because, if they tried to use any other spacing the wagon wheels would break on some of the old, long distance roads. Because that's the spacing of the old wheel ruts.

So who built these old rutted roads?

The first long distance roads in Europe were built by Imperial Rome for the benefit of their legions. The Roman roads have been used ever since.

And the ruts?

The original ruts, which everyone else had to match for fear of destroying their wagons, were first made by the wheels of Roman war chariots. Since the chariots were made for or by Imperial Rome they were all alike in the matter of wheel spacing.

Thus, we have the answer to the original question. The United States standard railroad gauge of 4 feet, 8 1/2 inches derives from the original specification for an Imperial Roman army war chariot.

And the motto of the story is Specifications and bureaucracies live forever.

So, the next time you are handed a specification and wonder what horse's ass came up with it, you may be exactly right. Because the Imperial Roman chariots were made to be just wide enough to accommodate the back-ends of two war-horses.

So, just what does this have to do with the exploration of space?

Well, there's an interesting extension of the story about railroad gauge and horses' behinds. When we see a Space Shuttle sitting on the launch pad, there are two big booster rockets attached to the sides of the main fuel tank. These are the solid rocket boosters, or SRBs. The SRBs are made by Thiokol at a factory in Utah. The engineers who designed the SRBs might have preferred to make them a bit fatter, but the SRBs had to be shipped by train from the factory to the launch site.

The railroad from the factory runs through a tunnel in the mountains. The SRBs had to fit through that tunnel. The tunnel is slightly wider than a railroad track, and the railroad track is about as wide as two horses' behinds.

So a major design feature of what is arguably the world's most advanced transportation system was originally determined by the width of a horse's ass.
Reply
#7
Original Basic used single letters for variables and tended to that to save precious Ram space.

That's my theory of where it all started, no cavemen involved sadly.
b = b + ...
Reply
#8
To be able to use a variable name like "1a" we would all have to use "LET" again. Obligatorily.

It's simply because the earliest versions of BASIC supported line numbers! Yes remember that? Couldn't have a program in GW-BASIC for example without line numbers. And in an 8-bit computer like my Radio Shack TRS-80 Color Computer 2, "1a=3" would be broken down to:

Code: (Select All)
1 a = 3

The earliest BASIC's on 8-bit computers allowed almost all spaces omitted when composing programs because a space actually took up room in memory, and back then those computers had to work with 16KB or less RAM.

Oh well C and Lua and many other programming languages also don't permit numerals to begin a variable name, for other reasons.

Dimster why do you want to have a variable like "1a" anyway?
Reply
#9
you could use Lisp Big Grin
(setq 1a 123)
123
(expt 1a 20)
628206215175202159781085149496179361969201
Reply
#10
To pile on to all the stuff others have already pointed out ( Tongue ), there are numeric literals that include alphanumeric characters, so allowing variables to start with numerals create pretty bad ambiguities even in QB45/QB64 where spaces are required around most things:

Code: (Select All)
Dim 20D As Long
20D = 50
A = 20D ' Is A set to 20 or 50?

Of course, this could be fixed by simply not allowing you to use variable names that could be confused with a numeric literal, but:

1. That's a lot of extra work for little gain, simply banning variables from starting with a numeric is much simpler.
2. It would prevent us from adding any new numeric syntax's in the future if they would conflict with allowed variable names.

There's probably some other conflicts that I haven't considered, but that's the most obvious to me.
Reply




Users browsing this thread: 7 Guest(s)