Jump to content

  • Log In with Google      Sign In   
  • Create Account

Are Certain Constants Really Necessary ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Poigahn   Crossbones+   -  Reputation: 519

Like
2Likes
Like

Posted 17 March 2013 - 05:54 AM

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 !!


Sponsor:

#2 wintertime   Members   -  Reputation: 1727

Like
12Likes
Like

Posted 17 March 2013 - 06:12 AM

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.



#3 jbadams   Senior Staff   -  Reputation: 18743

Like
13Likes
Like

Posted 17 March 2013 - 06:14 AM

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.)

#4 Servant of the Lord   Crossbones+   -  Reputation: 19685

Like
5Likes
Like

Posted 17 March 2013 - 06:28 AM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Poigahn   Crossbones+   -  Reputation: 519

Like
1Likes
Like

Posted 17 March 2013 - 08:13 AM

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 !!


#6 freeworld   Members   -  Reputation: 325

Like
1Likes
Like

Posted 17 March 2013 - 06:30 PM

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.

#7 TheChubu   Crossbones+   -  Reputation: 4425

Like
1Likes
Like

Posted 17 March 2013 - 08:23 PM

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


#8 Ravyne   GDNet+   -  Reputation: 7498

Like
1Likes
Like

Posted 18 March 2013 - 06:05 PM

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.



#9 Poigahn   Crossbones+   -  Reputation: 519

Like
0Likes
Like

Posted 20 March 2013 - 06:06 AM

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 !!


#10 nobodynews   Crossbones+   -  Reputation: 1934

Like
0Likes
Like

Posted 20 March 2013 - 07:34 AM

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!


#11 Poigahn   Crossbones+   -  Reputation: 519

Like
0Likes
Like

Posted 20 March 2013 - 11:52 AM

I think you misunderstood the point of the post you're responding to.

You are correct.  I did not understand the meaning to what I was rsponding to.  However, COnstant by definition means steady, and never changing.  Therefore it would not ever be updated internally,  in that case by definition, would be a veriable, therefore, it would get an identifier.

 

 My point, by starting the topic, was 2 fold.  1 - in small programs, an Identifier for a constant does not have to be used, However in larger, especially in programs where a reference for how a particular value is used, an identifier could  explain why that value is there.

 

EXAMPLE :  Game has a maximum level that could be reached rather that having a line of   > IF Attr(6) => 20 then Attr(6) = 20   vs IF Attr(6) =>MaximumLevel then Attr(6) = 20

 

In the first IF nothing states what Attr(6) means or what 20 means  versus The second IF a person could infer that Attr(6) is a level of some type because of MaximumLevel and that the level of 20 was reached.


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


#12 EddieV223   Members   -  Reputation: 1406

Like
0Likes
Like

Posted 20 March 2013 - 01:09 PM

The closer your code looks to real English, the better it is for future you and anyone else who may have to read/modify the code.


If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#13 Cornstalks   Crossbones+   -  Reputation: 6995

Like
1Likes
Like

Posted 20 March 2013 - 01:19 PM

The closer your code looks to real English, the better it is for future you and anyone else who may have to read/modify the code.

I don't know if I'd argue that it should look like real English, but I agree with the sentiment and would actually state it more like "The less your code looks like an obfuscated C programming challenge, the better for everyone."


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#14 Ravyne   GDNet+   -  Reputation: 7498

Like
1Likes
Like

Posted 20 March 2013 - 02:50 PM

EXAMPLE : Game has a maximum level that could be reached rather that having a line of > IF Attr(6) => 20 then Attr(6) = 20 vs IF Attr(6) =>MaximumLevel then Attr(6) = 20

In the first IF nothing states what Attr(6) means or what 20 means versus The second IF a person could infer that Attr(6) is a level of some type because of MaximumLevel and that the level of 20 was reached.

 

Sometimes you will have literal values (that is, numbers, strings, or other data) that are hard-coded into your app. If the concept that they represent is not shared anywhere else in code then its reasonable, at your preference, to not have them be constants. Indeed, all literal values are just constants without a name.

 

There's a proverb that goes "Words have meaning, but names have power." I think a corallary for us programmers is "Literals have purpose, but identifiers (names that belong to something) have meaning."

 

The act of making a constant out of a literal -- that is, giving it a name -- is most benefitial when the concept it represents is shared, but even when not shared it has an additional side benefit of documenting what it is, and of clearly separating it from other identical literals.

 

In short, no, you don't *have* to make every literal a constant, but there are strong arguments to be made that you often should.


Edited by Ravyne, 20 March 2013 - 02:52 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS