<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="copyright" content="(C) Copyright 2010" />
  <meta name="DC.rights.owner" content="(C) Copyright 2010" />
  <meta name="DC.Type" content="concept" />
  <meta name="DC.Title" content="Defensive Programming" />
  <meta name="DC.Relation" scheme="URI" content="GUID-35D7EEFC-B2E4-5444-8875-2A24790E08C2.html" />
  <meta name="DC.Relation" scheme="URI" content="index.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-35D7EEFC-B2E4-5444-8875-2A24790E08C2.html" />
  <meta name="DC.Format" content="XHTML" />
  <meta name="DC.Identifier" content="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285" />
  <meta name="DC.Language" content="en" />
  <link rel="stylesheet" type="text/css" href="commonltr.css" />
  <title>
   Defensive Programming
  </title>
  <link type="text/css" rel="stylesheet" href="css/common.css" media="screen"/>
<link type="text/css" rel="stylesheet" href="css/sdl.css" media="screen"/>

<!--[if IE]>
<link href="css/iefix.css" rel="stylesheet" type="text/css" media="screen" />
<![endif]-->
  <link rel="stylesheet" type="text/css" href="nokiacxxref.css" />
 </head>
 <body id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285">
  <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285">
   <!-- -->
  </a>
  <?php include_once (CURRENT_SKIN_PATH.'/sdl_header.html'); ?>
  <div id="sdl_container">
   <div id="leftMenu">
    <div id="expandcontractdiv">
     <a id="collapseTree" href="javascript:tree.collapseAll()">
      Collapse all
     </a>
     <a id="index" href="index.html">
      Symbian^3 Application Developer Library
     </a>
    </div>
    <iframe style="border:none" height="800" width="300" src="index-toc.html"></iframe>
<div id="treeDiv1">
     &#160;
    </div>
    <script type="text/javascript">
     var currentIconMode = 0; window.name="id1197760 ";
	YAHOO.util.Event.onDOMReady(buildTree, this,true);
    </script>
   </div>
   <div id="sdl_content">
<div class="breadcrumb">
     <a href="index.html" title="Symbian^3 Application Developer Library">
      Symbian^3 Application Developer Library
     </a>
     &gt;
     <a href="GUID-35D7EEFC-B2E4-5444-8875-2A24790E08C2.html">
      Essential Idioms
     </a>
     &gt;
    </div>
    <h1 class="topictitle1">
     Defensive Programming
    </h1>
    <div>
     <p>
      To help Symbian developers identify potential problems early in development, macros are provided to test for error conditions in functions (asserts) and objects (class invariants). Casting is one well known source of hard-to-find errors.
      <a href="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285.html#GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-A0253DF3-098C-5427-9B45-3CEC4F4875AB">
       Casting
      </a>
      discusses its use.
     </p>
     <div class="section" id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-55EB9D07-65F1-572B-8F36-9A442F69B9C2">
      <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-55EB9D07-65F1-572B-8F36-9A442F69B9C2">
       <!-- -->
      </a>
      <h2 class="sectiontitle">
       Testing conditions with asserts and invariants
      </h2>
      <p>
       One method of catching errors early is to identify conditions that should be true at the beginning and end of functions, and raise errors if they are not.
      </p>
      <p>
       Two mechanisms support this programming style.
      </p>
      <ul>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-620C4F53-5465-5A53-9880-7DC27EF752FF">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-620C4F53-5465-5A53-9880-7DC27EF752FF">
         <!-- -->
        </a>
        <p>
         asserts
        </p>
       </li>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-9A1881EA-4C72-5359-9D16-553806070556">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-9A1881EA-4C72-5359-9D16-553806070556">
         <!-- -->
        </a>
        <p>
         class invariants
        </p>
       </li>
      </ul>
      <p>
       <strong>
        Asserts
       </strong>
      </p>
      <p>
       Two macros are supplied for asserting specific conditions in functions:
      </p>
      <ul>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-9481684E-5E9D-54B1-87E0-3539F4A29157">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-9481684E-5E9D-54B1-87E0-3539F4A29157">
         <!-- -->
        </a>
        <p>
         <samp class="codeph">
          __ASSERT_ALWAYS
         </samp>
         to catch run-time invalid input, for both release and debug builds
        </p>
       </li>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-4BAF939D-03D4-5D1E-A1BD-79537D930E93">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-4BAF939D-03D4-5D1E-A1BD-79537D930E93">
         <!-- -->
        </a>
        <p>
         <samp class="codeph">
          __ASSERT_DEBUG
         </samp>
         to catch programming errors, for debug builds only
        </p>
       </li>
      </ul>
      <p>
       <strong>
        Class Invariants
       </strong>
      </p>
      <p>
       Class invariants are used to test that an object is in a valid state. They are used only in debug builds.
      </p>
      <ul>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-F79328BA-61FD-5CB6-9D09-4C4DF6A2459B">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-F79328BA-61FD-5CB6-9D09-4C4DF6A2459B">
         <!-- -->
        </a>
        <p>
         Define class invariants for non-trivial classes using
         <samp class="codeph">
          __DECLARE_TEST
         </samp>
         . The class must supply functions that specify its allowed stable states.
        </p>
       </li>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-541758DE-21E9-5599-A12B-0208867240CA">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-541758DE-21E9-5599-A12B-0208867240CA">
         <!-- -->
        </a>
        <p>
         To ensures that the object is in a stable state prior to executing the function, call the invariant at the start of all public functions using
         <samp class="codeph">
          __TEST_INVARIANT
         </samp>
         .
        </p>
       </li>
       <li id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-20FFC383-0371-5494-8887-D0B3888162EE">
        <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-20FFC383-0371-5494-8887-D0B3888162EE">
         <!-- -->
        </a>
        <p>
         For non-const functions, you can ensure that the object has been left in a stable state by also calling the invariant at the end of the function.
        </p>
       </li>
      </ul>
     </div>
     <div class="section" id="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-A0253DF3-098C-5427-9B45-3CEC4F4875AB">
      <a name="GUID-D5FD665E-333B-50FF-A46F-6B22C0877285__GUID-A0253DF3-098C-5427-9B45-3CEC4F4875AB">
       <!-- -->
      </a>
      <h2 class="sectiontitle">
       Casting
      </h2>
      <p>
       Casts, as in other operating systems, should be used with caution. If a cast seems to be needed, check that this does not reflect a design weakness.
      </p>
      <p>
       The C++
       <samp class="codeph">
        dynamic_cast
       </samp>
       operator should not be used because the Symbian platform does not use C++ exceptions in user code.
      </p>
      <p>
       Note that in early versions of the OS (pre v6.0), the GCC compiler did not support the C++ casting operators. The idiom was then to use instead one of the macros
       <samp class="codeph">
        REINTERPRET_CAST
       </samp>
       ,
       <samp class="codeph">
        STATIC_CAST
       </samp>
       ,
       <samp class="codeph">
        CONST_CAST
       </samp>
       , and
       <samp class="codeph">
        MUTABLE_CAST
       </samp>
       , which were defined as simple C style casts for that compiler.
      </p>
     </div>
    </div>
    <div>
     <div class="familylinks">
      <div class="parentlink">
       <strong>
        Parent topic:
       </strong>
       <a href="GUID-35D7EEFC-B2E4-5444-8875-2A24790E08C2.html">
        Essential Idioms
       </a>
      </div>
     </div>
    </div>
    <p class="copyright">Copyright &#169;2010 Nokia Corporation and/or its subsidiary(-ies).<br /> All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the <a href=" http://www.eclipse.org/legal/epl-v10.html"> Eclipse Public License
v1.0</a>.</p> 
   </div>
  </div>
  <?php include_once (CURRENT_SKIN_PATH.'/sdl_footer.html'); ?>
 </body>
</html>