Are Certain Constants Really Necessary ?

Started by
12 comments, last by Ravyne 11 years, 1 month ago

I have been reading through several sample programs and tutorials, I quite simply sometimes get lost following certain variables and ask the question why they are used that way ? Maybe someone can provide a logical answer or possibly agree with my observation.

In Particularly the following :

Constant Hieght:640, Width:800;

Screen(Height,Width);

Is this really necessary ? Is it not just as effective to simply code Screen(640,800); ?

Now some constants I could understand why they are used and in fact use them quite often myself. Example creating and dimensional array of constants where a certain set of variables may change through the course of the program, but will only vary within a certain predesigned set.

For Example : Constant Hi(1):60,Hi(2):80, Hi(3):100;

This is then used like so :

Print(" Your High at this level is > ",Hi(Level));

So, are constants that never really need to change within the program really necessary define as a constant variable ?

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

Advertisement

Because a name gives meaning to a number. Otherwise one would need to type comments instead.

Then there is also the issue of needing some numbers more than once and preventing mistyping or having to change it on many places where you dont know if its just the same number by accident or really one of those you want to change.

Then it could also be planned to change some constants to variables later when for example the fixed screensize gets inconvenient.

What if you need to use that width and height in more than one place?

By using constants rather than hard coding the values as "magic numbers" you make it easier and more reliable to update the code to use different values or to reuse it without having to change the numbers in multiple locations.

In some cases -- those where the numbers in question might be less familiar/easily recognizable -- the use of a named constant can also make the code easier to read and understand.


The use of named constants isn't necessary, but it can be beneficial in some cases.

(Posted from mobile.)

- Jason Astle-Adams

It's not that constants need to change within the program, it's that they need to change during the development of the program.

Later, you might decide the window would be better off 800 x 600, but would have to track down the 20 places you used the magic number 640 x 480, and might miss one or two places, and spend alot of time wondering why your GUI no longer properly aligns to the right side of the screen, or why your mouse clicks are off a few pixels.

But in addition to constants, these variables sometimes are also used as placeholder variables until the program expands to load them from config files.

So essentially what youare stating,and Thank You, because that does make logical sence, is that in larger programs....

1 - Using an well defined variable name for a Constant Variable, rather than, in my example, would help explain what the number is used for.

2 - Making Global Changes is simplified by simply changing the value at the declaration point of the program, rather than having to hunt down ever instance in which that number is used ( Or String ).

I would like to add that in addition to the constant declaration, that when a variable name is used for instances such as my example, that if the programmer is using a detect graphics or resoulution statement that the height / width variable itself would change to encompass the individual user's hardware adapters. This is part of proper planning.

I would like to Thank the above respondants.

I simply asked the question, not for personal knowlege, rather than to illustrate a point to those coders whom use vague variable names the importance a giving a variable a meaningful Name.

Again Thank you

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

Servants example is the biggest reason I use such constants. I will also do this a lot for variables I know later ill be loading from a config file... but in the need to get things tested asap ill use constants then later remove the constant and make it a member variable once i got the settings-loading code in place. This assumes it won't be used anywhere but the given file.

More of a place holder in bigger projects. In smaller projects I do it to keep all those variables I know ill have to tweak over and over again in one clean place.... so as servant said I don't have to hunt for them.

Edit... derp servant I didn't read your whole post.... atleast I'm your new fan boy ;)
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

Sometimes its just the habit of it. You need some numbers, so you give them a name and use them. Its not like it costs you anything, and you might need to use such numbers in different places along the road.

Besides, the less places you have where you get such numbers, the less places you'll have to look for if your code breaks.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Think of constants (and, less preferably in nearly all cases, #defines) as constants for the compiler itself, rather than the program -- although, of course, the program can read and use them too. Constants and defines are known quantities at compile time, therefore, they enable the compiler to compile the software differently -- e.g. by directly inlining the value into the instruction stream, or placing it in a read-only area, whatever's best for the platform and optimization profile.

Now, at this point you're probably wondering why I'm essentially just giving you the definition of a constant, and here's where I follow it up with this: constants provide all the benefits to code generation (at compile-time) as variables give to code execution (at runtime). Namely, it gives a name and a context to a number, this promotes it from a simple number to a concept; further, just like a variable, any change to its value is reflected wherever you reference it, and in the case of constants that means its value is defined in exactly one place.

On the point of constants becoming concepts, its an important and sometimes overlooked point about constants -- you might have several constants with the same value, but conceptually, the constants are not identical. If you mixed concepts, you might make a change to it's value and find that it affects a system that you weren't intending it to, in addition to the one that you were. In this way, 'magic numbers' with identical values also represent mixed concepts. That is, you can't just do a blind replace-all on that value because the context is lost; you'll have to examine each use of the magic number to determine whether its context is the one you mean to affect.

throw table_exception("(? ???)? ? ???");

On the point of constants becoming concepts, its an important and sometimes overlooked point about constants -- you might have several constants with the same value, but conceptually, the constants are not identical. If you mixed concepts, you might make a change to it's value and find that it affects a system that you weren't intending it to, in addition to the one that you were. In this way, 'magic numbers' with identical values also represent mixed concepts. That is, you can't just do a blind replace-all on that value because the context is lost; you'll have to examine each use of the magic number to determine whether its context is the one you mean to affect.

I am sorry I don not get your point here ! Is not the use of a Constant Identifier is so you could change the value once, in order to change it globally ? Knowing how it was used, and how it will change the programs results is all part of knowing what you program is designed to. And that by having several constants with the same value you are already acknowleging that you, as the programmer might eventually change their value in certain parts of your code.

Here I would think that you would declare as variables, load their values from a protected file at runtime, this way, as the programmer if you ever need to modify the effects of the values and the way they are used, you could simply replace those values in the external file, rather than having to modify the original code and recompile.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

On the point of constants becoming concepts, its an important and sometimes overlooked point about constants -- you might have several constants with the same value, but conceptually, the constants are not identical. If you mixed concepts, you might make a change to it's value and find that it affects a system that you weren't intending it to, in addition to the one that you were. In this way, 'magic numbers' with identical values also represent mixed concepts. That is, you can't just do a blind replace-all on that value because the context is lost; you'll have to examine each use of the magic number to determine whether its context is the one you mean to affect.

I am sorry I don not get your point here ! Is not the use of a Constant Identifier is so you could change the value once, in order to change it globally ? Knowing how it was used, and how it will change the programs results is all part of knowing what you program is designed to. And that by having several constants with the same value you are already acknowleging that you, as the programmer might eventually change their value in certain parts of your code.

Here I would think that you would declare as variables, load their values from a protected file at runtime, this way, as the programmer if you ever need to modify the effects of the values and the way they are used, you could simply replace those values in the external file, rather than having to modify the original code and recompile.

I think you misunderstood the point of the post you're responding to. The post is saying that if you have a constant of 5 in one part of your program and a constant of 5 in another part of your program that doesn't mean those constants have anything to do with each other. If the first is the number of enemies you allow on screen at once and the second is a timer for something, you wouldn't necessarily ever want to update them both to the same value at the same time. That's why you should give them different identifiers despite having the same value. Whether those identifiers are constants or not themselves to be loaded from a file or set at compile time is a different conversation than what Ravyn was talking about.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement