Sunday, April 12, 2009

Temporary variables – 5 common mistakes

Temporary variables are a type of variable which are used to store a value obtained from a particular computation or data source, which will be used in a later stage of the program. Although temporary variables serve very useful purposes, they can be the cause of major software bugs that seem to occur inexplicably. The common coding or programing mistakes associated with temp variables:

1. Loss of precision: The use of a lesser precision variable for a temp variable can lead to loss of precision which leads to bugs.

Ex: Using a Integer variable to store the result of a calculation that includes a higher precision like float or double. Even in langauages like java by explicitly typecasting(coders mistake).

2. Value out of bound: Similar to the first mistake but occurs in arrays and strings. Can mainly lead to rendering mistakes.

Ex:While using the last few characters of a string or last few values of an array.

3. Multiple inputs: The temporary variable value can get overwritten by incorrect code even before its value is used.

Ex: Status being changed even before the code required to be executed in previous status completes execution.

4. Excessive memory usage:The excessive use of temporary variables can have an impact on the memory usage of the program.

Ex:Using temp variables even when they are not absolutely needed.

5. No exception handling ( Like Null value, negative value) :The temporary variables can behave in weird ways if various exceptional cases are not anticipated and guarded against.

Ex:Not guarding against storing negative values in temp variables later used as array index etc.

No comments: