linuxnewbie.org.gif
Tuesday, 12-Dec-2000 10:38:27 EST
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 Today
Linux Central
Linuxnewbie.org
PHPBuilder
Just Linux
Linux Programming
Linux Start
BSD Today
Apache Today
Enterprise Linux Today
BSD Central
All Linux Devices
SITE DESCRIPTIONS
[-What's New-]
Order a Linuxnewbie T-Shirt
Easy Webcam NHF
Directory Navigation NHF
Installing Snort 1.6.3 on SuSE 6.x-7.x
Customizing vim
The SysVinit NHF
Installing ALSA for the VT82C686 integrated sound
USB Creative Video Blaster II for Linux
Configuring the Intellimouse Explorer in XFree86 V4+
The beginnings of a distro NHF
Getting Past Carnivore?
Getting and Installing PGP
Getting your ATI Rage 128 Working
How to create a multiple partition system
Using Fdisk
Introduction to Programming in C/C++ with Vim
Adding a Hard drive in Linux -- In five steps
Installing ALSA for the Yamaha DS-XG Sound Card
Getting your Diamond Rio Mp3 Player to work with Linux
Bash Programming Cheat Sheet
Installing NVIDIA Drivers for Mandrake
Setting up Portsentry
Hard Drive Speed Tweak for Linux
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-]
The beginnings of a distro NHF
Connecting to the Internet using KPPP
Getting your SBLive to work
Unreal Tournament NHF
LWE Day 2 Pictures
LWE Day 1 Pictures
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-]

Copyright 2000 internet.com Corp. All Rights Reserved. Legal Notices Privacy Policy

internet.com.gif