Sell me const-correctness

This is the definitive article on “const correctness”: https://isocpp.org/wiki/faq/const-correctness. In a nutshell, using const is good practice because… It protects you from accidentally changing variables that aren’t intended be changed, It protects you from making accidental variable assignments. For instance, you are protected from if( x = y ) // whoops, meant if( x == … Read more

What are the use cases for having a function return by const value for non-builtin type?

Basically, there’s a slight language problem here. std::string func() { return “hai”; } func().push_back(‘c’); // Perfectly valid, yet non-sensical Returning const rvalues is an attempt to prevent such behaviour. However, in reality, it does way more harm than good, because now that rvalue references are here, you’re just going to prevent move semantics, which sucks, … Read more

“const correctness” in C#

I’ve come across this issue a lot of times too and ended up using interfaces. I think it’s important to drop the idea that C# is any form, or even an evolution of C++. They’re two different languages that share almost the same syntax. I usually express ‘const correctness’ in C# by defining a read-only … Read more