★ wanayoo — archive 1999 http://www.linuxnewbie.org/nhf/intel/programming/programpractice1.htmlNouvelle recherche | Portail wanayoo
linuxnewbie.org.gif
Thursday, 08-Jun-2000 09:40:27 EDT
Newbized Help Files articles discussion board bookshelf sensei's log advertising info
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);

[-NHF Control Panel-]
The Linux Channel at internet.com
Linux Planet
Linux Central
Linuxnewbie.org
PHPBuilder
Just Linux
Linux Programming
Linux Start
BSD Today
Apache Today
SITE DESCRIPTIONS
[-What's New-]
Order a Linuxnewbie T-Shirt
Installing NVIDIA Drivers for Mandrake
Setting up Portsentry
Gentus 2.0 First Look
Hard Drive Speed Tweak for Linux
Installing SSH-2 on SuSE 6.x and other System V based systems
Installing Libsafe for SuSE 6.x
Getting Your Sound Blaster to Work in Slack 7
Wheres the RAM?
How do I create a boot disk in Linux?
Scheduling in Linux
Overcoming LILO dual-boot problems
Tripwire for SuSE
PHP & Apache intro
Good Programming Practice
Getting Online with
Linux via Windows
Book Review:
Linux Programming White Papers
Linux 2.4.x: What's New?
Filesystem, Directories, etc.
Linux World Expo
Licq NHF
Connecting to the Internet using KPPP
Sensei's Log
Chat room
Join: Linuxnewbie.org SETI Black Belts!
Send in your news
Click the image to add Linuxnewbie.org to your MyNetscape Page
[-LNO Newsletter-]

[-Archive-]
Getting your SBLive to work
Unreal Tournament NHF
The LNO FAQ!
WoW (Words of Wisdom)
Other sites news
What is Linux?
What is Linux? part deux (ups & downs)
Search newsgroups
The List
ALS Report
Feedback Form
jobs.linuxtoday.com.gif
Match: Format: Sort by:
Search:
[-Quick Links-]
Linux-Mandrake
Linux Start
Linux.com
Linuxhelp.org
Linuxtoday.com

Copyright 2000 internet.com Corp. All Rights Reserved. Legal Notices Privacy Policy
Linuxnewbie.org uses newspro for its news script

internet.com.gif