Computer Concept & Programming In C - Unit I - 9

Q.18 What are the ways to design and implement correct, efficient and maintainable programs?
Related Questions -
Q. Explain the efficient programming.                                                       (AKTU. 2011 - 12)
Ans. A good design and a good implementation of a program is a combination of three features
(i) Correct design and implementation
(ii) Efficient design and implementation
(iii) Maintainable design and implementation.
We now discuss each of them in detail.
(i) Ways For Correct Design and Implementation: -
The design and implementation of a program is said to be correct if and only if for every valid set of input values which satisfies certain predefined condition known as precondition the program gives a set of valid output values which also satisfies some condition known as post condition.
The following steps help in making the design and implementation of a program correct.
1. Remove all kinds of lexical and syntactic errors after identifying each of them.
2. Remove all kind of logical errors, semantic errors after identifying each of them.
3. Make sure that program always accomplish the required task for which it is designed. That is it adheres to functionality of program.
Ex : A program to print prime number should print only prime numbers and no such number which is odd but not prime. If such thing happens the program is said to be functionaly incorrect.
4. Test the program for a large number of test cases designed. A test case is a pair of input and corresponding desired output. Also, the number of test cases should be as large as possible.
5. Ensure that the program never falls into an abnormal condition and provide ways to handle such conditions.
For Example : Division by zero is an abnormal condition and any division a/b should be performed only after checking (b ¹ 0) condition is TRUE.
(ii) Ways For Efficient Design And Implementation: -
A program that is correct may or may not be efficient. A program is required first by to be correct and secondly to be efficient.
A program is said to have an efficient design and implementation if and only if it is correct and accomplish the required task by making use of minimum possible time resources and hardware resources.
For Example : A program to sort 100 numbers that takes 2hrs of time and 10MB of memory may be correct but is said to have an inefficient design and implementation.
In contrast another program to sort 1000 numbers that takes 10-4s and 1MB of memory is an efficient program.
Thus in order have an efficient design and implementation of a program one has to follow these steps below.
1. Minimise Memory Requirements and Other Hardware Resource Requirements: -
(i) Try to declare minimum variables.
(ii) Try to use minimum data structures.
(iii) Avoid using recursive functions (the functions which are defined in terms of itself).
(iv) Prefer using dynamic memory allocation (malloc ( ) and free ( ) functions) rather than using arrays which is static memory allocation.
(v) Try to declare variable only in the block where it is required.
For Example :
void main ( )
{
     int i;
     for (i = 0; i < 10; i++)
     :
} -------
The above program defines scope of variable  throughout the main function. However variable  is required only inside for loop so it can be declared as below :
void main ( )
{  ---------
      for (int i = 0; i < 10; i++)
   -------
   -------
}
The above program defines scope of variable  only within for loop. So this is a better way.
2. Minimise Computation Time Requirement: -
Computation time or running time of a program or an algorithm is the time required by that program in order to complete the desired task.
The running time of an algorithm is denoted by T(n) and is dependent on the number of input or input size . Generally T(n) increases with . 
For Example : A sorting program / algorithm will take less time to sort 100 number and more time to sort 1000 numbers.
Let T(n) be running time and
n1 = 10 and n2 = 1000
\ T(n1) < T(n2) Q n1 < n2
This is true for all programs that solves a real world problem.
Moreover the following steps should be taken for an efficient design and implementation of the program.
1. Minimize the total number of loops like for loop while loop or do-while loops in a program.
2. Minimize the total number of nested loops the programs.
3. The following strategy specialized algorithms takes increasing time in most of the cases
(i) Branch and Bound (Minimum Time) (ii) Greedy algorithm
(iii) Dynamic programming (iv) Divide and conquer
(v) Iterative (Maximum Time)
So if the program is based on divide and conqver method then it takes more time than if that program is based on dynamic programming strategy.
Thus the underlying algorithm on basis of which program is designed matters a lot.
4. The most suitable structure should be designed for condition checking.
For Example : Switch-case construct is better than if-else construct where one out of many conditions is executed.
(iii) Ways For Maintainable Design and Implementation: -
A program is said to have a maintainable design and implementation if and only if it can be modified from time-to-time to remain updated to solve never problems and that too more efficiently.
Following steps are required for having maintainable design and implementation.
(i) The program should be well documented.
(ii) Comments must be there to explain the purpose of every function call, loop, condition checking etc. These things make a program more comprehending.
(iii) The design of the program should consist of small modules whose functions are well defined instead of having one large program.
(iv) The use of goto statement should be avoided.
(v) The use of recursive functions should be avoided. Both goto statement as well as recersive functions make a program more difficult to understand and hence reduce maintainability.
Note : It is often the case that improving one out of these three factors often leads to the degradation of other two factors. So it is left to the choice of programmer to select the most suitable trade-off between these three criteria namely, correctness, efficiency and maintainability of program.