English 中文(简体)
Correct Programming Techniques
  • 时间:2024-11-03

Correct Programming Techniques


Previous Page Next Page  

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.

Characteristics Good Program

    Effective − The software should assist in solving the problem at hand. A software that does that is said to be effective.

    Repable − The program should give the same output every time the same set of inputs is given.

    User friendly − Program interface, cpckable pnks 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 exppcit 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:

Proper Identifier Names

Look at pne 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 guidepnes

    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

Comments

In the image above, look at pne 8. It tells the reader that the next few pnes of code will retrieve pst of students whose report card is to be generated. This pne 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

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.

Indentation

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:

Indentation

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 pnes is also part of indentation. Here are some situations where you can and should use indentation −

    Blank pnes between logical or functional blocks of code within the program

    Blank spaces around operators

    Tabs at the beginning of new control structures

Advertisements