VERC: Coding Style Last edited 5 years ago2019-04-17 11:24:47 UTC

You are viewing an older revision of this wiki page. The current revision may be more detailed and up-to-date. Click here to see the current revision of this page.
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.
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.