using constants (1 Viewer)

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
how do you guys use them? and why? do these things really matter? and why would I use them over vars? keep in mind, that I don't care one bit about anything regarding ""clean code"" and "'efficiency"" when it comes to comparing the use of these two concepts.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:16
Joined
Feb 28, 2001
Messages
27,148
Constants have the special "safety" net that prevents you from accidentally changing them when you didn't mean to, perhaps by passing a constant to a subroutine that has an accidental, unintended side-effect. If well coded, nothing bad happens. But I'm human and make mistakes.

This attitude, by the way, comes from the full quote of Murphy's law. The more commonly known part is, "If something CAN go wrong, it WILL go wrong." The part people forget was "Therefore, design your work so that it can't go wrong." Ed Murphy was a real person, an engineer at White Sands Proving Grounds in the middle of the 20th century. From his advice we now have polarized plugs and molded specialty sockets that can only be plugged together in one way. Circuit boards have "splits" in the sockets and the board that are keyed for each other.

Using constants in a program when you could have declared a variable is simply a way to assure that you cannot mis-use that named thing in a way that could change it because of a careless statement or side-effect. You can only use it one way. The compiler or the run-time system will find it for you and protect you from yourself.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:16
Joined
Aug 30, 2003
Messages
36,124
I use them for things like file paths where templates are stored. Various places in code with combine the path with a file name. When they move everything to a different server every 5-7 years, I just change the path in one place. I also use them for passwords to access restricted forms. Both of these uses could be handled by storing values in a table and looking up, but changes are so rare I just put them in a constant and save the trips to the server to do the lookups.

we now have polarized plugs and molded specialty sockets that can only be plugged together in one way.

Funny story, years ago our shop manager had a problem with his new monitor not working (old VGA plug, only one way to plug them in). He had managed to plug it in the wrong way, or at least push it down so it looked like it was plugged in. Mangled the pins but we managed to bend them back into place and get it working. ;)
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
careless statement or side-effect. You can only use it one way. The compiler or the run-time system will find it for you and protect you from yourself.

do I really need that!? with all the effort I give, and my sharp mind? I might make mistakes, but I sure as hell don't make many. the reason? because i make machines tell me if it works or not. how could you possibly make a mistake that way!?

But I'm human and make mistakes.

no you don't! you're perfect Richard. the best sailor the Navy ever saw. like Bret the Hitman Hart: the best there is, the best there was, and the best there ever will be. LOL. You're even better than @NauticalGent . aren't you? or are you gain on equal ground? In terms of needed corrected, you certainly are equal. ;) per this man's knowledge of who you are.
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
@The_Doc_Man , then why in the world would I ever use variables again!? If constants make life so simple you can't possibly make a mistake, and if I'm not afraid the world is going to blow up, and thus are obsessed with marginal efficiency effects that are just stupid, why should I ever declare a variable again? because it might have to be converted in routines using the C() class of functions?
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
Funny story, years ago our shop manager had a problem with his new monitor not working (old VGA plug, only one way to plug them in). He had managed to plug it in the wrong way, or at least push it down so it looked like it was plugged in. Mangled the pins but we managed to bend them back into place and get it working. ;)

you think that's funny? LOL. try this one, told to me by a professional computer repair shop owner. a very intelligent man, but not accepted in the tech world because he doesn't ""look the part"". thus, they think he's stupid. but through conversation with me, he proved himself to be quite amazing, and have a caring soul to boot:
his assistant: i had a lady come in yesterday and tell me her screen was blank.
me: was it dead? was it old? outdated? did the backlight die?
her: i thought so. I tried everything I could think of after taking if apart and examining all the parts for failures.
me: and you found nothing?
her: nope. then I put it all back together and pushed the power button. it turned on.
me: LOL. i gotta go. talk to ya later!

here are the people that told me about this intelligent customer:
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:16
Joined
Feb 28, 2001
Messages
27,148
do I really need that!? with all the effort I give, and my sharp mind? I might make mistakes, but I sure as hell don't make many. the reason? because i make machines tell me if it works or not. how could you possibly make a mistake that way!?



no you don't! you're perfect Richard. the best sailor the Navy ever saw. like Bret the Hitman Hart: the best there is, the best there was, and the best there ever will be. LOL. You're even better than @NauticalGent . aren't you? or are you gain on equal ground? In terms of needed corrected, you certainly are equal. ;) per this man's knowledge of who you are.

Not sure why you posted this, because I guarantee you in writing I'm not perfect. And forgive the language, but I don't need ass-kissing either. So your purpose in posting this response totally eludes me. If that was supposed to be sarcasm, it is also wasted.
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
Not sure why you posted this, because I guarantee you in writing I'm not perfect. And forgive the language, but I don't need ass-kissing either. So your purpose in posting this response totally eludes me. If that was supposed to be sarcasm, it is also wasted.

ummm....ass kissing? what made you think I engage in that? I gave you a compliment. oops? I posted it because I wanted the veterans' viewpoints on if constants even mean anything, or if they serve a purpose other than just wasted features that be substituted with other things. follow?

not to mention, I might have not known something about them. other than the fact that they're "constant"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:16
Joined
Feb 28, 2001
Messages
27,148
Straight answer: Constant declarations, like variable declarations and enumeration declarations and subroutine or function declarations, are all "tools" in the Access VBA programmer's tool kit. Just like hammers, saws, spirit levels, and screwdrivers in a carpenter's tool kite, each one has a purpose and a situation of applicability. The more tools you have in your tool kit, the more situations you will be able to manage when something odd shows up.

If it was your intent to pay me a compliment, thank you. It is just rare that you do so and you managed to surprise me.
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
Well you can expect more surprises from me in the future then richard. Not necessarily as an attempt to win my way back into this community because I don't give one crap about helping people here if you guys aren't going to let me. but then again I'm busy with other things anyway. And I'm not going to put you down but I can tell you that that last response from you didn't provide me any value whatsoever. I know they're all different types of tool kits but literally if you learn from the corporate leaders who buy automation and spend their Summers and Winters and switzerland, you realize that basically they're just buying patterns and employing people until they can put them out of work.but yet again I'll say that that's not everybody but it tends to be the majority every year that this earth goes on
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:16
Joined
Aug 30, 2003
Messages
36,124
If constants make life so simple ...why should I ever declare a variable again?
And I'm not going to put you down but I can tell you that that last response from you didn't provide me any value whatsoever. I know they're all different types of tool kits

I suspect Doc's response was to the first quote, and I had the same reaction. They do different things, that's why you'd use a variable in some circumstances and a constant in others. If his answer was of no value, you didn't understand it.

As his original answer conveyed, a variable's value can be manipulated in code, a constant's can't. Thus you use a constant when you want the value to be...constant, and a variable when you want the value to be...variable. ;)
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
a variable's value can be manipulated in code, a constant's can't. Thus you use a constant when you want the value to be...constant, and a variable when you want the value to be...variable. ;)
ok then, got an example of when I would not use const's? other than repetitive tasks that involve different sets of data being manipulated? (like say, validating form controls on many different forms by a single call, as in an example I already posted).

maybe these things?
Code:
- computerName
- path variable
- credential table name
- dir for daily backups (per your words)
anything else? got a sample scenario, with code?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:16
Joined
Aug 30, 2003
Messages
36,124
I suspect variables are used 99% of the time. Anytime you want to be able to set the value, for example from a form control.

Diff:
Dim lngWhatever As Long

lngWhatever = Me.TextboxName

'code that does something with lngWhatever
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
I suspect variables are used 99% of the time.

I agree. I have almost never seen const's used. but I did take a test in javascript, issued by a dumb testing company, like they all are, bought by the actual employer, Dwolla: https://www.dwolla.com/

it was using const's heavily. I understood the problems they were asking me to solve, but I had no time to take it. I didn't even take their request to interview me. :p

oh, and speaking of genius testing companies, I once took a test in PHP from Robert Half Technology, the HEADHUNTING FIRM! aren't they spose to be seeking smart people!? LOL. they bought it from a chinese company if I remember right, and one of the questions was literally something like this:
If you want to check whether a post variable in PHP is set or not, which of the following lines of code would you use?

a) if $_POST["tag"]
b) if not $_POST["tag"]
c) if $_POST["tag"] <> ""
d) if null (if $_POST["tag"])
NONE of those are right! I took a screenshot and sent the damn thing to the recruiter who found me. I told him to get his money back from the testing company because obviously they had no clue who they were buying from, as most of those guys don't. Why would I work for a company that buys from the Chinese who don't even know what they're doing!? :rolleyes: I still know many smart people that work at Robert Half though, and keep in touch with them often. But the recruiter I'm talking about ditched me as a connection on LinkedIn later on. Go figure.

by the way, the correct answer is (or rather, can be, either of these, unless I'm mistaken):
Code:
1) if ! $_POST["tag"]
2) if ! isset($_POST["tag"])
MY GOD. seriously,. this is exactly why you DON'T pay for testing services in the coding world.
 
Last edited:

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
hey Paul, you want a great example of simplistic reality and why testing is a complete waste of time? check out this intelligent comment by a professional businessman that knows how to stay away from people just trying to get money out of young people:

11111.jpg
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:16
Joined
Jan 20, 2009
Messages
12,851
how do you guys use them? and why? do these things really matter? and why would I use them over vars? keep in mind, that I don't care one bit about anything regarding ""clean code"" and "'efficiency"" when it comes to comparing the use of these two concepts.
Strange that you wouldn't care about coding efficiency. I would consider the use of a variable where a constant was appropriate as a deficiency in the coder's ability.

A constant can be declared in a single statement. A variable needs to be declared then have its value assigned. The use of a constant signals its nature to anyone reading the code. You don't have to worry about reassigning the value of a constant when your code screws up and you have to reset the project.

Probably also worth mentioning the use of Enums which are essentially sets of constants allowed as argument values for a particular parameter. I would also consider the coder to be deficient if they did not use them where appropriate when writing a sub or function.

Similarly the use of a Public Variable for something that aught to be a Property of the object.

Worse still the use of a Global Variable read or written to from inside a Sub or Function. Values should be passed to a sub or function through parameters. (It is the equivalent of going in or out of a building through a window instead of the door.)
 

neuroman9999

Member
Local time
Today, 01:16
Joined
Aug 17, 2020
Messages
827
Strange that you wouldn't care about coding efficiency. I would consider the use of a variable where a constant was appropriate as a deficiency in the coder's ability.

I was referring to people who are so obsessed with efficiency and afraid the world is going to blow up, that they literally will go to hell and back splitting hairs over "this vs that" in code that it ends up wasting serious amounts of time.

Probably also worth mentioning the use of Enums which are essentially sets of constants allowed as argument values for a particular parameter. I would also consider the coder to be deficient if they did not use them where appropriate when writing a sub or function.

Similarly the use of a Public Variable for something that aught to be a Property of the object.

Worse still the use of a Global Variable read or written to from inside a Sub or Function. Values should be passed to a sub or function through parameters. (It is the equivalent of going in or out of a building through a window instead of the door.)

all of things I've just quoted you are exactly why so many arguments about who's "more right" takes place in large companies. I am assuming of course, but I know it goes on. in this day in age, the answer isn't usually what "is proper and most efficient", but rather what "can get done in ''x'' days or weeks so we can shove the next product out the door on time and see if it sells.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:16
Joined
Feb 28, 2001
Messages
27,148
When I talk about the "toolkit" it is because I think of Access as a tool for the mind (of the programmer) just like a power tool is a tool for the hand of the carpenter. Having "tools in the toolkit" implies you know more than one way to approach a problem. It derives from the old phrase that says "If all you have is a hammer, everything needs to be a nail." Knowledge of your language's abilities over a wide range of applications simply means that you will have more tools in the mental toolkit to face the next problem. I guess my metaphor was too deep for you?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:16
Joined
Jan 20, 2009
Messages
12,851
I was referring to people who are so obsessed with efficiency and afraid the world is going to blow up, that they literally will go to hell and back splitting hairs over "this vs that" in code that it ends up wasting serious amounts of time.



all of things I've just quoted you are exactly why so many arguments about who's "more right" takes place in large companies. I am assuming of course, but I know it goes on. in this day in age, the answer isn't usually what "is proper and most efficient", but rather what "can get done in ''x'' days or weeks so we can shove the next product out the door on time and see if it sells.

It takes less coding time to declare a constant than to declare a variable and give it a value. Never mind the time it takes to restore a value after a reset, or worse still, the time wasted when forgetting to restore the value. Only an inexperienced developer or a fool would choose to use a variable for a constant.

In any project involving multiple developers, the time to debug how modules of code work together can easily exceed the total time taken to write the modules, unless good coding practices are followed. Defining the interfaces through which the modules engage is of paramount importance especially where code is reused. A reference to an external variable buried deep in the code can waste enormous amounts of debugging time.

Enums with meaningful names saves coding and debugging time by avoiding having to remember if it was 4 or a 5 that needed to be used for this or that that argument. Throwing an error instead of quietly taking an invalid value is also a time saver.

Anyone with your attitude to coding certainly would never be included in any team I was putting together for a project. You would waste far too much of everyone's time.
 

Users who are viewing this thread

Top Bottom