The value of values

or Why value-based programming is not a bad thing!

Working on values was implemented in C++ to maintain compatibility with C. Bjarne Stroupstrup also invented RAII, a method that ensure that resources are acquired correctly, and also properly released. This is more intuitive to beginners, and honestly, this is what we want to do most of the time; keeping pointers for when we really need them. c3wifegarbage

Values and references both have their uses and patching the nonexistence of value-based variables with a garbage collector makes your programs slow and less predictable. Even with the optimization made to garbage collection algorithms and the increase of computer performance, many applications need to minimize overhead and cannot afford to lose on something that can be done at compile time. There is also the predictability of a garbage collector. The ones I worked with had a non-deterministic nature, the memory gets cleaned up, but it’s hard to know the exact moment. Sometimes life depends on a program, some applications in space or health care cannot afford randomness. Serious programming often requires performance and predictability. It will be possible for someone to add the possibility to use a garbage collector, but it shall not be a necessity in C3.

Anyway, garbage collection doesn’t make your program leak proof, if you make some complex cycles (a pointer pointing on something that is pointing to the original), chances are that it won’t be detected, leaving a nasty leak while the programmer have been used not to care about those things. There are so many things other than memory that has to be managed, memory doesn’t have to be a special case. Examples: network connections or OS resource handles . Why assume the programmer is an idiot that needs to be protected from herself? Good design doesn’t prevail by limiting control, especially in a creative tool like a programming language.

The solution is to make the use of objects, both as value or reference, easy for general application and leave the door open for a meticulous management. Using value-based programming is easy in C++ and will continue to be in C3, thanks to RAII. Using pointers in C++ have a reputation of being complex, but it becomes an easy thing if you are using smart pointers. Most of the time, there is two ways we want our pointers to behave: non-copyable pointers, and reference-counting pointers. Non-copyable pointers simply disallow multiple pointers to the same object. Reference-counting pointers keep track of the number of pointers to the same object. Both know when the pointed object is no longer necessary and deletes it at this moment. No need for complex manual management! Another example is a clone pointer that makes a copy instead of pointing to the same item when copied. Other more custom pointers could also be used. This is where the serious programmer is free to push thing down to the metal.

Having smart pointers being part of the language standards allows making other simplification for the user. In C++, not redefining constructor, destructor, copy constructor can be a dangerous thing if your class contains pointers. But in C3, the default behavior will most probably be the one you need because the type of pointer you use define its behavior in those situation. Basic language functions won’t return raw pointers (like C++’s new operator). Other operators could be generated automatically correctly, like operator==, which isn’t the case in C++. My husband recently answered this question after seeing it on stackoverflow.

Why don’t C++ compilers define operator== and operator!= ?

IMHO, there is no “good” reason. The reason there are so many people that agree with this design decision is because they did not learn to master the power of value-based semantics. People need to write a lot of custom copy constructor, comparison operators and destructors because they use raw pointers in their implementation.

When using appropriate smart pointers (like boost::shared_ptr), the default copy constructor is usually fine and the obvious implementation of the hypothetical default comparison operator would be as fine.

So this is how memory is never stack as a pile of garbage, waiting for another process to collect it. It just manages itself in a very easy way and the serious programmer continue to be free to invent new ways to manage his pointers. Memory recycling itself without more efforts. The simple programs continue to be easy of implementation, and the programmer gets flexibility, performance and control for all possible applications.

Husband ‘s note:
C++ “concrete type” facilities were only optimized for small objects that are cheap to copy.
C3 makes value-based semantics easy and efficient, even for more complicated objects.

7 thoughts on “The value of values

  1. Are you implying that some are saying that value-based programming is a bad thing? Any reference to a rationale behind that?

    As for garbage collectors, please read my partial rebuttal. They sure have their downsides, I think you’re a little too harsh about them.

    One thing I’m wondering: are pointer to stack variables allowed in C3? And in that case, is there any provision to ensure the pointer doesn’t outlive the stack frame?

  2. @Michel Fortin
    I’m implying that some languages, like java, have completely wiped-out the possibility, and other are limiting it considerably. In the case of java, I feel it was mostly for the sake of purity, a contradiction because basic types can be used by value.

    The link to your article doesn’t seem to work, I believe it’s more

    You are right that I am harsh against garbage collectors. This is mostly a professional deformation because in my field, every single allocation call have to be optimized in order to be competitive. My husband leave open the implementation of a garbage collector, we just want to make sure the language can live without it.

    On your last question, I’ll let c3designer answer that!

  3. Most garbage collectors handle cycles just fine. The complexity of the cycle doesn’t matter. It is reference counting that can’t handle cycles (ex: boost::shared_ptr). Memory leaks can still occur in garbage-collected languages, but for a different reason.

    People who include garbage collection in a language don’t “assume the programmer is an idiot that needs to be protected from herself”. It’s a form of automatic memory management that makes a programmer’s life easier, allowing her to worry less about memory and focus on other things. Garbage collectors are not perfect, but nothing else is either.

  4. Honestly, I don’t have much experience with cycles myself, most applications I worked on had a basic need for manual memory management. So I may be wrong on that matter.

    I am not fundamentally against garbage collector, I perfectly understand those that use it, cannot blame someone to make it live easier if there is no drawback for him. What I stand against is languages that force it upon its users, and do not consider those that have different needs. I like to have the choice!

  5. As soon as I get some time to create some good looking examples 🙂 I would like to know what examples would be interesting to you (other than Hello world!)

    However, please keep in mind that I’m now working on a parser generator and there won’t be a C3 compiler anytime soon…

Leave a Reply

Your email address will not be published. Required fields are marked *