Monday, July 21, 2008

Meaningful Identifiers

Anthony, _anthony and anthony were good friends. Anthony is a grown up guy,_anthony is a boy and anthony is a dog. One day, Anthony invited _anthony and anthony to his place for the party. _anthony was not feeling well but decided to tag along anthony. _anthony is too young to drive so they decided to walk to the Anthony’s place. Unfortunately, anthony could not walk so they called a cab. _anthony…

Who cannot walk? A dog or a boy…

Let’s re-factor this story a little bit. Let’s rename Anthony to MAnthony, _anthony to BAnthony and anthony to DAnthony. M for men, B for boy and D for dog.

With these new identifiers this story will be much better [to read and understand]. As in any story or a novel, meaningful identifiers are really important. Identifiers in any source code provide a meaningful token for the underlying memory. Identifiers tell a lot about the program. Just looking at the identifiers like account, debit and credit one can figure out that the system is a financial system.

This is a good read for anyone interested in more details on the way we program

Monday, July 14, 2008

VS 2008 Debugger going crazy?

Look at the code below and put the breakpoint on line
if (_s == null)

observe the behavior when we do something like
s2 = Singleton.getSingletonInstance();


using System;

namespace Pattern
{
class Program
{
static void Main(string[] args)
{
Singleton s1, s2;

s1 = Singleton.getSingletonInstance();

s1.instanceCount = 100;

s2 = Singleton.getSingletonInstance();
}
}


sealed class Singleton
{
private static Singleton _s = null;
public int instanceCount;

private Singleton()
{
}

public static Singleton getSingletonInstance()
{
if (_s == null)
{
_s = new Singleton();
}

return _s;
}

}
}


When the breakpoint will hit, try watching the value of _s. The watch window will pop endlessly all over the screen when you click on the following + sign next to the static members icon.
Screenshot: