Computer Programming
Computer Programming
CHAPTER 1
• Concept of programming
• Features of a good program
• Systems development cycle.
2
[Type the document title]
Concept of programming
A program is a set of instructions that tells the computer what to do. Computer programming (often
shortened to programming or coding), is the process of writing, testing, debugging/troubleshooting and
maintaining act of instructions (source code) for solving a problem with the computer. A source code is
written in an acceptable computer programming language. The code may be a modification of an existing
source or something completely new.
The purpose of programming is to create a program that exhibits a certain described behavior
(customization). The process of writing source code requires expertise in many different subjects,
including knowledge of the application domain. Alternatively. Programming is the craft of transforming
requirements into something that a computer can execute. Problem solving on computer is a task of
expressing the solution to the problem in terms of simple concepts, operations and computer code
(program) to obtain the results. To achieve this aim, you may proceed as follows.
1. First, understand the problem clearly:- Decide what you want to be calculated by the computer.
What will be the input data required? (if any). This is the problem formulation.
2. Write the steps of computation that are necessary to arrive at the solution. This is setting up the
algorithm.
There is an ongoing debate on the extent to which the writing of programs is an art, a craft or an
engineering discipline. Good programming is generally considered to be the measured application art, craft
and engineering, with the goal of producing an efficient and maintainable software (program) solution.
The discipline differs from may other technical professions in that programmers generally do not need to
be licensed or pass any standardized (or governmentally regulated) certification tests in order to call
themselves “programmers” or even “software engineers”
3
[Type the document title]
5) Error-Resistant Operations:
A good program should be designed in such a way that it can perform validation run on each
input data to determine whether or not they meet the criteria set for them. Eg
Reasonableness check, Existence check, Dependency check, etc.
6) Maintainable Code:
A good program design will always be easy to change or modify when the need arises.
Programs should be written with the maintenance activity in mind. The structure, coding and
documentation of the program should allow another programmer to understand the logic of the
program and to make a change in one part of a program without unknowingly introducing an
error in another part of the same program.
4
[Type the document title]
7) Portable Code:
A good program design will be transferable to a different computer having a language
translator for that language without substantial changes or modification
8) Readability:
The program codes will be easy for a programmer to read and understand the logic involved in
the programming.
9) Storage Saving:
A good program design is not to be verbous, that is, it will not be allowed to be unnecessary
long, thereby consuming much storage that will be required for processing data and storage
of information produced from processing.
10) Efficiency:
The amount of system resources a program consumes (processor time, memory space, slow
devices, network bandwidth and to some extent even user interaction), the less the better.
11) Robustness:
How well a program anticipates situations of data type conflict and other incompatibilities that
result in run time errors and program halts. The focus is mainly on user interaction and handling of 12)
Usability:
The clarity and intuitiveness of a programs output can make or break it’s success. This involves
a wide range of textual and graphical elements that makes a program easy and comfortable to
use.
System Development Cycle
Most IT projects work in cycles. First, the needs of the computer users must be analyzed. This task is
often performed by a professional Systems Analysts who will ask the users exactly what they would like
the system to do, and then draw up plans on how this can be implemented on a real computer based system.
The programmer will take the specifications from the Systems Analyst and then convert the broad
brushstrokes into actual computer programs. Ideally at this point there should be testing and input from the
users so that what is produced by the programmers is actually what they asked for.
5
[Type the document title]
Finally, there is the implementation process during which all users are introduced to the new systems,
which often involves an element of training.
Once the users start using the new system, they will often suggest new improvements and the whole
process is started all over again.
These are methodologies for defining a systems development cycle and often you will see four key stages,
as listed below.
Feasibility Study
Design
Programming
Implementation
CHAPTER 2
6
[Type the document title]
To understand:
Concept of Algorithm
An algorithm is a set of instructions to obtain the solution of a given problem. Computer needs precise and
well-defined instructions for finding solution of problems. If there is any ambiguity, the computer will not
yield the right results. It is essential that all the stages of solution of a given problem be specified in
details, correctly and clearly moreover, the steps must also be organized rightly so that a unique solution is
obtained.
Features of an Algorithm
1. It should be simple
2. It should be clear with no ambiguity
3. It should head to unique solution of the problem
4. It should involve a finite number of steps to arrive at a solution
5. It should have the capability to handle unexpected situation.
Pseudo code
A pseudo code is the English-like representation of the program logic. It does not make use of
standard symbols like the flowchart. It is a sequential step by step arrangements of the instructions
to be performed to accomplish a task. It is an informal and artificial language that helps
programmers develop algorithms.
Example 1
Write a pseudo code for findings the area of a room.
Solution:
• Begin process
• Input room length
• Input room width
• Multiply length by width to get area
• Print area
• End process
Example 2
Write a Pseudo code for finding the greatest of 3 numbers represented as A, B, and C.
9
[Type the document title]
Solution
• Begin process
• Input A,B,C
• If A>B then big = A
• Else big = B
• If big >C then bigst = big
Else bigst = C
Example 3
Write an Algorithm to determine a student’s final grade and indicate whether it is passing or failing.
The final grade is calculated as the average of four marks.
Solution
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4.
• If average is below 50 Print “Fail” else
Print “Pass”
10
[Type the document title]
CHAPTER 3
To understand:
English-like form
The English form of representing as algorithm entails breaking down the solution steps of the problem into
single and sequential English words. The steps are represented in English to say what action should be
taken in such a step.
Example 1
Develop an algorithm to obtain a book on computer from your school library located on the fourth floor of
the building. You are to proceed to the library from your ground floor classroom.
1. Start from the classroom
2. Climb the stairs to the 4th floor and reach the library
3. Search a book on computer
4. Have the book issued 5. Return to your classroom
6. Stop.
Note: The above algorithm solution of example 1, has been written in simple and clear English way.
There is no
Example 2
Develop an algorithm to find the average of four numbers stored in variables A,B,C,D
Solution 1. Start
2. Read values in variables A,B,C,D
3. Calculate the average as (A+B+C+D)/4 and store the result in P.
4. Write the value stored in P
5. Stop.
Example 3
Develop and algorithm to find the average of four numbers stored in variables A, B, C, D. When the value
of variable A is zero, no averaging is to be done.
Solution
1. Start
2. Read values stored in variable A,B,C.D
3. If the value of A is Zero, then jump to step 6
4. Calculate the average of A, B, C, D and store the result in variable P.
12
[Type the document title]
Flowchart
Flowchart is a representation of the algorithm using standard symbols. Each symbols has a new function.
The Algorithm steps determine which symbol to use to represent it in the flow each step is linked to
another step by using the directional arrows.
Example
Solution
(Pseudocode)
• Input the length in feet (Lft)
• Calculate the length in an (LCM) by multiplying LFT with 30
• Print Length in Cm (LCM)
Start
Input Lft
LCM ← Lft * 30
Print
LCM
Stop
14
[Type the document title]
CHAPTER 4
To understand:
Decision table
A decision table is a form of truth table that structures the logic of a problem into simple YES and No form.
It is easily adapted to the needs of business data processing. It is a rectangle divided into four sections
called quadrants. It provides a structure for showing logical relationships between conditions that exist and
actions to be taken as a result of these conditions.
The quadrants of a decision table are called the condition steeb, the condition entry, the action stub and the
action entry respectively.
1 Condition stub:- This gives a list of all the conditions that are relevant to the system.
2 Condition entry: - Shows a YES or NO entry (abbreviated to Y for YES and N for NO) whether listed
condition is present or absent.
3 Action stubs: - This quadrant gives a list of all the actions that could be taken by the system, based on
the conditions.
4 Action entry: - This quadrant indicates whether a specific action will be taken or will not be taken .
You are regarded to construct a limited entry decision table to describe the above process.
SOLUTION
n= 3 2n = 23 =8
Cash sales ? Y Y N N Y Y N N
Cash sales > #1000 ? Y N Y N Y N Y N
Action stub Action entry offer
5% discount X X
.. 10% .. X
.. 15% .. X
.. no discount X X X X
Condition stub Condition entry
Existing customer Y Y Y N
Cash sales Y Y N -
Sales > #1000 Y N - -
Action stub Action entry offer
of 5% discount X
.. 10% ..
X .. 15% .. X
no discount X
(x + y ) * (w + Z)
Sum
y
Multiply
x Sum1 Answer
w Sum 2
Sum
z
19
[Type the document title]
CHAPTER 5
To understand:
Definition of flowchart
Description of flowchart symbols
Solving simple programming table with flowcharts
20
[Type the document title]
The flowchart
A flowchart is a pictorial representation of an Algorithm or of the plan of solution of a problem. It
indicates the process of solution, the relevant operations and computations, point of decision and other
information that are part of the solution. Flowcharts are of particular importance for documenting a
program. Special geometrical symbols are used to construct flowcharts. Each symbol represents an
activity. The activity could be input/out of data, computation/processing of data, taking a decision,
terminating the solution, etc. The symbols are joined by arrows to obtain a complete flowchart.
Example 1
Draw a flowchart to find the average of four numbers stored in variables A,B,C,D
22
[Type the document title]
Solution
Start
Read numbers in
A,B,C,D
Stop
Example 2
Draw a flowchart to find the average of four numbers stored in variables A,B,C,D. when the value of A is
zero, no averaging is to be done.
23
[Type the document title]
Solution
Start
READ values
For A,B,C,D.
Is
A=0
?
Stop
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
Solution
Pseudo code
Algorithm
Step 1: Input W, L
Step 2: A← L *
W Step 3: Print A.
25
[Type the document title]
Flowchart
Stop
Input
W,L
A←L*W
Print A
Stop
Example 5
Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation. ax 2
+ bx + c = 0
Hint: d = SQrt(b2 – 4ac), and the roots are:
X1 = (-b + d)/2a and X2 = (b – d)/ 2a
Solution Pseudo code
Algorithm
Step 1: Input a, b, c
Step 2: d ← Sqrt (b * b – 4 * a * c)
Step 3: X1 ← ( - b + d)/ (2 * a)
26
[Type the document title]
Step 4: X2 ← (- b – d)/ (2 * a)
Step 5: Print X1, X2
Flowchart
Start
Input
a, b, c
d ← Sqrt + (b * b – 4*a*c
X2 ← (-b – d) / (2* a)
Print
X1, X2
Stop
Uses of flowcharts
1 It gives us an opportunity to see the entire system as a whole.
2 It makes us to examine all possible logical outcomes in any process.
3 It provides a tool for communicating i.e a flowchart helps to explain the system to others.
4 To provide insight into alternative solutions.
5 It allows us to see what will happen if we change the values of the variable in the system.
CHAPTER 6
To understand:
All computer programs can be coded using only three logic structures (or programs) or combinations of
these structures:
1. Simple sequence
2. Selection
3. Repetition
The three structures are useful in a disciplined approach to programming because
[Link] program is simplified. Only the three building blocks are used, and there is a single point of entry
into the structure and a single point of exit.
2. The three coding structures allow a program to be read from top to bottom making the logic of the
program more visible for checking and for maintenance.
Simple Sequence
The simple-Sequence structure consists of one action followed by another. In other words, the flow of
control is first to perform operation A and the to perform operation B.. A simple sequence is flowcharted as
two process symbols connected by a flowline.
Selection
The selection structure consists of a test for condition followed by two alternative paths for the program to
follow. The programs selects one of the program control paths depending on the test of the condition.
After performing one of two paths, the program control returns to a single point. This pattern can be
termed IF .. ELSE because the logic can be stated (for condition P and operations C and D): IF P (is true),
perform C; ELSE perform D . A flowchart for the selection structure consists of a decision symbol
followed by two paths, each with a process symbol, coming together following the operation symbols.
Condition
?
Module A
IF Condition, then:
[Module A]
Condition
?
Module A
Module
B
Else
[Module A]
[End of IF structure]
Repeat for K = R to S by T:
[Module]
[End of loop]
Algorithm example for iteration or repetition
For example let us take 10 sets of numbers each set containing three. The problem is to get the biggest
number in each set and print it.
Algorithm
Step 1: Read the total number of sets
Step 2: Initialize the number of the set as N=1
Step 3: Read three numbers of a set say A, B, C.
Step 4: Compare A with 3 and choose the bigger.
Step 5: Compare the bigger number with C and Choose the biggest
Step 6: Print the biggest number,
Step 7: Increment the number of the set by 1
(N=N+1)
Step 8: Check whether we have exceeded 10. If not Go – To step 3. Otherwise.
Step 9: STOP
The flow chart for the same in given below
32
[Type the document title]
CHAPTER 7
To understand:
As program become larger, and more complex, it becomes more difficult to write clear understandable
solutions that work correctly. The goal of modular programming is to break up the program into small
parts that are more easily understood. The planning, coding and testing can be done on these small,
relatively simple units, rather than on one large, complex body of code.
Programmers must develop the skill and the ability to look at a large program and to decompose it into
individual factions. Once a programmer has learned to modularize programs, program will be coded more
quickly, will be more likely to work correctly, and will certainly be easier to read to be maintained by
others.
Virtually all computer scientists recommend modular programming. The only disagreement seems to be at
what point a programmer should begin writing what are called “subroutines”. Many programmers wait
until programs become hopelessly complex. Then introducing subroutine can save the day. The more
practical approach is to being using subroutines early. As programs become more complex, if correct
habits need already in place, the programmer doesn’t need to be “rescued” The solution to the program is
at hand. A subroutine is a group of statements intended to accomplish an individual task.
When a program is written with individual tasks in subroutine, a mainline, or control program is needed.
This control program is sometimes called the program outline, as it presents an overviewed of the program
tasks. [Another term sometimes used for the program maintain is the driver.
Program example
Using modular (subroutines) approach, show the program plan of a computer program for calculating
simple interest on a deposit by a customer.
34
[Type the document title]
Solution
Modular Pseudo code plan for the problem
1. Input data
1.1 Prompt and input rate, deposit amount, and number of years.
2. Calculations
2.1 Calculate interest = deposit * rate * years
2.2 Calculate ending balance = deposit + interest
3. Output
3.1 Print interest and ending balance
Start
Input data
Calculations
Write output
35
[Type the document title]
Calculations
Calculations
Calculate balance
= Deposit + Interest
Enter deposit
Return
Enter year
Return
Write Output
Print Interest
Print Balance
Return
36
[Type the document title]
Many programmers who write modular programs prefer to plan their programs with hierarchy chart.
A hierarchy chart is used to plan and show program structure. It is constructed much like an
organization chart. As shown the solution below, level A shows the entire program, which is broken
down into major program functions on the B level. The modules can be broken into smaller and
smaller parts until the coding for each function becomes straightforward. Many programmers use a
hierarchy chart to plan the overall structure of a program. Then, when the individual modules are
identified, flowcharting or pseudo code will be used to plan the details of the logic.
Interest Program
CHAPTER 8
To understand:
Before computer program is successfully written, documented and installed, it must have passed through
the following stages. Each stage has something to contribute to the accomplishment of the whole task.
The stages are:
Problem definition:
Before any reasonable and meaningful program could be written, the problem that prompted it must have to
be defined. No one solves a problem he does not know. The problem to be solved by computer should be
well stated and understood before the solution will be worked out. From the solution, it is expected that the
output of the problem is known and the input will be prepared to arrive at the output.
depending on the language in use ie for BASIC program, when the command RUN is typed and entered,
the program begins executing. If any rules language is broken, the program will not work. The errors must
be removed before the program will start producing the output. Testing is very necessary to ensure that the
correct and required answers are produced as the output.
Compile program
No
Re-think problem
Get unreasonable or Yes Re-think program
incorrect answers? Edit program
No
Success/documentation
41
[Type the document title]
CHAPTER 9
To understand:
Programs can be written in several languages. Just as there are many spoken languages, there are many
computer languages. In this lecture we shall study the different levels of computer languages and their
forms.
Currently all computer languages can be grouped into three, namely, machine languages, assembly
languages and high-level languages. Machine languages and assembly languages are together referred to
as low-level languages. The detail characteristics of each group of languages are discussed below.
Low-level Languages
These group of languages are so named because in frm they are very close (i.e. similar) to the language the
computer understands, and very remote from languages spoken by human beings. Low-level languages are
in two forms namely: machine language and assembly language.
Machine language
Machine language is as old as the computer itself. It is the computer’ s own language. It consists of the
code that designates the electrical states in the computer (i.e, on or off): this is expressed as combination of
Os and 1s./It is called the computer’s own language because codes or instructions written in machine
language can be executed directly by the computer; without the need for any translation. This is the only
language that has this characteristic.
Each type of computer has its own machine language. That is to say, that different brands of computers
cannot understand programs written in another brand’s machine language. Talking specifically, a
computer made by IBM company has its own language which is different from the one of NCR
company. Even two different models of computers made by the same company do not usually have the
same machine language. Thus programs written in machine language are said to be machinedependent.
43
[Type the document title]
Every instruction in machine language programs must specify both the operation to be carried out as
well as the storage locations of the data items to be used in the operation. In form, it consists of a
series of numbers. The operation part is called opcode or operation code and the remaining part gives
the addresses of the data items in memory that will be affected by the operation. Due to these special
requirements machine language programming is extremely complex, tedious and time consuming.
For example, the instruction, in machine language, to make the computer add together the numbers
currently stored in memory addresse four and seven and then store the sum in address four will look
like this.
1A47
The first two numbers 1A is the operation code for add in IBM 360 machine. On another machine it
will be another series of numbers different from the one given.
For effective and efficient program in machine language, the programmer must keep track of which
memory locations have been used and the purpose of each memory location. Also the programmer
must know every operation code and the action that it causes the computer to take. It is quite lengthy
and tedious. To overcome this, the assembly language come into existence.
Assembly Language
In order to relieve programmers the arduous task of writing in machine language, the assembly
language was developed. It is very much similar to machine language but instead of writing in series of
numbers, convenient symbols and abbreviations are used. Assembly language programming does not
require the programmer to remember numeric opcodes and addresses. However, it still requires the
programmer to be familiar with the operation codes and the methods of addressing memory locations
for that particular machine. This is because, the assembly language; though at a higher level, still
depend very much on the language of that particular machines.
Programs in assembly language cannot be executed directly, it still has to be changed to the machine
language during execution. Thus we can see that assembly language too is machine dependent. That is
to say, different brands of computers have different assembly language. For this reason, assembly
language is still classified as low-level language.
44
[Type the document title]
For the IBM 360 computer the machine language code for ass is 1A while in assembly language,
addition operation code is AR is a mnemonic for “Add Register”. For STORE operation the assembly
code is the mnemonic STO while TRA stands for TRANSFER Operation and MR stands for MOVE
REGISTER operation etc.
Similarly the programmer can assign a name to each memory location. For instance address seven may
be given the name P and address four may be given the name Q, thus the instruction.
AR P. Q
In assembly language will be executed as adding the contents of register Q to the contents of P; of
course the final result will be in register P.
As mentioned earlier, regardless of which assembly language is used, the computer cannot directly
execute the programs written in this language. It has to be translated into the machine language by
another special purpose software called translator. The details of the translation process shall be studied
in later lectures.
High-level languages
The Machine and Assembly languages discussed before require programmers to construct programs in a
form that does not follow normal ways of human thinking, communication and language notation. To
avoid this problem High-level languages were developed. Another name for high-level languages is
problem-oriented languages. With this language, programmers’ attention are now directed towards
problem solving instead of operations going on inside computer. These languages allow mathematicians
and Scientists to use common algebraic notations for coding formulas while other lay programmers can
write their programs in ordinary sentence form. The time and effort needed to write programs are now
reduced considerably and programs are easier to correct and modify.
A large number of high-level languages are in use today. In fact, more are being developed daily as
researchers are still going on.
4) `C’ language
5) PL/1
Each of the high-level languages has rules that govern how to write instructions in them. Like
any human language, it is the duty of the programmer or user to learn the rules of the language
he wants to use.
The example below shows how to add two numbers held in variables X and Y placing the sum
in X using the most common four high-level languages.
BASIC……. LET X = X + Y
COBOL………. ADD Y TO X
46
[Type the document title]
PASCAL…….. X: = X + Y
It can be observed that the notation is very similar to human ways of thinking and expression
and very remote from the machine language.
47
[Type the document title]
CHAPTER 10
To understand:
MACHINE LANGUAGE
Machine Code or machine language is a low-level programming language that can be understood directly
by a computer’s central processing unit (CPU). Machine code consists of sequences of binary numbers, or
bits, which are usually represented by 1s and 0s, and which form the basic instructions that guide the
operation of a computer. The specific set of instructions that constitutes a machine code depends on the
make and model of the computer’s CPU. For instance, the machine code for the Motorola 68000
microprocessor differs from that used in the Intel Pentium microprocessor.
Writing programs in machine code is tedious and time-consuming since the programmer must keep track of
each specific bit in an instruction. Another difficulty with programming directly in machine code is that
errors are very hard to detect because the program is represented by rows and columns of 1s and 0s.
ASSEMBLY LANGUAGE
Assembly language is type of low-level computer programming language in which each statement
corresponds directly to a single machine instruction. Assembly languages are thus specific to a given
processor. After writing an assembly language program, the programmer must use the assembler specific to
the microprocessor to translate the assembly language into machine code. Assembly language provides
precise control of the computer, but assembly language programs written for one type of computer must be
rewritten to operate on another type. Assembly language might be used instead of a high-level language for
any of three major reasons: speed, control, and preference. Programs written in assembly language usually
49
[Type the document title]
run faster than those generated by a compiler; use of assembly language lets a programmer interact directly
with the hardware (processor, memory, display, and input/output ports).
Assembly language uses easy-to-remember commands that are more understandable to programmers than
machine-language commands. Each machine language instruction has an equivalent command in assembly
language. Assembly language makes programming much easier, but an assembly language program must
be translated into machine code before it can be understood and run by the computer. Special utility
programs called assemblers perform the function of translating assembly language code into machine code.
Like machine code, the specific set of instructions that make up an assembly language depend on the make
and model of the computer’s CPU. Other programming languages such as Fortran, BASIC, and C++, make
programming even easier than with assembly language and are used to write the majority of programs.
These languages, called high-level languages, are closer in form to natural languages and allow very
complicated operations to be written in compact notation.
5) They are machine independent. That is, program written for computer can be transferred to
another computer with little or no modification.
CHAPTER 11
To understand:
* Debugging.
• Identify sources of bugs in a program
• Explain syntax, run-time and logical errors.
• Identify techniques of locating bugs in a program
• Explain program maintenance.
• Distinguish between debugging and maintaining a program
51
[Type the document title]
Debugging is the art of diagnosing errors in programs and determining how to correct them. "Bugs" come
in a variety of forms, including: coding errors, design errors, complex interactions, poor user interface
designs, and system failures. Learning how to debug a program effectively, then, requires that you learn
how to identify which sort of problem you're looking at, and apply the appropriate techniques to eliminate
the problem.
Bugs are found throughout the software lifecycle. The programmer may find an issue, a software tester
might identify a problem, or an end user might report an unexpected result. Part of debugging effectively
involves using the appropriate techniques to get necessary information from the different sources of
problem reports.
Debugging is described as identification and removal of localized implementation errors or bugs from a
program or system. Program debugging is often supported by a debug tool, a software tool that allows the
internal behavior of the program to be investigated in order to establish the existence of bugs. This tool
typically offer trace facilities and allow the planting of breakpoint in the program at which execution is to
be suspended so that examination of partial results is possible and permit examination and modification of
the values of program variables when a breakpoint is reached.
In computer program/software, a bug is an error in coding or logic that causes a program to malfunction or
to produce incorrect results. The computer software (debug tool) is used to detect, locate, and correct
logical or syntactical errors in a computer program. Similarly, in hardware, a bug is a recurring physical
problem that prevents a system or set of components from working together properly. To detect, locate, and
correct a malfunction or to fix an inoperable system, the term troubleshoot is more commonly used in
hardware contexts. The three major program error are; syntax error, logical error and run-time error.
With coding errors, the source of the problem lies with the person who implements the code. Examples of
coding errors include:
• Calling the wrong function ("moveUp", instead of "moveDown")
• Using the wrong variable names in the wrong places ( "moveTo(y, x)" instead of "moveTo(x, y)")
• Failing to initialize a variable ( "y = x + 1", where x has not been set)
• Skipping a check for an error return
Software users readily see some design errors, while in other cases design flaws make a program more
difficult to improve or fix, and those flaws are not obvious to a user. Obvious design flaws are often
demonstrated by programs that run up against the limits of a computer, such as available memory, available
disk space, available processor speed, and overwhelming input/output devices. More difficult design errors
fall into several categories:
• Failure to hide complexity
• Incomplete or ambiguous "contracts"
• Undocumented side effects
52
[Type the document title]
Complex interactivity bugs arise in scenarios where multiple parts of a single program, multiple programs,
or multiple computers interact.
Sometimes, computer hardware simply fails, and it usually does so in wildly unexpected ways. Determining
that the problem lies not with the software itself, but with the computer(s) on which it is usually
complicated by the fact that the person debugging the software may not have access to the hardware that
shows the problem.
Preventing Bugs
No discussion of debugging software would be complete without a discussion of how to prevent bugs in the
first place. No matter how well you write code, if you write the wrong code, it won't help anyone. If you
create the right code, but users cannot work the user interface, you might as well have not written the code.
In short, a good debugger should keep an open mind about where the problem might lie.
Although it is outside the scope of this discussion to describe the myriad techniques for avoiding bugs,
many of the techniques here are equally useful after the fact, when you have a bug and need to uncover it
and fix it. Thus, a brief discussion follows.
Methods of debugging
Understand the Problem
In order to write effective software, the developer must solve the problem the user needs solved. Users,
naturally enough, do not think in strict algorithms, windowing systems, web pages, or command line
interfaces. Rather, users think of their problems in the way that they think of their problems (yes, that is
circular).
Sit down with the intended user, and ask them what they want from the software. Users frequently want
more than software can actually deliver, or have contradictory aims, such as software that does more, but
doesn't require that they learn anything new. In short, ask the users what their goals are. Absent those goals,
users will keep reporting bugs that do not add up to a coherent whole.
Although each debugging experience is unique, certain general principles can be applied in debugging. This
section particularly addresses debugging software, although many of these principles can also be applied to
debugging hardware.
An experienced programmer often knows where errors are more likely to occur, based on the complexity of
sections of the program as well as possible data corruption. For example, any data obtained from a user
should be treated suspiciously. Great care should be taken to verify that the format and content of the data
are correct. Data obtained from transmissions should be checked to make sure the entire message (data)
was received. Complex data that must be parsed and/or processed may contain unexpected combinations of
values that were not anticipated, and not handled correctly. By inserting checks for likely error symptoms,
the program can detect when data has been corrupted or not handled correctly.
If an error is severe enough to cause the program to terminate abnormally, the existence of a bug becomes
obvious. If the program detects a less serious problem, the bug can be recognized, provided error and/or log
messages are monitored. However, if the error is minor and only causes the wrong results, it becomes much
more difficult to detect that a bug exists; this is especially true if it is difficult or impossible to verify the
results of the program.
The goal of this step is to identify the symptoms of the bug. Observing the symptoms of the problem, under
what conditions the problem is detected, and what work-around, if any, have been found, will greatly help
the remaining steps to debugging the problem.
This step is often the most difficult (and therefore rewarding) step in debugging. The idea is to identify
what portion of the system is causing the error. Unfortunately, the source of the problem isn't always the
same as the source of the symptoms. For example, if an input record is corrupted, an error may not occur
until the program is processing a different record, or performing some action based on the erroneous
information, which could happen long after the record was read.
This step often involves iterative testing. The programmer might first verify that the input is correct, next if
it was read correctly, processed correctly, etc. For modular systems, this step can be a little easier by
checking the validity of data passed across interfaces between different modules. If the input was correct,
but the output was not, then the source of the error is within the module. By iteratively testing inputs and
outputs, the debugger can identify within a few lines of code where the error is occurring.
Having found the location of the bug, the next step is to determine the actual cause of the bug, which might
involve other sections of the program. For example, if it has been determined that the program faults
because a field is wrong, the next step is to identify why the field is wrong. This is the actual source of the
bug, although some would argue that the inability of a program to handle bad data can be considered a bug
as well.
54
[Type the document title]
A good understanding of the system is vital to successfully identifying the source of the bug. A trained
debugger can isolate where a problem originates, but only someone familiar with the system can accurately
identify the actual cause behind the error. In some cases it might be external to the system: the input data
was incorrect. In other cases it might be due to a logic error, where correct data was handled incorrectly.
Other possibilities include unexpected values, where the initial assumptions were that a given field can
have only "n" values, when in fact, it can have more, as well as unexpected combinations of values in
different fields (field x was only supposed to have that value when field y was something different).
Another possibility is incorrect reference data, such as a lookup table containing incorrect values relative to
the record that was corrupted.
Having determined the cause of the bug, it is a good idea to examine similar sections of the code to see if
the same mistake is repeated elsewhere. If the error was clearly a typo, this is less likely, but if the original
programmer misunderstood the initial design and/or requirements, the same or similar mistakes could have
been made elsewhere.
Having identified the source of the problem, the next task is to determine how the problem can be fixed. An
intimate knowledge of the existing system is essential for all but the simplest of problems. This is because
the fix will modify the existing behavior of the system, which may produce unexpected results.
Furthermore, fixing an existing bug can often either create additional bugs, or expose other bugs that were
already present in the program, but never exposed because of the original bug. These problems are often
caused by the program executing a previously untested branch of code, or under previously untested
conditions.
In some cases, a fix is simple and obvious. This is especially true for logic errors where the original design
was implemented incorrectly. On the other hand, if the problem uncovers a major design flaw that
permeates a large portion of the system, then the fix might range from difficult to impossible, requiring a
total rewrite of the application.
In some cases, it might be desirable to implement a "quick fix", followed by a more permanent fix. This
decision is often made by considering the severity, visibility, frequency, and side effects of the problem, as
well as the nature of the fix, and product schedules (e.g., are there more pressing problems?).
After the fix has been applied, it is important to test the system and determine that the fix handles the
former problem correctly. Testing should be done for two purposes: (1) does the fix now handle the original
problem correctly, and (2) make sure the fix hasn't created any undesirable side effects.
For large systems, it is a good idea to have regression tests, a series of test runs that exercise the system.
After significant changes and/or bug fixes, these tests can be repeated at any time to verify that the system
still executes as expected. As new features are added, additional tests can be included in the test suite.
The diagram below illustrates the fix and test approach of debugging a program.
55
[Type the document title]
Compile program
No
Re-think problem
Get unreasonable or Yes Re-think program
incorrect answers? Edit program
No
Success
Syntax of a program
The syntax of a program is the rules defining the legal sequences of symbolic elements in a particular
language. The syntax rules define the form of various constructs in the language, but say nothing about the
meaning of these constructs. Examples of constructs are; expressions, procedures and programs.
Programming Errors
Error simply means mistake. That is errors occur in programs as a result of system failure (hardware),
wrong code/instructions (software) and human error. There are four categories of programming error;
Is an error that occurs during the execution of a program. In contrast, compile-time errors occur while a
program is being compiled. Runtime errors indicate bugs in the program or problems that the designers
had anticipated but could do nothing about. For example, running out of memory will often cause a
runtime error.
Note that runtime errors differ from bombs or crashes in that you can often recover gracefully from a
runtime error.
56
[Type the document title]
Program is compiled OK, but something goes wrong during execution e.g division by zero or an
attempt to read data that does not exist.
Detected by the computer run-time system
Computer usually prints error message and stops.
Error in the form of statement: misspelled word, unmatched parenthesis, comma out of place
Detected by the computer at compiler time
Computer cannot correct error, so object program is not generated and thus program is not executed
Computer (compiler) prints error messages, but continues to compile.
Linker errors: These types of errors have the following basic characteristics;
Prevents the generation of an executable image
Common linker errors; o specifying the wrong header file o disagreement among the function
prototype, function definition and calls to that function
Program maintenance
57
[Type the document title]
Program/software maintenance is the modification of a software product after delivery to correct faults, to
improve performance or other attributes, or to adapt the product to a modified environment. This
international standard describes the 6 software maintenance processes as:
1. The implementation processes contains software preparation and transition activities, such as the
conception and creation of the maintenance plan, the preparation for handling problems identified
during development, and the follow-up on product configuration management.
2. The problem and modification analysis process, which is executed once the application has become
the responsibility of the maintenance group. The maintenance programmer must analyze each
request, confirm it (by reproducing the situation) and check its validity, investigate it and propose a
solution, document the request and the solution proposal, and, finally, obtain all the required
authorizations to apply the modifications.
E.B. Swanson initially identified three categories of maintenance: corrective, adaptive, and perfective.
• Adaptive maintenance: Modification of a software product performed after delivery to keep a
software product usable in a changed or changing environment.
• Perfective maintenance: Modification of a software product after delivery to improve performance
or maintainability.
Preventive maintenance: Modification of a software product after delivery to detect and correct latent faults
in the software product before they become effective faults.
A common perception of maintenance is that it is merely fixing bugs. However, studies and surveys over
the years have indicated that the majority, over 80%, of the maintenance effort is used for non-corrective
actions. Key findings shows that program maintenance is really evolutionary developments and that
maintenance decisions are aided by understanding what happens to systems (and software) over time.
While Debugging is a very important task in the software development process, because an erroneous
program can have significant consequences for its users. Some languages are more prone to some kinds of
faults because their specification does not require compilers to perform as much checking as other
languages. Use of a static analysis tool can help detect some possible problems.
also has the advantage of creating reusable routines that can be used in other programs. Once a routine is
debugged and verified it is easier to copy and paste it into another program than to write it all over again.
58
[Type the document title]