Part 2 - Chapter 8 Defensive Programming

책 정리/Code Complete 2nd 2009. 3. 9. 01:19
General methods to process garbage data or input.

1. To check all data from the outside.
2. To check all input parameters.
3. To decide how to process with incorrect input.

Assertion
It's a code to examine itself as a routine or macro.
If Assertion is true, it means everything works hos I expect.
If not, it'll assert to show there is an error in a program.

Ex in JAVA )  assert denominator != 0 : "denominator is unexpectedly equal to 0.";
-> If boolean expression is false, it'll show a message.

It's used to find out unexpected condition and assumptive things.
Ex ) 1. input or output is in expected range.
       2. File or Stream checks whether it's open or close.
       3. Either file or stream is opened as readable, writable, or readable/writable mode.
       4. Whether a pointer is null
       and so on...

The purpose is to be used during development or maintenance period for programmer.
Ex ) To make assertion macro in C++
#define ASSERT( condition, message ) { \
       if ( !(condition) ) { \
           LogError( "Assertion failed: ", \
           #condition, message ); \
           exit( EXIT_FAILURE ); \
       } \
 } \

A Guide how to use Assertion

1. Don't write executable code in Assertion.
Dangerous Ex ) Debug.Assertion( PerformAction() );
In this case, it can be unable to execute.
If you don't compile Assertion, code which is in Assertion isn't compiled.
Safe Ex ) actionPerformed = PerformAction()
              Debug.Assert( actionPerformed )
2. To use Assertion to check preceding and 후행 condition for class or routine.

3. To process an error after assertion.
If a variable is out of a range, you put a certain value.

Technique of dealing an error

1. To return a neutral value.
For incorrect data, it's good to procede works to return non-error value.
But it depends on a sort of programs.
Ex) The case of calculation, you return 0.
      The case of operation of string or characters, u return empty one.
      The case of a pointer, u return an empty pointer.
      The case of drawing routine, u draw previous or basic color.
      The case of X-RAY showing, u terminate a program. -> It's bad to show  
      incorrect value.

2. To return the same value as before or skip to next data.
While you read record, if u encounter broken data, you'll seek it over and over again.
You should seek next another data or return the same data as before.
Ex) Thermometer or the color of sth in game.
3. To make a error dealing object or routine like a function
Exception
1. Avoid throwing exceptions in constructors and destructors unless you catch them in the same place
if code within a constructor throws an exception, the destructor won't be called, thereby setting up a possible resource leak

'책 정리 > Code Complete 2nd' 카테고리의 다른 글

[Code Complete 2nd/e] Words - 1  (0) 2009.03.18
Code Complete - Eng Version Doc  (0) 2009.03.18
Part 2 - Chapter 6. Class Handling  (0) 2009.03.08

설정

트랙백

댓글