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

설정

트랙백

댓글

Part 2 - Chapter 6. Class Handling

책 정리/Code Complete 2nd 2009. 3. 8. 17:06

Abstract Data Type

Once you understand ADT, you can make easy class to make in the beginning and to change in the future.

ADT is not int or float type which are defined ( or fixed ). It's a data type whice is made by developer such as struct or class.

 

The advantages :

  1. Changing doesn't influence the whole program.

  2. It's easy to advance a program.

  3. Apparently It's easy to understand. and so on...

 

Better Class Interface

  1.  To make consistent interfaces.

    Inappropriate Example

    1. class EmployeeCensus : public ListContainer{
    2. public:

    3. void AddEmployee( Employee employee );

    4. void RemoveEmployee( Employee employee );

    5. Employee NextItemInList();

    6. Employee FirstItem();

      Employee LastItem();

    7. }

In this case, 2 functions are about Employee. 3 functions are about List. It's not consistent abstract.

 

  Appropriate Example

  1. class EmployeeCensus : public ListContainer{
  2. public:

  3. void AddEmployee( Employee employee );

  4. void RemoveEmployee( Employee employee );

  5. Employee NextEmployee();

  6. Employee FirstEmployee();

    Employee LastEmployee();

  7. private:

  8. ListContainer m_EmployeeList;

  9. }

The Abstraction is all about Employee.

2 To provide a pair of service which has an opposite function.

 If there is a function which turn on the radio, we might need a function which turn off the radio.

But make sure that the opposite function is needed. So you should check it.

 

  1. During maintenance, be careful to mess abstraction.

    SqlQuery GetOueryToCreateNewEmployee() const; ~~~

    Its' level of abstraction is lower then Employee Class's. It devastates an abstraction.

 

To persue the convenience of reading not the convenience of writing codes.

It takes more time to read codes than to write codes

 

If you make a program through interfaces, It's gonna break capsulation. then It's gonna break abstraction as well.

-> make a program through priavate functions.

 

To be careful the class including more than 7 data members.

If there is a class which has more than 7 data members, you consider whether u should devide the class.

 

To design for inheritance. If not, make it impossible to inherit. : C++ -> non-virtual, Java -> final

 

To be doubt if there is base class which has a  derived class. Make it simple to a class.

 

If there is a class which has functhion doing nothing, u should change the origin.

ex) Cat Class and Scratch() <- ScratchlessCat and Scratch()- doing nothing.  : It would be a problem.

     Cat Class includes Claws Class. It's better.

 

To make data private instead of protected. - Joshua Bloch said "Inheritance break capsulation"

 

The case of using switch ~ case , U should use it when cases are about totally different classes or actions.

 It's an example that seems to be changed to polymorphism.

  1. switch( shape.type ){

  2. case Shape_Circle:

  3. shape_DrawCircle();

  4. break;

  5. case Shape_Square:

  6. shape.DrawSquare();

  7. break;

  8. ...

  9. }

 

 It's an example that seems unable to be changed to polymorphism.

  1. switch( ui.Command() )

  2. {

  3. case Command_OpenFile:

  4. OpenFile();

  5. break;

  6. case Command_Print:

  7. print();

  8. break;

  9. ....

  10. }

 

 instance => which has an object.

If there is a class without members and with only actions, u should avoid to make that class.

 

In JAVA, All rutines are possible to override and to prevent inheritance to use a final routine.

In C++, basically it's impossible to override. u should  선언 virtual for inheritance.

'책 정리 > 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 8 Defensive Programming  (0) 2009.03.09

설정

트랙백

댓글