Home arrow C++ arrow Page 3 - Who`s Afraid to Be Const Correct? Help Your Compiler Help You
C++

Who`s Afraid to Be Const Correct? Help Your Compiler Help You


In programs and commercial libraries that are not const-correct, you may find that const_cast is used. While a programmer may do this to force an application to do what he wants it to, it is never a good idea. Jun Nakamura explains why.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
August 30, 2005
TABLE OF CONTENTS:
  1. · Who`s Afraid to Be Const Correct? Help Your Compiler Help You
  2. · The Compiler is Your Friend
  3. · Why Use the Compiler to Catch Errors?
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Who`s Afraid to Be Const Correct? Help Your Compiler Help You - Why Use the Compiler to Catch Errors?
(Page 3 of 4 )

It would be wonderful if you were able to write bug-free code 100% of the time, but I don’t know any programmers who can (maybe I just hang out with the wrong people ). Even though compiler errors can lead you in the wrong direction, they will still point you to the location where there is a problem with your code. These days I just take note of the line number mentioned in the error message and try to see if I can find the problem on my own. Only when I have problems spotting the error, do I look at what the compiler thought was wrong with it. You could say this is my conditioned response after having too many fits with the compiler pointing me in the obvious wrong direction.

Scripts are great when you need quick mockups or tools, and I use Python and Lua a lot. But because they are weakly typed, errors that would have been caught by a C++ compiler go unnoticed by their weakly typed script compilers/interpreters. To discover a bug during runtime while trying to demonstrate something to your manager is rather painful. Somehow this seems to happen a lot. Everything works fine until somebody asks you to do a demonstration.

It is much better to find errors during compile time than it is to find them during run time. Since you can run a program many times without touching all the code, it is possible for a run time bug to surface only at a late stage; compile time bugs are found every time you compile.

Constant member functions are a good example of coding contracts that enable the compiler to find inconsistencies in your design and/or implementation. This is one of the reasons that it can be so painful to introduce const at a late stage of a project… it makes it clear that pieces of code are being used the wrong way. 

Maybe it wasn’t visible that the contracts were being broken, and maybe the application appears to be working fine. Still I can guarantee that if you continue without making it const correct, damage will be done for real at some point. And finding the root of the bug will be very difficult at that time.

So take a closer look at code that uses const_cast to remove const from variables or parameters. Whoever decided to use the const_cast wanted to work around an error the compiler would otherwise spit out. He is trying to work around a contract that he clearly is violating!

It is always very interesting to see which coders are ignoring compiler warnings.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials