+ 1

What is the use of constants?

why would I use a constant instead of a Variable? I understand that a constant cant be changed but I can simply use a variable and not change it

11th Dec 2016, 12:10 AM
Andrei
Andrei - avatar
5 Answers
+ 5
Variables can be undefined, constants not. It's more a caution meassure. In bigs proyects, where you have thousands variables, you might change an unwated var. But you can't change the CONST value, so its safer, and don't let you commit clunky errors.
11th Dec 2016, 12:21 AM
Nahuel
Nahuel - avatar
+ 3
Let's suppose that you want to have a number of PI. You want to use constant value here, as PI number is unique and you do not want to ever alter it in future. Safety purpose mainly.
11th Dec 2016, 2:29 AM
Maksym ZieliƄski
Maksym ZieliƄski - avatar
+ 1
Didn't allowed to change. like some date of app.It's safety!
11th Dec 2016, 10:22 AM
supi
supi - avatar
0
Using constants is more a way of defensive programming, to protect yourself from yourself, from accidentally changing the value somewhere in the code when you're coding at 2 a.m. or before having drunk your coffee.
22nd Jan 2017, 12:40 PM
lamya
lamya - avatar