-3

While I understand the function of these 2 keywords, I do not understand why do we use them.

I did a lot of research but most of my findings only talk about WHAT and WHEN to use const or readonly or the difference between each, but none of them explain WHY. Let's take the example below:

const decimal pi = 3.142
decimal circumference = 2 * pi * //r

as opposed to

decimal pi = 3.142
decimal circumference = 2 * pi * //r

The purpose of const/readonly is to prevent people from changing the value, but it is not like the user has the chance to change the value of decimal pi, so why bother using const (or readonly)?

Please note: My question is WHY do we use const/readonly, but NOT "what are const/readonly.

Additional info: I need to clarify this one more time. I don't think the question is under-researched. I clearly understand the functionality of each keywords, but I just don't know why do we even bother using them. Does it actually improve performance? Or it's just a "decorative" way to emphasize: Hey - please don't change me?

C.J.
  • 3,409
  • 8
  • 34
  • 51
  • because those things won't change. – Daniel A. White Nov 05 '14 at 21:10
  • Consider if you're building a library for use by other developers or clients. In this case you may want to make sure they can't change the underlying values. – Tim Nov 05 '14 at 21:10
  • 1
    Prolly this is an opinion based question. I'd say const/readonly are there to help the readability of your code :) – DarkBee Nov 05 '14 at 21:10
  • +1 question itself seems fine, just a bit under-researched – DividedByZero Nov 05 '14 at 21:11
  • 4
    @RandomUser under-researched questions are not worth a +1. – Daniel A. White Nov 05 '14 at 21:11
  • do some reading more indepth https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e7c05d2-bd6d-4ca6-903c-bff11ef353e3/what-is-the-difference-between-const-and-static-readonly?forum=csharpgeneral – MethodMan Nov 05 '14 at 21:12
  • @DanielA.White true, true... -1 – DividedByZero Nov 05 '14 at 21:13
  • I don't think this is under-researched at all, but on the other hand, you guys clearly do not understand the purpose of this question. I clearly understand the functionality of each keywords, but I just don't know why do we bother using them. It seems to me the keyword `const` is just an excessive word, just to "emphasize": hey - this thing should not be changed. – C.J. Nov 05 '14 at 21:18
  • A const can't be changed because after compiling the code it no longer exists. Read only is the one that stops users from changing during the application. This is quite under researched. – deathismyfriend Nov 05 '14 at 21:43

6 Answers6

3

Compiler optimizations and to tell fellow Developers that they shouldn't be modified.

John
  • 6,503
  • 3
  • 37
  • 58
  • @marsh http://stackoverflow.com/questions/1707959/is-there-a-runtime-benefit-to-using-const-local-variables – John Nov 05 '14 at 21:13
  • 1
    @marsh In C#, unlike C++, you can't cast away the constness. `const` is very different in the two languages. **Very** different. – Servy Nov 05 '14 at 21:15
  • I did not notice the tag, my mistake. – marsh Nov 05 '14 at 21:16
1

Its not for the user of your program. It is for other programmers. It makes it abundantly clear that this value should not be changed. Pi should never change. It may seem a bit silly in your small example but when projects span thousands of lines of code and get split into functions it can be different.

Also that value could get passed into a reference with a different name. How does the programmer know that it should not be changed any more? Perhaps he gets it with the keyword calculationValue he thinks will I wouldnt mind changing this to 50.0 for my uses. Next thing he knows he changed the value of pi for tons of other methods.

marsh
  • 2,592
  • 5
  • 29
  • 53
1

There are a few reasons. The first would be if the variable would be accessible by outside code, you wouldn't want someone else changing the definition of PI, also it makes it clear that this variable should never change, which does provide the ability for the compiler to make some optimizations. Then there's also the fact that it can prevent you from making a mistake in your own code and accidentally changing a constant value.

Shriike
  • 1,351
  • 9
  • 20
1

"Readonly" is an expression of your intention as a programmer, and a safeguard. It makes your life easier (and anyone who has to maintain your code in the future) if a read-only constraint can be enforced. For example, if you have a "readonly" member that is initialized in the constructor, you will never have to check it for a null reference.

"Const" is similar in that its value cannot be changed, but also quite different in that its value is applied at compile time. This makes it more memory-efficient, as no memory needs to be allocated for "const" values at runtime. Note however that, in contrast to "readonly", "const" only supports value types -- "const" reference types are not allowed.

There is one interesting implication of the difference between "readonly" and "const", when writing class libraries. If you use a "const", then any applications that use your library must be re-compiled if you distribute a new version of the library with a different value for the "const". By contrast, if you use a "readonly" member, then applications will pick up a modified value without needing to be re-compiled (as you can imagine, this would simplify your life if you had to distribute a patch or hotfix).

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
0

Two reasons:

  1. Indicating to other developers that this is a value that should never change. It can help to distinguish between values like pi (which will always be 3.1415...), versus values that may some day be based on a configuration, or a user's input, or some other situational condition.

    Along the same lines, you can help to prevent other developers doing something stupid like trying to assign a new value to the pi variable, because the compiler will yell at them. In a simple two-line method like this, that's less likely to be an issue, but as your code base grows more complex it can save people a lot of time to be prevented from doing things they're not supposed to do.

  2. Allowing compilers to make optimizations. Both the initial compilation and the JIT compilation can take advantage of information about values that you know are not going to change. In the example you've given, the compiler will generate the equivalent of the following code when you use the const keyword:

    decimal circumference = 6.284m * r;
    

    Notice how the CPU doesn't need to multiple 2 * pi every time you call the method, because that's a value which is known at compile-time.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

It's not only about the user but also about the developer I would say. Half a year and 20,000 lines of code later you - or anyone else working on the code - might have simply forgotten about this.

Plus, could be performance improvements when using constants I would assume

silent
  • 14,494
  • 4
  • 46
  • 86