Click here to Skip to main content
12,546,448 members (56,503 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked

Programming Tips

, 17 Feb 2003
Rate this:
Please Sign up or sign in to vote.
Tips to write useful code


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?

if (a<b)
    if (!Func3())

Now let's write it again:

if (dtLastBackup<dtCurrent)
    if (!DoBackup())

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


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++)

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
    DB m_db;
    bool Open();
    bool Close();
    DB operator->()
        return m_db;

void InsertTable(const char *szTableName)
    DBClass db;
    if (db.Open())

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++)

// 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++)

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.


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


About the Author

Gilad Novik
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"

You may also be interested in...


Comments and Discussions

GeneralRegarding JSP and SQL Pin
ManoSh8512-Mar-06 0:31
memberManoSh8512-Mar-06 0:31 
QuestionAlways handle exceptions?? Really? Pin
4space26-Feb-03 0:54
member4space26-Feb-03 0:54 
GeneralImprovement Pin
Manish K. Agarwal25-Feb-03 4:58
memberManish K. Agarwal25-Feb-03 4:58 
GeneralRe: Improvement Pin
Gilad Novik25-Feb-03 5:04
memberGilad Novik25-Feb-03 5:04 
GeneralNice.. Pin
Anonymous18-Feb-03 20:09
sussAnonymous18-Feb-03 20:09 
Generalstack Pin
Goran Mitrovic18-Feb-03 12:26
sussGoran Mitrovic18-Feb-03 12:26 
GeneralRe: stack Pin
Gilad Novik18-Feb-03 20:51
memberGilad Novik18-Feb-03 20:51 
GeneralRe: stack Pin
Goran Mitrovic19-Feb-03 11:59
sussGoran Mitrovic19-Feb-03 11:59 
GeneralRe: stack Pin
Gilad Novik19-Feb-03 20:31
memberGilad Novik19-Feb-03 20:31 
GeneralRe: stack Pin
DavidCrow24-Nov-03 7:09
memberDavidCrow24-Nov-03 7:09 
GeneralGood :) Pin
Locked Ghost18-Feb-03 3:22
memberLocked Ghost18-Feb-03 3:22 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 18 Feb 2003
Article Copyright 2003 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid