Is it a good practice to declare all the variables in my program as a const variable?


private int myAge = 20;  
private const int Age = 20;  

Now, from the above example, which one is a good practice for declaring variables. Is declaring const proved to be more effective in coding than just declaring a variable?

  • 1
    Well... Is the variable a constant? Or is it something that will change? Your age seems likely not to be a constant-- I assume you age over time. pi is a variable that would seem appropriate to be a constant-- that is not going to change. – Justin Cave Sep 4 '16 at 5:01
  • yes but Age will be constant variable for a long time.... so I only have to change the value once every year right? – Mohammed Imadh Sep 4 '16 at 5:43
  • That would involve re-deploying your code every time any non-constant "const" variable needs to change. And tracking when that needs to happen. You could maybe do that if you only have one such variable in your code base though it would be a bad idea. In reality, large code bases have a lot of variables whose value changes infrequently so if you did this with non-constant values, you'd have a ton of deploys, some likely at inopportune moments, to fix the values. And you would inevitably miss some. – Justin Cave Sep 4 '16 at 6:06
  • I would have derived highest amount of benefit from cases where programmers who wrote a 500 liner method would have also declared the arguments to be "const" (because they were not modifying the arguments). – blackpen Sep 4 '16 at 7:34

If the variable is actually a constant then, yes, it's a good idea to declare it as such. If the variable is not a constant then, no, it would be a terrible idea to declare it as a constant.

Your age is unlikely to be a constant. Presumably, you age over time so you would want to perform the calculation. Something like the variable pi or the speed of light is actually a constant and should be declared as such.

If you declare non-constant values as constants then you'd have to re-deploy code every time those values change. If you have only one variable in your code base and you know when your age changes, that would be a bunch of extra work to track and fix every year but it could be done. Real applications, though, have lots of different variables that change at different rates. Keeping track of all of them would mean that you'd be constantly deploying code just to change "constant" values. Many of those deploys will come at inopportune moments-- month ends, holidays, during vacations, during periods of peak load, etc. And inevitably, you'll miss some.

| improve this answer | |
  • Well, it is good that someone explains the OP why "age" is a bad example for a constant, but just by not declaring a variable as const - in the way the OP showed in his example - the need for redeploying remains the same. Think how the question might be answered if the OP had choosen a better example. – Doc Brown Sep 4 '16 at 7:16
  • @DocBrown how do you mean? – Mohammed Imadh Sep 4 '16 at 7:49
  • The reason to declare an identifier as constant is that it would be a bug to change it. There are things you can do with constants you can't do with variables that vary. You declare it constant so the compiler will yell at you if you forget and try to change it anyway. Doesn't matter if it is your age, time of day, or the color of your socks. – candied_orange Sep 4 '16 at 8:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.