Programming Methodology
Introduction
Learning to write computer program is very much like learning any skill. First, we should understand the problems well and then try to solve it in a logical manner. For example: We have read many books available in the market for describing the car driving methods. However, we can learn driving once we actually get into the car and start driving it. The same logic is applied in computer programming also. Computer programming is the process of writing, testing, troubleshooting, debugging and maintaining of a computer program.
An effective program is that which gives result of all different inputs, including wrong input also. While creating program, we need to follow certain systematic approach. This systematic approach comprises two steps/things, viz., program structure and program representation. The program structure is implemented by using top-down or bottom-up approach and is known as ‘popular approach’, while the program representation plays an important role in making the program more readable and understandable.
What is a Good Program?
A Good Program means that it should produce correct and faster results, taking into account all the memory constraints. While making good program, we need to follow certain guidelines of programming language for creating a successful program. The following is the list of good programming habits that most people agree.
Clarity and Simplicity of Expression
Expressions are used to implement a particular task. It is a combination of Operators, Operands and Constants. Any expression used in the program should be understood by the user. The followings are some of the points to be kept in mind while using expressions in a program.
i. Use library functions to make programs more powerful
Example
To find output = x6
Output = X *X * X * X * X * X
We can use output = power (X, 6)
ii. Follow simplicity to maintain the clarity of expression
Example
X = A+B –U +VY
A-B X+Y
Then, we can write
X1 = (A+B) / (A-B)
X2 = (U+V*Y) / (X +Y)
X = X1 –X2
iii. Avoid program tricks usage, whose meaning is difficult to understand by the user.
Use of proper names for identifiers
Identifiers are user defined names. They are used to name things. A name is associated with a function or data object (constants and variables) and used to refer to that function or data object. Identifiers are made up of letters (A-Z, a-z), digits (0-9), and the underscore character ( _ ). They, however, must begin with a letter or underscore and not with a digit.
(i) Give meaningful name for variable (data –object) and function.
Example
To calculate Area of a Square
We use the variable names are Area and Side
Area = Side * Side.
(ii) Use proper names for constants.
Example
¶ = 3.14
Give Pi = 3.14
(iii) Do not use same name like custom, customer or account, accountant.
(iv)Do not use one letter identifiers.
Comments
A comment is a programming language construct, which is used to embed programmer-readable annotations in the source code of a computer program. Those annotations are potentially significant to programmers but typically ignorable to compilers and interpreters. Comments are usually added with the purpose of making the source code easy to understand. Hence, add comments to your code in simple English language that describes the function of the code and the reason for your decision to do it in a particular way as well. They are generally categorized as either ‘block comment’ or ‘line comment’.
Indentation
Leading white space (spaces and taps) at the beginning of each statement, which is used to determine the group of statement, is known as ‘indentation’.
Following example program makes use of comments and indentation to enhance the clarity and understanding of program.
//WAP to check if a string is palindrome or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char string[80],c;
cout<<"Enter string(max. 79 characters): ";
cin.getline(string,80);
//Loop to find the length of the string.
for(int len=0;string[len]!='\0';len++);
int i,j,flag=1;
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(string[i]!=string[j])
{
flag = 0;
break;
}
}
if(flag!=0)
cout<<"It is a palindrome.\n";
else
cout<<"It is not a palindrome.\n";
getch();
}
Characteristics of good programming
Every computer needs proper instruction set (programs) to perform the required/assigned task. The quality of the program depends upon the instructions given to it. However, it is required to feed/provide the proper and correct instructions to the computer in order to yield/provide a correct and desired output. Hence, a program should be developed to ensure proper functionality of the computer and also should be easy to understand. A computer program should have some important characteristics, which are as follows:
Flexibility
A program should be flexible enough to handle most of the changes without having to rewrite the entire program. A flexible program is used to serve many purposes. For example, CAD (Computer Aided Design) software is used for different purposes such as; engineering drafting, printing circuit board layout and design, architectural design, technical drawing, industrial art, etc. Most of the programs are being developed for certain period and they need updation during the course of time.
User Friendly
A program that can be easily understood by a beginner is called ‘user friendly’. It must interact with user through understandable messages. In addition, the proper message for the user to input data and to display the result, besides making the program easily understandable and modifiable.
Portability
Portability refers to the ability of an application to run on different platforms (operating systems) with or without minimal changes. Since the change of platform is a common phenomenon nowadays, due to the developments in hardware and the software, portability has to be taken care of it. In case, a program is developed for a particular platform, it would become obsolete after a certain period of time. At the same time, if a program that is developed does have the ability to work on different platforms, it makes software more useable. High language programs are often more portable than assembly language programs.
Reliability
It is the ability of a program to do its intended function accurately even if there are even small changes in the computer system. Moreover, the program must be able to handle unexpected situation like wrong input or no input. The programs, which save such ability, are known as ‘reliable’. For example, if the user does/gives wrong information to input, it should display a proper error message.
Self-Documenting Code
A source code, which uses suitable name for the identifiers (variables and methods), is called self-documenting code. Also, giving proper name for variables and methods would tell the reader of your code clearly -what is it doing? Hence, a good program must have a self-documenting code.
Problem solving process
The problem solving process starts with the problem specifications and ends with a concrete (and correct) program. Programming means a problem solving activity, which consists of four steps. They are;
(i) Understanding the problem;
(ii) Devising a plan;
(iii) Executing the plan; and
(iv) Evaluation
Understanding the problem
The first step is to understand the problem well. It may be very difficult to understand the problem but it is crucial. In general, one must find out the output from the given data (input data) and assess the relationship between input and output data. It is also important to verify whether the given information is sufficient to solve the problem or not.
Devising a plan
It means drawing an action plan to solve the problem, once understood. A plan is devised from data processing to the result according to the relationship that links both of them. If the problem is trivial, this step will not require much thinking.
Executing the plan
Once the plan is defined, it should follow the plan of action completely and each element of the plan should be checked as it is applied. In the course of execution, if any part of the plan is found to be unsatisfactory, the plan should be revised.
Evaluation
Finally, the result should be examined in order to make sure that it is valid and that the problem has been solved completely.
Problem solving methodology
As we all know, there are many methods/approaches available to solve a particular problem. However, the efficient way is to adopt a systematic method of problem solving. The use of systematic method of problem solving is crucial when we use a computer to solve a problem. We introduce here a seven steps problem solving method, which is closely related to the software life cycle (the various stages in the life of a program), that can be adapted by each person to solve the problem in their own style. They are given as under:
1. Problem Definition
2. Problem Analysis
3. Design the problem
4. Coding
5. Program Testing and Debugging
6. Documentation
7. Program Maintenance
Problem Definition/Specification (Theme)
Computer programs are written to solve problems posed by humankind. Prior to writing a program, one has to understand a description of the problem to solve. This description may be very precise or vague, but nevertheless, it is necessary/present. For instance, if you want to write a program to “Find the average of five numbers”, you should ask yourself:
“What does average mean exactly?”
“How to calculate average value?”
Posing such questions compels you to define the problem very precisely. Once you are sure of what the problem entails, you must write down a list of specifications. Specifications are precise definitions of what the program must do. It must include the following at least:
1. Input: what data must be included as input and in which form?
2. Output: what data must the program produce and in which form? (in order to solve the problem)
Note: At the end of the problem definition step, you should have a list of specifications.
Problem Analysis
In this step, the problem has to be fragmented into smaller and manageable parts. The original problem has to be analyzed and divided into a number of sub-problems as these sub-problems are easier to solve and their solutions would become the components of the final program. Each sub-problem is divided into further smaller ones and this fragmentation has to be continued to achieve simple solutions. The use of modular programming is to get proper solution.
Modular Programming: Modular Programming is the act of designing and writing programs as functions (a large program is divided into the small individual components) that each one performs, a single well-defined function, which has minimal interaction between the sub-programs. It means that the content of each function is cohesive and there is low coupling between them. There are two methods available for modular programming. They are: top-down design and bottom-updesign.
Top-Down design: The principles of top-down design dictate that a program should be divided into a main module and its related module. Each module should also be divided into sub modules according to software engineering and programming style. The division continues till the module consists only of an elementary process that is intrinsically understood and cannot be further sub-divided.
Bottom-up design: Bottom-up design is just the opposite of top-down design. It refers to a style of programming, in which, an application is constructed with existing primitives of the programming language and then gradually more and more complicated features are added till applications are written. In other words, initiating the design with simple modules and then build them into more complex structures ending at the top is bottom-up design.
Designing the problem
Designing the problem can be expressed in the form of
Ø Algorithm
Ø Flowchart
Algorithm: An algorithm is a set of instructions that describe a method for solving a problem. It is normally given in mix of computer code and English language. This is often called ‘pseudo-code’.
Flowchart: The algorithm is represented in the form of a diagram with action boxes linked by lines showing the order in which they are executed. This is known as „the flow of control‟. It is the diagrammatic representation of an algorithm.
Coding
The process of translating the algorithm into syntax of a given language is known as ‘Coding’. Since algorithm cannot be executed directly by the computer, it has to be translated into a programming language.
Program Testing and Debugging
Program Testing means running the program, executing all its instructions/ functions and testing the logic by entering sample data in order to check the output. Debugging is the process of finding and correcting the errors in the program code.
Type of errors: There are three types of errors which generally occur during compilation and running a program. They are (i) Syntax error; (ii) Logical error; and (iii) Runtime error.
Syntax error: Every programming language has its own rules and regulations (syntax). If we overcome the particular language rules and regulations, the syntax error will appear (i.e. an error of language resulting from code that does not conform to the syntax of the programming language). It can be recognized during compilation time.
Semantics error: Semantics errors occur when statements are not meaningful. Semantics refers to the set of rules which give the meaning of a statement. For instance, the statement
X*Y=Z;
will result in a semantical error as an expression cannot come on the left side of an assignment operator.
Logical error: Programmer makes errors while writing program that is called ‘logical error’. It is an error in a program’s source code that results in incorrect or unexpected result. It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running. The logical error might only be noticed during runtime, because it is often hidden in the source code and are typically harder to find and debug.
Runtime error: A runtime error is an error that causes abnormal termination of program during running time. In general, the dividend is not a constant but might be a number typed by you at runtime. In this case, division by zero is illogical. Computers check for a “division by zero" error during program execution, so that you can get a "division by zero" error message at runtime, which will stop your program abnormally. This type of error is called runtime error.
Documentation
The documentation includes the problem definition, design documents, a description of the test perform, a history of the program development and its different versions and a user’s manual. Such a manual is designed for a naive user and illustrates the preparation of input data, running the program and obtaining & interpreting the results.
Program maintenance
It is not directly part of the original implementation process, but needs special emphasis. All activities that occur after a program operation are part of the program maintenance. Many large programs have long life span that often exceed the lifetime of the hardware they run on. Usually, the expenditure for the program maintenance will be more than the developmental cost of the program. The program maintenance includes the following:
Ø Finding and eliminating previously undetected program errors;
Ø Modifying the current program, often to improve its performance, or to adapt to new laws or government regulations, or to adapt to a new hardware, or to a new operating system;
Ø Adding new features or a better user interface, or new capabilities to the program; and
Ø Updating the documentation.
Maintenance is an important part of the life cycle of a program. It is also important as far as documentation is concerned, since any change pertaining to a program will require updating of internal as well as external documentation. Maintenance documentation will include results of the program development steps, design documents, program code and test information.
No comments:
Post a Comment