Advantages:
Defensive programming can detect errors early in development.
Example code:
The following is the implementation code for the associated access method.
- (void) Settire: (Tire *) Tire Atindex: (int)Index{if(Index<0||Index>3) {NSLog (@"Bad Index (%d) in Settire:atindex:",Index); Exit (1); } tires[Index] = tire;} -(Tire *) Tireatindex: (int)Index{if(Index<0||Index>3) {NSLog (@"Bad Index (%d) in Tireatindex", Inddex); Exit1); }return(tires[Index]);}
The tire access method uses common code to check the array index of the tires instance variable to ensure that it is a valid value. If the array index exceeds the range of 0 to 3, the program outputs an error message and exits. This code is called defensive programming, which is a good programming idea. Defensive programming can detect errors early in development, such as indexing errors for tires arrays.
OBJECTIVE-C-----Defensive programming