Defensive Programming
- Last UpdatedJul 18, 2023
- 2 minute read
Defensive programming is an approach to improve software and source code. It aims to improve general quality by reducing the number of software bugs. It promotes making the source code readable and understandable. It aims to make your code behave in a predictable manner despite unexpected input or user actions.
You should try to:
-
Verify that your code does not produce unexplained hardware alarms.
-
Check that denominators in division are not zero.
-
Check that array indexes cannot go out of range.
-
Check that arguments from external sources are valid.
-
Check that loop terminations are obvious and achievable.
-
Only write code once. If you find that two sections of code look identical or almost identical it is worth spending the time to re-write or re-design it. This will generally reduce the size of the code in question by a third or more, which reduces complexity and therefore maintenance and debugging time. An effective method to achieve this is to convert the identical code to a new function.
-
Do not access the module data in any function other than the member functions.
-
Write the member functions whenever an array is defined. Do not try to pass arrays between functions, make the arrays the centre piece of the object.
-
Cicode is a multitasking language. Several tasks (commands, expressions and tasks created by TaskNew function) can be executed concurrently. This powerful feature of Cicode should be used with care as some of the functions may be modifying module data. It is essential that the number of tasks running at any point in time be minimized. This may require the use of semaphores to help protect the module data from interference and corruption. (For the use of semaphores, refer to SemOpen, SemClose, SemSignal and SemWait functions in on-line help or the Cicode Reference manual).
|
|
|
UNINTENDED EQUIPMENT OPERATION
|
