code robustness and const keyword

Jun 21, 2016 at 7:46pm
This question is out of place: the thread was closed before I noticed it! Sorry.

IDM says:

Use const wherever you can get away with it - it's a big advantage in making more robust code.


I am really interested in why it can make more robust code. This is a skill set I'd like to develop.

Thank you
Jun 21, 2016 at 9:43pm
By using const keyword with an argument, you're telling the compiler you promise you're not going to modify a particular argument. Likewise, if you declare a member function of a class to be const, you're telling the compiler that you're not going to modify any variable members of the class.

In either case, the compiler will detect if you violate that promise and generate an appropriate error.
Jun 22, 2016 at 1:46am
If you don't violate is the performance significantly increased by using const?
Jun 22, 2016 at 2:21am
Performance is not changed by using const. However passing complex classes by reference instead of by value dramatically increases performance. Passing variables using the const qualifier prevents you from accidentally modifying the variable.
Last edited on Jun 22, 2016 at 2:22am
Jun 22, 2016 at 2:32am
Well, in some cases the compiler can provide optimizations that it wouldn't have been able to otherwise, which can improve performance. I wouldn't bank on that, though.
Last edited on Jun 22, 2016 at 2:32am
Jun 22, 2016 at 6:50pm
Some other basic ideas about robust code.

An overall thought is: What are all the possible things that can go wrong?

A basic thing is to always ensure that division by zero doesn't happen. Other things to think about is over and underflow, sometimes you might be able to code to prevent that.

Another thing is to make use of types to help enforce something, as in using an unsigned type for values which are always positive. Seems obvious, but I see beginners use int when the shouldn't.

There is a thing called contract programming - it has two parts: a precondition for a function; and a post-condition.

So a simple example might be given the area of a circle, find it's radius. What precondition can we have there? There is a pretty trivial post condition as well.

More to the point, how are we going to enforce the precondition? We could put a comment at the beginning of the function, but that by itself is fairly useless. A much better idea is to, as well, have code that enforces the precondition, so in this case it would the use of std::abs. It's good to leave the comment in, so some eager beaver doesn't delete the code - not realising why it is there. Another thing to do is have a static_assert to prove that it is valid. static_assert is a very good thing you should use it a lot.

The same idea as preconditions occurs in C++ classes. But here it is the idea of a class invariant. That is all the member variables have to have sensible values in order for them to work with each other. For example the angles of a triangle must add to 180 degrees, and the lengths of the sides must be capable of making a triangle; or the points on a circular arc must make sense with the radius, and can't be in a straight line. Preconditions must be checked upon construction and every time after the value of any member variable changes.

Support for pre and post conditions and invariants in C++ isn't great at the moment, but it's coming. It's called concepts. The boost library already has them.

Hope this helps :+)
Topic archived. No new replies allowed.