|All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
How do you bake constants into your code?
Sunday, September 21, 2008 3:03 PM
Do you use magic number/strings in your code? Shame on you! Do you use constants or read only variables, KUDOS to you! But do you know the difference between a const and a readonly variable? Read on for some insights into their differences.
Since the beginning of programming coders have been using magic strings/numbers in their code. But about six months after releasing their code (and returning from vacation) they couldn't remember WTF they were thinking and what those "magic" values meant. They were magical all right!
Fast forward to relatively modern languages (and I'm sure some older ones too :>) and you have constants support in the compiler. The classic example is PI = 3.14, the developer would dictate in their code that PI = 3.14 and from now on, anything using that piece of code understands what PI is. Beautiful. THAT is magic IMHO. But what happens if you want your variable to act like a constant but you don't know what it is at compile time? hhhmmm What's a coder to do in that case? Most, if not all modern languages have the concept of a read only variable for just such an occasion. The quick explanation is a read only variable acts like a constant in that you can't change it, but the one BIG difference is your able to set it at runtime.
Here is a fast/terse list of differences between the two (and some error messages if you break the rules).
- automatically considered static, therefore don't need that scope modifier, in fact the compile will complain about it
- value is initialized at COMPILE time therefore the compiler MUST be able to figure out the value otherwise an error results
- cannot be changed once it's assigned, that's just something you can't do, sort of like disobeying the laws of gravity.
- must be a value type, nope, cannot put any classed into a const cause that would mean your initializing with a new and therefore it's done at runtime but we already discussed, const variables are initialized at compile time.
- can be either instance or static variables
- evaluated at runtime so you can have a variable act like a const but initialized at runtime, this is the best of both worlds
- Although you can get the best of both worlds, you are limited to where you can initialize the variable
I hope you enjoyed this brief discussion on the differences between const and readonly. There are benefits to both of these keywords and now I hope you're better armed to make up your own mind on which to use in your code.
References: const at MDSN, readonly at MSDN, Patrick Steele's blog entry, CSharp-Online.NET blog entry