Good Programming Practice: Version 1.0
Written By:X_console
shellscope@yahoo.com
GOOD PROGRAMMING PRACTICE: VERSION 1.1
Introduction
This is a guide to learning how to write good programs. I'm not going to
teach you a programming language, rather I'm going to teach you how to
do proper programming so that you can write good code, good programs and
easy to maintain code. Obviously I'm assuming that you're familiar or
learning at least one programming language. By the way, when I say
programming language, I also refer to scripting languages. In this
document, I will be doing examples in C and all source code is compiled
with egcs-2.91.66.
Terminating statements
The most common programming error is a novice programmer forgetting to put
a
semi-colon to terminate a statement. The errors you get from doing this
are
sometimes so cryptic it befuddles a novice programmer. Always be sure to
check each line of your code to see if you've properly terminated it.
Granted that not all languages require a terminating semi-colon. Here's
an example of forgetting a semi-colon:
int main(void)
{
/* no semi-colon! expect an error! */
printf("Hello World!\n")
return(0);
}
You'd be surprised how many people make this mistake. Sure, that's a
small program... but wait 'till you write code that's 1000 lines in
length! You can bet that you missed out a semi-colon somewhere! To help
you remember the semi-colon, think of it as a period when you write a
sentence in English. Make it a habit.
Now the other thing about semi-colons is that some people aren't sure
where to put them, so they put them everywhere. Bad idea. Now some of
you experienced programmers are probably laughing, but I've seen this
happen in the school I go to. People sending emails wondering why their
program doesn't work, and it's all thanks to the semi-colon. An example
of this:
/* semi-colon after main() is wrong: */
int main(int argc, char *argv[]);
{
printf("Hello World");
return(0);
}
You do not put a semi-colon when you're starting the block of a
function or method, or procedure. This is wrong.
White Space
In C, white space is ignored. That means
you can write surprisingly obfuscated code by ignoring white space
yourself. Let's take a look at a C program that ignores white space:
int main(void){printf("HelloWorld");return(0);}
How's that for conciseness? Obviously the above is a little exagerated,
but you'll most commonly come across code like the following:
if(x==0) {a=b=c=d=MAX; x++;}
Yes it saves space, but it's damn confusing for anyone who looks at your
code, and for you when you look at it one month later. Write it the
right and clean way!
if(x == 0)
{
a = b = c = d = MAX;
x++;
}
See how much better it is? White space was meant to make code look
clear. The computer doesn't care about white space because it can't read
your code. People need white space to read your code.
Braces and Blocks
Braces and blocks vary depending on the programmer. This can cause a
little confusion sometimes when source code passes from one programmer
to another. For instance the K & R style of looks like this:
int main()
{
int x = 1;
int y = 10;
while(x < y ){
printf("Value of x is %d\n", x);
x++;
}
}
This is how a lot of C programmers write their code. Some
programmers prefer to do their braces this way:
int main()
{
int x = 1;
int y = 10;
while(x < y)
{
printf("Value of x is %d\n", x);
x++;
}
}
I prefer the latter method because I can see where the block starts and
ends clearly. The K & R programmers will of course tend to disagree. The
only solution to this is to get used to both styles of indenting, but
when writing a program, be consistent with what you use. If you use K &
R, then use K & R for the program. Don't suddenly change it halfway
through.
Abusing "if"
Some people love using "if" statements so much they come up with the
following:
if(a == 0)
{
a++;
return(a);
}
if(a == 1)
{
a += 5;
return(a);
}
if(a == 2)
{
a += 10;
return(a);
}
if(a == 3)
{
a += 20;
return(a);
}
if(a == 4)
exit(1);
|