- Programming Methodologies
- Understanding the Problem
- Identifying the Solution
- Applying Modular Techniques
- Writing the Algorithm
- Flowchart Elements
- Using Clear Instructions
- Correct Programming Techniques
- Program Documentation
- Program Maintenance
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Correct Programming Techniques
In this chapter, we will cover how to write a good program. But before we do that, let us see what the characteristics of a good program are −
Portable − The program or software should run on all computers of same type. By same type we mean a software developed for personal computers should run on all PCs. Or a software for written for tablets should run on all tablets having the right specifications.
Efficient − A software that does the assigned tasks quickly is said to be efficient. Code optimization and memory optimization are some of the ways of raising program efficiency.
Effective − The software should assist in solving the problem at hand. A software that does that is said to be effective.
Reliable − The program should give the same output every time the same set of inputs is given.
User friendly − Program interface, clickable links and icons, etc. should be user friendly.
Self-documenting − Any program or software whose identifier names, module names, etc. can describe itself due to use of explicit names.
Here are some ways in which good programs can be written.
Proper Identifier Names
A name that identifies any variable, object, function, class or method is called an identifier. Giving proper identifier names makes a program self-documenting. This means that name of the object will tell what it does or what information it stores. Let’s take an example of this SQL instruction:
Look at line 10. It tells anyone reading the program that a student’s ID, name and roll number are to be selected. The names of the variables make this self-explanatory. These are some tips to create proper identifier names −
Use language guidelines
Don’t shy from giving long names to maintain clarity
Use uppercase and lowercase letters
Don’t give same name to two identifiers even if the language allows it
Don’t give same names to more than one identifier even if they have mutually exclusive scope
In the image above, look at line 8. It tells the reader that the next few lines of code will retrieve list of students whose report card is to be generated. This line is not part of the code but given only to make the program more user friendly.
Such an expression that is not compiled but written as a note or explanation for the programmer is called a comment. Look at the comments in the following program segment. Comments start with //.
Comments can be inserted as −
Prologue to the program to explain its objective
At the beginning and/or end of logical or functional blocks
Make note about special scenarios or exceptions
You should avoid adding superfluous comments as that may prove counterproductive by breaking the flow of code while reading. Compiler may ignore comments and indentations but the reader tends to read each one of them.
Distance of text from left or right margin is called indent. In programs, indentation is used to separate logically separated blocks of code. Here’s an example of indented program segment:
As you can see, indented program is more understandable. Flow of control from for loop to if and back to for is very clear. Indentation is especially useful in case of control structures.
Inserting blank spaces or lines is also part of indentation. Here are some situations where you can and should use indentation −
Blank lines between logical or functional blocks of code within the program
Blank spaces around operators
Tabs at the beginning of new control structures