Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C++
Article

Programming Tips

Rate me:
Please Sign up or sign in to vote.
2.89/5 (36 votes)
17 Feb 20033 min read 64.5K   34   11
Tips to write useful code

Introduction

I assume you are downloading some source code from time to time. You can learn a lot about a programmer while looking at his source code. There are some tips to help you write clear and effective code. So let's skip this boring introduction and go strait to the article!

Some tips

When naming a variable or a function, try to choose a significant name to help you (and your co-workers) understand the code. You can also use Hungarian notation.

Check the following code. Can you understand what it does?

a=Func1();
b=Func2();
if (a<b)
{
    if (!Func3())
    {
        Func4(1);
        return;
    }
    Func5(2);
}

Now let's write it again:

dtLastBackup=GetLastBackupDate();
dtCurrent=GetCurrentDate();
if (dtLastBackup<dtCurrent)
{
    if (!DoBackup())
    {
        ErrorMsg(IDS_ERRORBACKUP);
        return;
    }
    MessageBox(IDS_BACKUPSUCCEEDED);
}

Now we can understand the code. It is also a good idea to prefix member variables and global variables.

g_szPath=GetPath();
m_nCount=0;

Here we can understand immediately that g_szPath is a global string variable while m_nCount is an integer member variable.

Use uppercase in each word of a variable/name: Use "GetPathString" instead of "getpathstring".

Try to avoid using global variables and static functions. Use them only when necessary. Use member variables instead.

Add a comment to each major function and each block in your code. This way you and your co-workers can understand what is the purpose of a block/function.

Try to write "beautiful" code to be able to understand it.

for (int y=0;y<20;y++) 
  {for (x=0;x<20;x++) cout<<"x";cout<<"\n";}
for (int y=0;y<20;y++)
{
    for (x=0;x<20;x++)
        cout<<"x";
    cout<<"\n";
}

Try to use objects when you need to write initialization code. This way you won't forget to release memory. You can also use smart pointers instead of regular ones.

For example, you can write a class to wrap a database object, so you won't forget to close it. It will be closed automatically when the object is being destroyed.

class DBClass
{
protected:
    DB m_db;
public:
    ~DBClass()
    {
        Close();
    }
    bool Open();
    bool Close();
    DB operator->()
    {
        return m_db;
    }
};

void InsertTable(const char *szTableName)
{
    DBClass db;
    if (db.Open())
        db->Insert(szTableName);
}

Use resources for strings and macros/enums for integers. This way you won't have to follow all your code while changing it.

void ResetBoard(char pBoard[20][20])
{    // Suppose you want to switch to 25x25 board???
    for (int x=0;x<20;x++)
        for (y=0;y<20;y++)
            pBoard[y][x]=0;
}

// We can change the board size by changing only one line
#define BOARD_SIZE 20

void ResetBoard(char pBoard[BOARD_SIZE][BOARD_SIZE])
{
    for (int x=0;x<BOARD_SIZE;x++)
        for (y=0;y<BOARD_SIZE;y++)
            pBoard[y][x]=0;
}

Split your project into several files grouped by functionality. This way you can reuse your code easily. This is also useful when you follow your functions flow.

Use functions/macros instead of copy/paste your code. If you want to change something, you need change it only at one place.

ALWAYS handle errors/exceptions! Don't let your application crash due to an exception you forgot to catch.

Use TRACE/ASSERT as much as possible. They don't affect the release build but they can save you a lot of time while debugging.

Take some time to search for existing code instead of reinventing the wheel. For example: Suppose you want to download a file from the internet. Why open socket, parse headers (HTTP) and read the data when you can use existing objects to download files for you?

When using multithreading programming, don't forget to use critical section/mutex wherever we are using shared variables.

Use _T and text macros so you'll be able to build a Unicode version of your application. For example, use _tcslen/lstrlen instead of strlen.

Use sizeof and don't assume you know the size of your variable/struct. This creates a portable code. Use fwrite(&nCount,sizeof(nCount)); instead of fwrite(&nCount,2);. An integer may be 2 bytes or 4 bytes long. Let the compiler define it.

Avoid using the heap. Try to use stack based variables. Don't be afraid of "stack overflow". The stack limit is equal to your memory limit. This way you can also avoid defragmentation of your memory.

Try to use standard libraries instead of others. Consider using STL instead of MFC when possible. This way you write a portable code.

Last but not least: BACKUP, BACKUP and BACKUP! You spend half a year working on your application just to find out that your HD crashed. Consider using CVS to track changes.

These are some basic tips. I am sorry for my poor English but I hope you can understand it anyway. If you have any other tips, you can send me and I'll add it to the article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

Comments and Discussions

 
GeneralRegarding JSP and SQL Pin
ManoSh8512-Mar-06 0:31
ManoSh8512-Mar-06 0:31 
QuestionAlways handle exceptions?? Really? Pin
4space26-Feb-03 0:54
4space26-Feb-03 0:54 
GeneralImprovement Pin
Manish K. Agarwal25-Feb-03 4:58
Manish K. Agarwal25-Feb-03 4:58 
GeneralRe: Improvement Pin
Gilad Novik25-Feb-03 5:04
Gilad Novik25-Feb-03 5:04 
GeneralNice.. Pin
Anonymous18-Feb-03 20:09
Anonymous18-Feb-03 20:09 
Generalstack Pin
Goran Mitrovic18-Feb-03 12:26
Goran Mitrovic18-Feb-03 12:26 
GeneralRe: stack Pin
Gilad Novik18-Feb-03 20:51
Gilad Novik18-Feb-03 20:51 
GeneralRe: stack Pin
Goran Mitrovic19-Feb-03 11:59
Goran Mitrovic19-Feb-03 11:59 
GeneralRe: stack Pin
Gilad Novik19-Feb-03 20:31
Gilad Novik19-Feb-03 20:31 
GeneralRe: stack Pin
David Crow24-Nov-03 7:09
David Crow24-Nov-03 7:09 
GeneralGood :) Pin
S O S18-Feb-03 3:22
S O S18-Feb-03 3:22 
All good advice Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.