Check out Half-Life Re-imagined competition results!
Check out Skewing textures in Hammer, our newest tutorial!
Say hello to saviorofearth, our newest member!

logo

Site Stuff

Reference

Maps

Community

ShoutBOX

Poll

Feeling Blue

What's your favourite shade of blue?

Azure

13

Cobalt

32

Turquoise

9

Cyan

10

Royal

8

Teal

3

Onliners

10 mins

Solokiller

16 mins

The Mad Carrot

21 mins

Dr. Orange

22 mins

UrbaNebula

27 mins

JeffMOD

36 mins

Instant Mix

40 mins

Jessie

Affiliates

A gaming and technology blog by TWHL admins Penguinboy and Ant. A music blog by TWHL users Ant and Hugh.

Coding Style

By Philip 'Cecil' Nilsson

Have you ever seen anything like this?

class AbC {
protected:
private:
public:  AbC();
int A(int); };
AbC::AbC() {return;}
int AbC::A(int c){
int b;
for(int a=0;a<c;a++) b++;
return b;
}
Not that easy to understand as something like this, eh?
class CAbc
{
    public:
        CAbc();
        int A(int count);
};

CAbc::CAbc()
{
    return;
}

int CAbc::A(int count)
{
    int b;
    for (int a = 0; a < count; a++)
    {
        b++;
    }
    return b;
}
Ask yourself now: how can I write code as nice as that?

There are a few things to think about to make your code look nicer and to make it more easy to read:

  • Indenting

    Indenting looks really nice and also helps alot when trying to find missing brackets ;) The most standard indention I can think of is four spaces. Tabs are not that good as they are different on most systems. Tabs are ok though, but I'd rather stick to spaces. Take a look at this:

    int a,b;
    if (a == 0){
    b = 2;
    for (int c = 0; c < b; c++){
    b++;
    }
    } else if (a == 2) {
    b = 3;
    }
    versus
    if (a == 0)
    {
        b = 2;
        for (int c = 0; c < b; c++)
        {
            b++;
        }
    }
    else if (a == 2)
    {
        b = 3;
    }
    

  • Spacing

    By spacing I mean adding spaces between variables, operators, constants and wherever else you can add some spaces in to improve readabilty. Take a look at this example:

    int a=3;
    printf("a%sasd",string);
    int a=2+3.4-4;
    
    and the good one ;)
    int a = 3;
    printf ("a%sasd", string);
    int a = 2 + 3.4 - 4;
    

    As you can see, I added some spaces around the = operator, some spaces should be added around all operators, by the way, as I did to the expression underneath ;) I also added some spaces between the function and it's arguments. That looks good but doesn't really help -- you can skip it if you wish. Adding a space after every comma (",") is smart as it keeps the different arguments apart so you won't miss one.

  • Variable names

    Naming your variables right really helps during devlopment. For example, if you have a variable somewhere in the code called fubar, you'll have no idea what it could be. If it instead was called iPlayerCount, you would immediately see that it was an integer that counts the players. The question is: How do I name my variables in a good way? One of the more popular naming conventions is Hungarian Notation. Learn more about it here.

  • Comments

    Commenting your code is useful if other people are working on the project. It also helps if you're going back to an old project you don't remember much about. You should put comments everywhere that there are big ugly chunks of code that were hacked together. Most functions should also have a little description. Having a description of the file at the top of all files is good for people new to your project. The function descriptions will also help them. Commenting ugly code is good, but it's better to rewrite it so that it's more understandable. It might not be possible in all cases, so commenting is good :)

    Relevant Links:
    Some Hungarian Notation over at MSDN
    Some Brief Hungarian Notation
    PEAR Coding Standards(PHP)

    This article was written with C++ in mind, but all languages that look like C will probably work. I also had PHP in mind while writing it.