Constants

Blitz3D Forums/Blitz3D Beginners Area/Constants

mrmango(Posted 2005) [#1]
Need to clarify something ( since searching using the word "constants kills the engine")

Apart from good practice of using constants where a variable will never change in the program. What other benefits does a programmer get?

Some ideas I assume may be correct is less memory allocation? Like a constant will never change, therefore less memory will be allocated. Perhaps from this a VERY small improvement in performance?

Cheers

mango


BlackJumper(Posted 2005) [#2]
Readability...

Instead of coding magic numbers and potentially making mistakes, you can define constants and make your code much easier to debug/maintain later...

e.g.
Const ETunpickable = 0   
Const ETsphere = 1
Const ETpolygon = 2
Const ETbox = 3

EntityPickMode bombmesh, ETsphere
EntityPickMode cloud, ETunpickable
EntityPickMode debris, ETpolygon


I think it is far easier to work out what is intended here than the more terse...
EntityPickMode bombmesh, 1
EntityPickMode cloud, 0
EntityPickMode debris, 2


Come back to your code in a few months time and see how much sense the second version makes !?!

Just be careful to type the constants correctly, otherwise you introduce a lot of zero-valued magic numbers exactly where you don't expect them.