C++
  Home arrow C++ arrow Page 3 - Who`s Afraid to Be Const Correct? Help You...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Who`s Afraid to Be Const Correct? Help Your Compiler Help You
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-08-30

    Table of Contents:
  • Who`s Afraid to Be Const Correct? Help Your Compiler Help You
  • The Compiler is Your Friend
  • Why Use the Compiler to Catch Errors?
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway