• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Poigahn

Are Certain Constants Really Necessary ?

13 posts in this topic

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 ?

2

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites
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 ;)
1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0