Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



Defensive programming


Overview

To help 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. Casting discusses its use.

[Top]


Testing conditions with asserts and invariants

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.

Two mechanisms support this programming style.


Asserts

Two macros are supplied for asserting specific conditions in functions:


Class Invariants

Class invariants are used to test that an object is in a valid state. They are used only in debug builds.

[Top]


Casting

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.

The C++ dynamic_cast operator should not be used because Symbian OS does not use C++ exceptions in user code.

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 REINTERPRET_CAST, STATIC_CAST, CONST_CAST, and MUTABLE_CAST, which were defined as simple C style casts for that compiler.