Why use constants? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why use constants?

Is there a benifit for using a constant over using the other variable types?

27th Jun 2017, 8:29 PM
Nobody
7 Answers
+ 5
They also can be used for set up. For example, I have the default speed of a foe (in a spaceship game, it will not change when the project will be finished). I use this value a lot in my code. If I want to change the value (to try with quicker foes), I'll just have to change the constant instead of changing every where I used it. So, why not using a variable ? For the same reasons that was already told by @aklex : optimisation.
27th Jun 2017, 11:22 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 6
It also lets the compiler know that the object/variable will not change, which can open up more optimizations made by the compiler
27th Jun 2017, 9:52 PM
aklex
aklex - avatar
+ 4
Constants are for making sure that if you intend to create a value that cannot be changed, then you can know that a constant will never change and therefore is safer to use. This is how you can avoid debugging headaches down the line.
27th Jun 2017, 9:12 PM
Jared
Jared - avatar
0
constant are used to make value same throughout the project
30th Jun 2017, 6:54 PM
Syed Iftequer
Syed Iftequer - avatar
0
it's mainly use full deceleration of database or configuration of database
30th Jun 2017, 6:55 PM
Syed Iftequer
Syed Iftequer - avatar
0
in my opinion we use the if we have something and it doesn't change
7th Jul 2017, 1:55 PM
mohamad92001
0
π es un ejemplo clarísimo
12th Jul 2017, 6:20 PM
Ciro Hurtado
Ciro Hurtado - avatar