Sunday, May 11, 2008

Why write Unit tests

One of the reasons we don't refactor our code is that we are afraid that it might break something. But, if you have unit tests in place then, every time you change something, you have your unit test to rely on. It not only saves your time to verify the output, but it also assures you that nothing is broken.

Saturday, May 10, 2008

char and its relatives

In C, I was used to saying

char *psz = "This is some string";

here's what changes in VC++ :

VC++ has the ability to use multibyte characters and Unicode characters - this can be set using IDE options.

Between multibyte character system (MBCS) and Unicode, Unicode has greater acceptance, so we usually program in Unicode. Now to do that first all the string should be defined like this :

wschar_t *buff = L"This is some string";

here, the prefix L tells the compiler that the string is made up of Unicode chars and since char represents a 8 bit character, we need another datatype to represent Unicode char, so we have wschar_t (16 bit Unicode)

However, you might need to switch between ANSI string and unicode strings, to support such a situation, VC++ gives us  a macro TCHAR. It expands to wschar_t  if Unicode is defined else to char.

similarly, instead of harcoding "L" prefix, we again have an option in MFC to use a macro "_T"

We can write the macro ourselves in SDK as follows or include tchar.h

#ifdef UNICODE
#define _T(x) L##x
#else
#define _T(x) ##x
#endif

so now we can write

TCHAR *psz = _T("This is some string");

Modal vs Modeless Dialogs

Modal Modeless
Application control is lost is not lost
DialogBox API is used CreateDialog API is used
EndDialog is used to destory it DestroyWindow is used
Implements it own message loop uses application's message loop
behaves like a synchronous call behaves asynchronously

Calling conventions for functions

Finally I got to know what WINAPI is :)

it is a calling convention.

When a function call is made, you have a choice on how to pass function variables to the stack, they can be passed right to left or left to right, also there is a choice on whether the caller or the callee should clear the stack.

Based on these choices and some other things, we have different calling conventions :

__stdcall or CALLBACK or WINAPI,__cdecl,__fastcall,__thiscall

Check out the following links for details:

http://msdn.microsoft.com/en-us/library/984x0h58.aspx

http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx

http://www.cs.cornell.edu/courses/cs412/2001sp/resources/microsoft-calling-conventions.html