Tuesday, July 31, 2007

Programming Tutorial Portal Tip 1

Coding Conventions Tutorial

Naming Variables

Always use meaningful variables in your program. Don't use num1 if that variable represents the average result. Worst still you would use the variable x for this. In programming, always bear in mind that you are programming for yourself and at the same time for others to clearly read it. That's need later if you are working as a team. Use variable names like these,

ave - to represent the average or you can simply use the word average itself.
x, y - for x and y coordinates.
subTota l - for the temporary result of a particular addition.
i, j, k, l, cntr - are usually common in loops.
payPerView,studPerRoom - this is the professional style of naming variables..

You see? The variables speak for themselves! Seeing these variables, the programming could immediately surmise what's the use of that variable.

Another form used by professionals is to precede the variable name with the data type of that variable. Example:

char chrLetter -- we precede Letter with chr to mean it's a character.

char strName[20] -- as you can see? Name is a string, we simply put str to make it stand
-- that it's a string.
int nAge -- Age is usually an integer, we don't have 1.5 age right? and the commonly
-- used prefix is n or int will do

double dblLightYears -- for double numbers used dbl as your prefix

float fVolume -- usually f is being used to represent float



Comments

In order to make a program understandable, you should include some explanatory notes at key places in the program. Most programming languages have provisions for putting comments in your programs. I myself have witnessed to the idea of not having comments in my program. I wrote a program a long time ago and what's unique about it is my reaction when I saw my uncommented source code. My reaction is simply, wow! Who wrote this program? Ahmmm? I really forgot the algorithm I used on it and it made me think it's not mine. Therefore, to save your time banging your head figuring out what a particular line does, simply use comments.

Example:

distance = speed * time; //Computes the distance traveled.



Indenting

One signature of a beginner's program is the way the source was laid out. Typically you will see, everything seems sticking to the left part of the monitor. It's like that the instruction codes are very afraid to touch the right side of the screen. I don't know what are they afraid of. If your a beginner in programming, you better practice your habit of indenting your source code as much as possible. Here's a typical style I prefer to use, take note this can also be applied to any other languages:


int main(){
int nAge;
printf("Enter your age: ");
scanf("%d", &nAge);

if(nAge < style="font-weight: bold;">

Neat isn't it? You see the hierarchy? It would take sometime to make it habit though. If you want to have your style of indenting, just remember, be CONSISTENT every time.

That's all for now.. Till the next Tip!

0 comments: