http://qs1969.pair.com?node_id=686468

I've always been a fan of const in C++, and make everything const-correct.

Perl doesn't have anything like that, and Perl 6 has some more exposure of "readonly" values. But that affects assignment to the container, not mutating of an object.

Does anyone here care? What do you think of const and keeping mutators separate from accessors, in general? Does anyone else miss that in other languages?

—John

Replies are listed 'Best First'.
Re: const correctness
by dragonchild (Archbishop) on May 14, 2008 at 09:41 UTC
    First off, Perl has Readonly and constant which provide the same functionality. And, every single (usable) OO framework on CPAN allows you to specify accessors as separate from mutators, should you so choose.

    Now, to some commentary.

    • Proper objects don't give you access to their state. That is a first-order violation of encapsulation. If you're peeking and poking at internal state, you don't have an object - you have a data structure with behavior. This is no better than the OO provided by Javascript. While I'm a fan of the prototype style of OO, it's not very useful from a sanity perspective.
    • I understand the sanity perspective when dealing with constants. But, I gotta tell you - I've been coding Perl for almost 13 years now and I have yet to run into a situation where I needed constants. The fact that I have never used Readonly and don't reach for constant in my own code should be telling. Frankly, I tend to use some sort of configuration object that exposes appropriate values through methods (not accessors!) and takes its initialization from some sort of config process (file, commandline, %ENV, etc). This is what DBM::Deep does and it works quite nicely.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Perl has Readonly and constant which provide the same functionality.

      They're not the same thing. const is not about constructing immutable values it's about being able to declare that "in the following code, the contents of this variable should not be modified, if you see me trying to modify it, throw a compile-time error".

      It's also about making a promise in your API which the caller knows will be kept because it has been checked at compile-time. Of course you can force the compiler to let you break the promise using a cast but it can't happen as an unanticipated side-effect or typo.

        In compiled languages, its also about giving the compiler clues about optimisations it can safely perform, especially in multi-threaded environments. A const value can be safely shared without locking, or moved into a register and not re-read for every reference. The current big problem is the lack of transitivity of const.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

      When first I learned programming in a formal environment (first computer science classes), I was very concerned about properly noting my "never to be changed" variables to the compiler. When I got to Perl, it was one of the first things I looked for, but was confused by the differences between and the possible subtle traps within Readonly and constant. So, I stopped using them, even though it bothered me to do so.

      And today my own thoughts are more in line with your point number two. My own code makes obvious what should not be changed internally, and the external interface protects/hides everything from outside code. I like giving myself as much protection against myself as possible (strict, warnings, etc.), but now feel like const would be redundant with the checks implicit in the way I code.

Re: const correctness
by BrowserUk (Patriarch) on May 14, 2008 at 07:16 UTC

    What does const-correctness mean in a dynamically-typed language where a read reference to a never-knowingly-mutated variable in a different context to its origin, numeric vs. string or vice versa, can cause its internal state to mutate? And is it worth giving up dynamic typing for? If so, why? What does it buy you?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: const correctness
by RMGir (Prior) on May 14, 2008 at 14:45 UTC
    Not directly relevant to your question, John M. Dlugosz, but the "D" language has an interesting approach to const-ness.

    Mike

      Its interesting stuff, but that article is misleading/out-of-date. Quotes like:

      Which brings up another aspect of const in D - it's transitive. Const in C++ is not transitive,

      suggests that D's const is superiour to C++ const because it is transitive. But, they more recently had to add an new keyword: invariant to try and achieve that transitivity. The big debate now is how to make those two work together. More interesting stuff, unless you're trying to get some work done.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: const correctness
by samtregar (Abbot) on May 15, 2008 at 19:27 UTC
    I've always been a fan of const in C++, and make everything const-correct.

    Because? I try to do it when I'm patching C code only because I imagine the maintainers might care. I doubt I would bother in my own code. It seems about as useful as that crazy-paranoid public/private/protected nonsense. Oh no, someone might call a private method! The sky, it's a gonna fall!

    -sam

      A decent C++ compiler (can't believe I wrote that) can perform several useful optimizations if you have correctly used const. It can also create a program which crashes spectacularly if you have used const incorrectly.