Author Topic: What's your philosophy about constants?  (Read 13757 times)

0 Members and 1 Guest are viewing this topic.

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
What's your philosophy about constants?
« on: March 23, 2022, 05:59:26 pm »
So putting on your QB64 hat and/or a "BASIC in General" hat ...

I wasn't happy with wwwBASIC's implementation of constants, very early stages, direction I wasn't crazy for (search for third occurrence of"'const" on that page if you dare), so I decided to rejig things in BASIC Anywhere Machine's fork of wwwBASIC.

Without planning on it, I wound up with the CONST statement working exactly like a "FN-less" and parameter-less version of DEF FN.

So now the following code runs A-1 in my dev version (EDIT: now in my online version) of BASIC Anywhere Machine:

Code: QB64: [Select]
  1. dim currMiles#
  2. const currKilometers# = currMiles# * 1.60934
  3. '-------------------------------------------
  4.         cls
  5.         input "enter number of miles: "; currMiles#
  6.         print str$(currMiles#) + " miles, that's: " + str$( currKilometers# ) + " kilometers."
  7.         print : print "press a key to start again" : while inkey$ = "" : wend
  8.  

Although totally unorthodox for BASIC, there's something I semantically really like about it.  A special soupçon of je-ne-sais-quoi.

It has me wondering, though, what dp die-hard BASIC folk think of that?  Does it just annoy you?  Or give you the creeps?  Or is this a total 5-alarm fire ?

And how would you do this in an orthodox QB64 (or general/traditional BASIC) way ?
« Last Edit: March 23, 2022, 06:26:07 pm by CharlieJV »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: What's your philosophy about constants?
« Reply #1 on: March 23, 2022, 06:14:18 pm »
Code: QB64: [Select]
  1. DIM currMiles#
  2. CONST ConvertMilesToKilometers# = 1.60934
  3. '-------------------------------------------
  4.     CLS
  5.     INPUT "enter number of miles: "; currMiles#
  6.     PRINT STR$(currMiles#) + " miles, that's: " + STR$(currMiles# * ConvertMilesToKilometers#) + " kilometers."
  7.     PRINT: PRINT "press a key to start again": WHILE INKEY$ = "": WEND

I miss DEF FN, too, but by definition, CONSTANT means a variable that never changes. That is to say the compiler sets aside less space, because it is a fixed value.

I had a discussion in antoehr thread about this, let's see...

"I hardly ever use constants. From the old days, it was a way to save that coveted stack space. This is because a constant is compiled with less allocated space, since the compiler knows it won't be changing in value. Those days are behind us, so this type of optimization is now more of a coding preference than a necessity for large applications. Readability is somewhat an advantage to constants. The programmer, or someone else studying the code, is informed of the variables which are fixed. No need to track them when debugging. For coders who have a hard time keeping things straight, constants prevent the mistake of taking a non declared variable, meant to be a constant, and mistakenly assigning it a new value, someplace in the code. That's called OOPs programming!"

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #2 on: March 23, 2022, 06:32:34 pm »
(SNIP!)

I guess I'm seeing currKilometers# as being (tongue in cheek) CONS(IS)TANT(LY), or CONSTANTLY matching currMiles# ...

Groan ...

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: What's your philosophy about constants?
« Reply #3 on: March 23, 2022, 06:33:00 pm »
my ted talk on const, 2022 edition:
const is completely redundant to ordinary variables, and its implemented more stupidly. so don't use it.
no, i mean it: const is nothing but a brain diaper. if you aren't sure whether you're editing your variables, you should go use an easy language like QBAS--- wait. step away from the computer instead.
... and that's what i say!

my credentials for having this opinion:
i do motivational talks for battered women and entertainment at youth mitzvah parties

You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: What's your philosophy about constants?
« Reply #4 on: March 23, 2022, 06:44:28 pm »
I use constants they are very handy, change one value in code and throughout your program the values are updated.

Only a fool would argue against such a convenience.

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #5 on: March 23, 2022, 06:47:13 pm »
(SNIP!)
const is completely redundant to ordinary variables, and its implemented more stupidly. so don't use it.
no, i mean it: const is nothing but a brain diaper.
(SNIP!)

I've got a couple of cognitive disorders going on, so I rely heavily on well-organized (EDIT: "well" is definitely in the eyes of the beholder, so let's say "organized to the gills in a way that fits the particular brain"), with very visible yet non-distracting visual cues to understand and remember.

Just saying I need that brain diaper because of cognitive incontinence.

How fortunate are those without dependence on a brain diaper?  (Yeah, a little envious am I ...)

Self-deprecating humour aside, them are great points.
« Last Edit: March 23, 2022, 07:14:39 pm by CharlieJV »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: What's your philosophy about constants?
« Reply #6 on: March 23, 2022, 06:51:56 pm »
@bplus That's the most common use for "constants" in a program, but you don't actually need the CONST to do it...

pi = 3.14159
' blah, blah, blah
PRINT 27 * pi
' blah, blah, blah
PRINT pi / 2
' etc, etc., etc.

So just change the variable pi at the top, to any number of acceptable digits, and it works just the same as CONST, except it won't throw a compiler error if you mistakenly assign pi to a different number someplace else in the program.

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: What's your philosophy about constants?
« Reply #7 on: March 23, 2022, 07:01:24 pm »
Which is easier to type:

Const pi = 3.1415

or

Dim Shared pi as some type
pi = 3.1415

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #8 on: March 23, 2022, 07:03:54 pm »
I use constants they are very handy, change one value in code and throughout your program the values are updated.

Only a fool would argue against such a convenience.

Ditto.  (Except fool.  Anybody's fool might just be my Huckleberry for one thing or another...)

Although that can just as easily be done with a variable, constant has a certain cognitive soupçon of je-ne-sais-quoi along with a "define me and I will protect thee from thyself and any muck-ery which might be afoot down the road" safeguard.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: What's your philosophy about constants?
« Reply #9 on: March 23, 2022, 07:05:26 pm »
Quote
"define me and I will protect thee from thyself and any muck-ery which might be afoot down the road"

LOL yes!

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #10 on: March 23, 2022, 07:11:33 pm »
@bplus That's the most common use for "constants" in a program, but you don't actually need the CONST to do it...

pi = 3.14159
' blah, blah, blah
PRINT 27 * pi
' blah, blah, blah
PRINT pi / 2
' etc, etc., etc.

So just change the variable pi at the top, to any number of acceptable digits, and it works just the same as CONST, except it won't throw a compiler error if you mistakenly assign pi to a different number someplace else in the program.

Pete

In a huge program with a lot of stuff going on and risk of typos, I could imagine defining something as a constant could help to easily catch a mistake when at some point assigning a new value to something that was meant to stay a constant.

Danged if I can think of a good example off the top of my head...

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: What's your philosophy about constants?
« Reply #11 on: March 23, 2022, 08:07:35 pm »
In a huge program with a lot of stuff going on and risk of typos, I could imagine defining something as a constant could help to easily catch a mistake when at some point assigning a new value to something that was meant to stay a constant.

Danged if I can think of a good example off the top of my head...

A good example would be:

CONST True = -1

Now, instead of typing IF x = -1 THEN....  you can type IF x = True THEN.... 

This helps catch basic typos such as typing IF x = 1 THEN... (you forgot the minus there, or the keypress didn't register from too much Cheetos-dusting...)  Especially in conjunction with OPTION _EXPLICIT, you'd get a notification when you typed IF x = Tru THEN...

Names are more readable and have the added security of enhanced syntax checking.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: What's your philosophy about constants?
« Reply #12 on: March 23, 2022, 10:40:39 pm »
I've used them on occasion, mostly for conversion factor stuff, but generally don't see the point. Use a shared variable that you don't change and it's the same thing as far as I can tell.

Offline tomxp411

  • Newbie
  • Posts: 28
    • View Profile
Re: What's your philosophy about constants?
« Reply #13 on: March 24, 2022, 02:04:10 am »
The whole point of a constant is that it can't be modified. The other point is that it's a literal replacement made at compile time.

Let's look at one of list of constants built in to the DotNet SDK, the keyboard scan codes. There are 773 keyboard scan codes in the System.Windows.Forms.Keys enumeration - just for a list of keys on the computer.

Now consider all of the other thousands of enumerations and constants in the system... the "Magic Number Database" has over 380,000. That's not all of them, either. So if you were to include all of the constants your program might need as variables every time you compile it, you would be adding in over 1.5 megabytes just for the variable storage, not to mention the other things that go into storing and retrieving a variable.

So there are two very important reasons to use CONST in your program:
1. It's an obvious semantic flag that says "this value isn't going to change."
2. Those values get converted directly to data during compile time. There's no variable storage or lookup for those values, and unused constants actually disappear from the object code, saving even more space.

Does it matter in a little dinky 30K demo? No. But does it matter in large applications, spanning thousands of source files and multiple megabytes of object code?

Absolutely.


Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #14 on: March 24, 2022, 08:09:06 pm »
Just wanted to say thanks for all comments.  I've decided to introduce a new statement called "EQN" (for equation).  Although EQN and CONST use the same background stuff to implement them, I like the semantic differentiation.  I'm not liking a CONST statement allowing appearance of not looking like a constant.

If anybody knows of any BASIC that already has an EQN statement or function, please let me know.

Imagine an income tax program or something like that, here's how EQN works:

Code: QB64: [Select]
  1. dim line1, line2, line4
  2. eqn line3 = line1 + line2 , line5 = line3 + line4
  3. line1 = 3 : line2 = 7 : line4 = 4
  4. print line1
  5. print line2
  6. print line3
  7. print line4
  8. print line5