Pročitaj na engleskom

Dijelite putem


const methods

My number one biggest desire in C#, or complaint (if you choose)... Are we ever going to see const methods, objects and properties? I'm one of those who basically think languages we should actually be delimiting non-const items, and having everything default to const, but even though there are work-arounds, not having a const specifier I think encourages people (like me) to be lazy and write really dangerous code!

 

Const is very hard to do a multi-language environment. You would need to do it as part of metadata, and you would either only do it in some languages (which would reduce its universality), or require every language to support it (not a good thing).

 

My personal experience convinces me that const is a good idea in the small, but doesn't work too well in the large. Whenever I tried it in C++, I'd always run into the case where a method that was const needed to not be const (and it wasn't a case where mutable would have helped). At that point, I either needed to de-constify all call paths down to that function, or split the api into const and non-const versions. The first is a lot of extra work, and the second will decrease my overall quality.

Comments

  • Anonymous
    April 22, 2004
    Yes, adding "const" after the fact is very difficult. And sometimes the semantics of "const" can be confusing (i.e., caching an expensive "get" value).

    Nevertheless, much of the time it really helps keep you honest. I'd also like to see the default be "const", adding "mutable" when needed.
  • Anonymous
    April 22, 2004
    The comment has been removed
  • Anonymous
    April 22, 2004
    so what if you can p0wn memory. anybody with half a brain and a debugger can do that.

    const was intended to discourage modifying the state of the object passed in, and the compiler had a duty to attempt to help.

    i hate the fact that i can pass in an object and the method can update a property without me really knowing about it.

  • Anonymous
    April 22, 2004
    Okay, but it's still going to lead to buggy code. There are current mechanisms in place that you can use to find out if your object has been modified.

    Events, comparing properties before and after, documentation, sending in a clone, etc.

    The problem is that const would be purely synctatic eye candy, and pretty much meaningless.

    If it is meaningless, but a lot of people would have a very strong tendency to assume it isn't, then that makes it dangerous.

    If people treat it as a dangerous hint, and still have to do their checks to maintain safety, then what was the point of having it?

    Might as well just put it in an intellisense comment at that point.


    And let's not even start on how it's going to interoperate with other framework languages, let alone ILASM.


    If you want const, it's going to have to go into the CLR, and every language is going to have to implement it. (With DRM type things coming up, who knows, it (or something similar) might actually make it in one day.))
  • Anonymous
    April 23, 2004
    I have to agree that the lack of const is one of my only two complaints about C#. The second is generics and luckily that is coming around. I bothered to learn C# and then got discouraged by these two missing features that I use a lot in C++.

    I have been part of C++ efforts to "fix" const correctness in a few projects and it always turns up hidden bugs that would have been prevented had const correctness been used from the beginning.

    Normally pass by value will prevent the passed in variable (object) from being modified by accident since the called function only changes a copy of the variable (object), but when you want to be efficient and pass by reference (since you have large objects), you need a way to distinguish the intent of passing in the reference: to modify or simply to read. You need a way to have a guarantee to the caller that the reference to the variable (object) is not used in an unintended way (i.e. to change its state). Documentation can certainly say this, but that has proven not to work. Having it right there in the language is a very positive enforcer.

    Just because some people do not understand how const can be used or are not able to use it properly does not mean that many of us do not or cannot use it properly.
  • Anonymous
    April 23, 2004
    The comment has been removed
  • Anonymous
    April 24, 2004
    re: Generic???? Part1: class constrain and struct constrain
  • Anonymous
    April 27, 2004
    "My personal experience convinces me that const is a good idea in the small, but doesn't work too well in the large. Whenever I tried it in C++, I'd always run into the case where a method that was const needed to not be const (and it wasn't a case where mutable would have helped)."

    My personal experience is the opposite and I've been programming C++ for a long time. Could you post some examples to back this up?
  • Anonymous
    May 01, 2004
    My personal experience of C++ is that anybody who insists that const is a problem has a terrible design.

    Having said that - anybody who has ever attempted to fix up a large piece of code that doesn't use const will know that (a) the person writing it should get another career and (b) there is no way it can be retrofitted to C#

    I also appreciate the previous comment about the difficulty of integrating const aware and non const aware languages.

    One suggestion is to follow the java route of providing readonly wrappers for at least all the main collections and interfaces. If you put all your modifiers for a class in an interface and work in terms of the interface then readomly wrappers can always be added later.
  • Anonymous
    May 10, 2004
    "My personal experience convinces me that const is a good idea in the small, but doesn't work too well in the large. Whenever I tried it in C++, I'd always run into the case where a method that was const needed to not be const (and it wasn't a case where mutable would have helped)."

    Yes, sounds terribly. Could you give an example?
  • Anonymous
    May 19, 2004
    Oh, leave Eric alone... If the "always be const-correct" preacher (Hurb Sutter) gives up on const, Eric is fully entitled to preach versus const too :P
  • Anonymous
    May 23, 2004
    >> If the "always be const-correct" preacher (Hurb Sutter) gives up on const

    Hm... Could you give a bit more information? :)
  • Anonymous
    May 23, 2004
    >> If the "always be const-correct" preacher (Hurb Sutter) gives up on const

    Hm... Could you give a bit more information? :)
  • Anonymous
    December 18, 2004
    Helpful For MBA Fans.
  • Anonymous
    December 27, 2004
    [http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]