Sunday, November 4, 2007

List of BAD Coding Conventions

1. Using unnecessary double pointers...

GetNodeName(Node** ppNode)

{

return *ppNode->GetName();

}

U should only use a double pointer when you are allocating your memory with new or malloc() inside a method and passing a pointer as argument to get the newly allocated memory address.

Example:

BOOL COM_CREATE_OBJECT(ComObj** pObj)

{

ComObj* pNewObj = new ComObj;

if(pNewObj)

{

*pObj = pNewObj;

return TRUE;

}

return FALSE;

}

void main()

{

ComObj** pObj = NULL;

// in this case you have to use double pointer.

COM_CREATE_OBJECT(&pObj);

}

2. Converting TCHAR to WCHAR (or WCHAR equivalent), or TCHAR to char (or char equivalent), by simple type casting..

Example:

WCHAR* nodeName = xmlPareser->GetNodeName();

TCHAR* convertedName = (TCHAR*) nodeName;

3. Assuming WCHAR, or wchar_t to be a typedef of unsigned short...

4. Using... wierd macro function names such as Z(), W(), X().. etc..

Example:

WCHAR* fileNameW = GetFileNameW();

TCHAR* fileName = X(fileNameW)... // here x is a function which converts wchar to tchar...

5. Returning temporary pointers as return type

Example:

TCHAR* convertUnicodeToTChar(WCHAR* str)

{

TCHAR buf[255];

wcstombs(buf, str);

// returning temporary pointer ... very dangerous indeed.., maybe u can
// use a global or member variables in this case..

return buf;

}

6. Doing Too Much Processing (such as running a big for loop) in OnMouseMove(), OnEraseBkGnd, OnPaint(), OnCmdUI() etc.

No comments: