Thursday, October 1, 2020

Programming Methodology

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. 
  • 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 :- 

1. 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.
2. 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.
3. Use of Comments:  

  • A comment is a programming language construct, which is used to embedprogrammer-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‟. Block comment is implemented in python by “”” and “”” and line comment is implemented by #.
4.  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‟.
Example
If A > B :
        print "A is Big‟ # Block1
else:
        print "B is Big‟ # Block2


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:-

1. Flexibility : 

A program should be flexible enough to handle most of the changes without having to rewrite the entire program.

2. 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.

3. 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.

4. 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.

5. Self-Documenting Code :

The 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 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

1. 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?”
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:
  • Input: what data must be included as input and in which form?
  • Output: what data must the program produce and in which form? (in order to solve the problem)

2. 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-up design.
  • 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.
3. Designing the problem

Designing the problem can be expressed in the form of
  •  Algorithm
  •  Flowchart
4. 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.

5. 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 generally occur during compilation and running a program. They are (i) Syntax error; (ii) Logical error; and (iii) Runtime error.
  • 1. 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.
    Example
    a = 0
    while a < 10
    a = a + 1
    print a
    In the above statement, the second line is not correct. Since the while statement does not end with ":‟. This will flash a syntax error.

  • 2. 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.    a = 100
                     while a < 10:
                            a = a + 1
                           print a
      In the above example, the while loop will not execute even a single time,   because the initial value of "a‟ is 100.

  • 3.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.
  • Example
     (a) A=10
           B=0
           print A/B
     (b) During running time, if we try to open a file that does not exist in the                 hard disk, then it will create runtime error.

6. 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.

7. 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.


No comments:

Post a Comment