Programming Help for Beginners
We write
programs to instruct computers. When programming using a high level
programming language like C++ or Java, we are using a syntax that is
somewhat closer to human languages. However, we use these programs as
inputs to either compilers or interpreters to be converted to computer
understandable binary format. For this reason, as far as the program
code adheres to the syntax of the used programming languages, the
compilers and interpreters never bother about the layout or visual
formatting of the program code. However, as human programmers, we
ourselves need to bother about the aesthetics of the program code.
What is a Coding Standard?
A
coding standard is a set of guidelines, rules and regulations on how to
write code. Usually a coding standard includes guide lines on how to
name variables, how to indent the code, how to place parenthesis and
keywords etc. The idea is to be consistent in programming so that, in
case of multiple people working on the same code, it becomes easier for
one to understand what others have done. Even for individual
programmers, and especially for beginners, it becomes very important to
adhere to a standard when writing the code. The idea is, when we look
at our own code after some time, if we have followed a coding standard,
it takes less time to understand or remember what we meant when we
wrote some piece of code.
Coding Standards Make a Difference
Look at the following example:
int volume(int i, int j, int k) {
int vol;
vol = i * j * k;
return vol;
}
Looking
at this code at a glance, it takes some time for one to understand that
this function calculates the volume. However if we adhere to a naming
convention for variables and method names, we could make the code more
readable.
Here are few sample conventions:
- use meaningful variable names
- use verbs in method names
- use nouns for variables
- use 4 spaces to indent
int calculateVolume(int height, int width, int length) {
int volume = 0;
volume = height * width * length;
return volume;
}
It
takes more time to type this code, however this saves far more time.
This code is far more readable than its original version. With a little
bit of effort, we could make the code much more understandable.
The Benefits
It
is not only the readability that we get through a coding standard in
programming. Writing more secure code could also be encouraged through
a coding convention. As an example, in C++ we could say that each
pointer variable must be initialized to NULL.
char* myName = NULL;
This ensures that we would not corrupt memory while using this pointer variable.
Code
readability is just one of the aspects of maintainability. Coding
standards help a great deal with program maintainability, our ability
to change programs with ease. Consistency imposed through a coding
standard is a key factor to achieve success in maintaining prorams.
Defining Your Own Coding Standard
A
programmer can define his or her own coding convention and adhere to
that in writing programms. However there are many coding conventions
available on the Internet. Those who program in Java should have a look
into http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html - Code
Conventions for the Java Programming Language by Sun.
For C++
coding standards, I would recommend that you have a look into
http://www.bbc.co.uk/guidelines/webdev/AppB.Cpp_Coding_Standards.htm -
C++ Coding Standards from BBC.
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-14.html
- C++ Programming HOWTO has some C++ Coding Conventions and also a
bunch of links that lead to several coding standards that you can pick
from.