2. CHAPTER TWO: THE PRINCIPLE OF
GOOD PROGRAMMING STYLE
•PROGRAMMING STYLE
•GENERAL GUIDELINES / RULES GOVERNING
GOOD PROGRAMMING STYLE
•THE STRUCTURE OF QBASIC AND ITS COMMON
I/O STATEMENTS
3. 2.1 PROGRAMMING STYLE
•Programming style can be regarded as
guiding rules that govern writing of source
code for a computer program.
•Programming Style = English Composition
•With good programming style, debugging is
never a bug to the debugger!
4. -PROGRAMMING STYLE CONT’D
•Programming styles are usually designed for a
specific programming language / family language.
i. C & C++: Same Style
ii. Basic (QBasic, GW-Basic) & VB: Same Style
iii. Style in C != Style in QBasic
6. 2.2 GENERAL GUIDELINES / RULES
GOVERNING GOOD PROGRAMMING STYLE
1. PROGRAM HEADER
2. INDENTATION & BRACING STYLE
3. USE OF APPROPRIATE VARIABLE NAMES
4. CONSISTENCY IN THE USE OF VARIABLE NAMES
5. NON-USAGE OF KEYWORD/RESERVED WORD
7. -THE RULES…
•THE PROGRAM HEADER
This rule says that, every good programming
language must have, at or near the beginning
of the program, a header block indicating the
program filename, program library header,
program purpose and other identifications
that pertain to the program (e.g. the
comment block).
8. • In COBOL, we have:
PROGRAM-ID. AJAYI-PRO.
PROGRAM-NAME. AJAYI-PROGRAM.
SOURCE-MACHINE. IBM-370.
OBJECT-MACHINE. IBM-370.
/* This program calculates CGPA of students*/
-PROGRAM HEADER CONT’D….INSTANCES
9. • Look at PASCAL:
PROGRAM. AJAYI-PROGRAM (INPUT, OUTPUT)
{* Description : This program calculates CGPA of students*}
-PROGRAM HEADER CONT’D….INSTANCES
10. • Consider JAVA:
Public Class AJClassPro
{
Public Static Void Main (String args[ ])
{
/* This program calculates CGPA of students */
// 2nd version of the commenting style
}
}
-PROGRAM HEADER CONT’D….INSTANCES
11. • Now to C:
#include<stdio.h>
/* This program calculates CGPA of students */
OR
#include<stdio.h>
#include<stdlib.h>
/* This program calculates CGPA of students */
-PROGRAM HEADER CONT’D….INSTANCES
12. • And C++, we have it as:
#include<iostream>
using namespace
//You can insert comments here or anywhere
Main()
{
}
-PROGRAM HEADER CONT’D….INSTANCES
13. -THE RULES…
• INDENTATION & BRACING STYLE
Indentation helps in identifying errors in control flow
statements as well as in blocks of codes.
In a well and properly indented program, errors in
codes are easily traced.
It enhances program readability!
14. -THE RULES…
•INDENTATION & BRACING STYLE…
If (condition) {
// code block - Your program goes here
}
The above is called IMMEDIATE Bracing Style
15. -THE RULES…
•INDENTATION & BRACING STYLE…
If (condition)
{
// code block - Your program goes here
}
The above is called DIRECT Bracing Style
16. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES
The use of appropriate variable name is very
important in a good programming style. Poorly-
named variables make code (program) difficult to
read and understood.
17. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES…
Consider this:
get a, b, c
if a < 24 && b < 60 && c < 60
return true;
else
return false;
18. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES…
Now compare with this:
get hrs, mins, secs
if hrs < 24 && mins < 60 && secs < 60
return true;
else
return false;
The latter enhances readability and understandability. However, the former can be made
better by including comments indicating/conveying the variables’ representation.
19. -THE RULES…
•CONSISTENCY IN THE USE OF VARIABLE NAMES
Consistency in the use of variable name implies
sticking to the use of a set of variable names
throughout your program, regardless of the case-
sensitivity or otherwise of the language.
20. -THE RULES…
• CONSISTENCY IN THE USE OF VARIABLE NAMES…
e.g.
int sum;
float avg;
SUM = (num1 + num2) **2
avg = Sum/2
average = average + 1
21. -THE RULES…
• NON-USAGE OF KEYWORD/RESERVED WORD
For a good programming style, a programmer should as much
as possible avoid the use of words that are close to or that are
reserved words or keywords. The best way of not entering into
this trap or error is by being acquainted with the language’s
keywords or reserved words.
e.g.
int get; /*where get is a reserved word in C*/
22. 2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS
• THE STRUCTURE OF QBASIC…
•QBasic is a procedural language. Unlike object-
oriented language, it has no standard structure
according to programming style rule. Howbeit, it
does have structure for programmers to follow.
23. 2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
•GUIDE TO QBASIC STRUCTURE
i. Line Number: Programs in QBasic are easily written
with line numbers for readability purpose.
ii. CLS: This means Clear Screen
iii. REM: Implies REMark – Comment.
iv. END: To signify the end of a program
24. 2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GUIDE TO QBASIC STRUCTURE- ILLUSTRATION
• For example, the program below does nothing but shows the guide
to QBasic Structure:
5 CLS
10 REM
15 END
OR
2 CLS
4 REM
6 END
25. 2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GARBAGE IN & OUT IN QBASIC
• INPUT STATEMENT
• To feed/supply data unto the computer system using QBasic,
we make use of any of the following two commands:
INPUT statement
READ&DATA statement
26. 2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GARBAGE IN & OUT IN QBASIC
• OUTPUT STATEMENTS
• These are statements that are used for fetch/retrieve/show
to screen, inputted or stored data from the computer
system.
• The major/basic output statement used in QBasic is called
PRINT.
28. EXAMPLE 1
•Code in QBasic to output the literals:
BIO-DATA Quiz
HE was born and brought up in Lagos.
He hails from Kwara.
He settles down in Ondo.
Who is He?
29. SOLUTION
5 CLS
10 REM
15 Print “BIO-DATA Quiz”
20 Print “_____________”
25 Print “He was born and brought up in Lagos.”
30 Print “He hails from Kwara.”
35 Print “He settles down in Ondo.”
40 Print
45 Print “Who is He?”
50 END
30. EXAMPLE 2
•Given two variables: birthyear and
currentyear, input values for the two
variables and compute your AGE by
finding the difference of the two
variables.