2012

2012 was one of the worst movies of all time. The year 2012, however, was not so bad. Of course not everything went the way I wanted but overall it was not a bad year at all. My new year resolutions included five things and I achieved three of them. 60% success – that is a new record for me. Usually my new year resolution success rate is 0%.

My new year resolutions are usually fringe things on which it is hard to concentrate on for prolonged periods of time, especially when life presents more important challenges to deal with. After years of setting goals for the new year and failing to get anything done consistently, now I know that the trick to having new year resolutions that you can achieve is to have a few of them (2 or 3) that you really really want. ”Good to have” things just don’t cut it. It should be “must have” things – things that are really important to you. And it should be no more than two or three things.

An important thing to keep in mind while executing your resolutions is to evaluate your progress continuously. You should do that at least every three months. That will provide a good deal of motivation. If you are running behind, it will give some renewed enthusiasm to get back on track. If you are on track, you will get a feeling of satisfaction and deep down you will know that this is something you can do. Either way, you win.

The things I did not achieve for 2012 were reducing my weight by 15 lbs and studying algorithms and data structures. At one point I reduced 5 lbs without any kind of dieting. I was exercising regularly and playing volleyball. Then I injured my wrist while playing volleyball and I stopped the exercising and volleyball. I gained back all the weight in a couple of weeks.

One of things I wanted to achieve was to write 12 blog posts in this year. You guessed it, this is the 12th one. If I didn’t have it as a goal I probably wouldn’t write this one. Not that it makes much difference but it makes me understand that having a goal puts one in a more advantageous position to achieve it. So I will make new resolutions for 2013 – maybe 2 or 3 ones that I really really want to achieve. The kind of ones that will keep the fire burning, the ones that makes it worth the effort.

So long, 2012. It was nice knowing you.

No country for old men

Like many brand conscious and price conscious people in America, we went for some midnight shopping yesterday. Black Friday, as it is called, is when people who are normally normal loses all sense of decency and common sense and push the people in front of them in the queue, or trample over them, with a sense of urgency seen on a sinking ship, and fill their shopping carts with junk that they will probably never use and then stand in a queue for an hour or two to give their hard earned cash to giant corporations so that their CEOs can buy yet another villa in the South of France.

While all that madness is quite disturbing in its own sense, what disturbed me even more is that I found that I don’t really have the energy or inclination for this kind of partying anymore. I still like to buy all that junk that I don’t need and then worry about where I can find the space for it, I found myself a little separated from the crowd. I am figuratively speaking here, because I would have loved to separate myself my those people knocking me over in a hurry to get to that gadget that they so desperately want, but there was no space in the mall for that kind of tranquility. You just keep moving forward in that river of people, flaying your arms around, grabbing at whatever pieces of merchandise you can get your hands on. What surprises me is that we are supposed to be in some kind of recession. I shudder to think of days were everybody is doing well. Anyway, as I was saying, I found myself quite tired very soon. The passion to amass pieces of junk, which I showed so furiously in the previous few years, was missing. My legs were aching, my hands tired and the spirits quite low.

I wonder if this is a sign of getting older ? Would I never again enjoy losing a night of sleep for the simple joy of watching capitalism in action ? Am I getting so mature that material wealth doesn’t give me any pleasure ? I hope not, because what is life if you can’t push some smelly college kids our of your way, trample over a few fellow country men and grab that last piece of gadget on sale.

Getting caller information in C# 5.0

In application log files, sometimes it is really useful to see the name of the method that wrote the log, the source file and the line number. If we were using C++, this information would be readily available to the logger using some convenient macros. But if we are programming in C# 4.0 or earlier, we had no way to get that information automatically if we don’t have the debugging symbols (except for the method name) and we don’t usually deploy the pdbs along with the binaries. Even if we did, the line number is almost always different between debug and release builds. Unless we build our binaries in debug mode and then deploy the pdbs along with the binaries, which is not usually the case, we are out of luck. The other disadvantages of using the StackFrame class to get the method information is that it is slow and if your code is obfuscated, the name of the calling method will not be what we expect.

The way I have seen it in most logging code in C# is the calling method’s name is passed into the logging method as a string but the caller’s source file and line number are usually not logged. Although it is possible to pass in all this info to the logging method, it is such a chore and a maintenance nightmare. The name of the method, the source file and line number can all easily change and one can just as easily forget to update the logging code. Anyway who wants to pass in all that information when all they want to do is log a message.

C# 5.0 introduces a way to get the caller information in an easy and non-intrusive way. While the design is not what I would call the most intuitive, I am happy that it works with minimal disruption to our existing code. We can get three pieces of caller information using three attributes that can be applied to optional parameters of a method which is interested in getting information about its caller. The attributes are

  • CallerMemberNameAttribute
  • CallerFilePathAttribute
  • CallerLineNumberAttribute

Here is an example of how we will use these attributes:

    publicstaticclassLogger

    {

        publicstaticvoid Log(

            string message,

            [CallerMemberName] string caller = “”,

            [CallerFilePath] string filePath = “”,

            [CallerLineNumber] int lineNumber = 0)

        {

            Console.WriteLine(

                “{0}. Caller {1}, src file: {2} on line #: {3}”,

                message,

                caller,

                filePath,

                lineNumber);

        }

    }

The compiler will automatically fill in the correct values for these optional parameters at compile time. How convenient !

Read more here.

Canadian immigration for Software Engineers

If you are a software engineer looking to immigrate to Canada, or at least apply for permanent residency as a back up plan while waiting for your US green card, you might be interested to know that you can apply for PR in Canada this year. A couple of years ago, occupations for software engineers were removed from the list of occupations eligible to apply for PR in Canada. If you were disappointed that you didn’t apply for it before it removed, then this your chance. Read more details here.

async and await in C# 5

C# 5 comes with just a few new features and async is arguably the more important of those. I have started looking into it a couple of days ago and I thought I would share my thoughts on it here.

Asynchronous programming was touted was the next big thing for quite some time, but it didn’t hit mainstream in the .NET world till recently. Well, people were always using threads from .NET 1.0 but it was hard and error prone to write multi-threaded programs. Library support and tool support was lacking. The Task Parallel Library (TPL) introduced in .NET 4.0 was a step in the right direction. In this context, making asynchronous programming easy for C# programmers is probably the right thing to do as far as Microsoft is concerned.

Two new keywords have been introduced in C# 5.0 to make working with asynchronous code easier – async and await.

The async keywords is used to mark a method as asynchronous. It is required to use the await keyword inside that method. Note that just marking a method with async does not make it asynchronous. In face you can write fully synchronous code in a method marked as async, and the compiler will be ok with that.

        async void NothingAsynchronousHere()

        {

            Console.WriteLine(“This method is executed synchronously”);

        }

Actually, the compiler is not completely ok with that, when you compile the code you will get a warning:

“This async method lacks ‘await’ operators and will run synchronously. Consider using the ‘await’ operator to await non-blocking API calls, or ‘await Task.Run(…)’ to do CPU-bound work on a background thread.“

Now let us look at an async method which is actually asynchronous:

        async Task<string> GetWebpage(string url)

        {

            WebClient client = newWebClient();

            var response = await client.DownloadStringTaskAsync(url);

            return response;

        }

This method shows how the await keyword is used. This method looks like a synchronous method, but it actually executes in 2 phases. The calling thread executes upto client.DownloadStringTaskAsync() (which is an asynchronous method and it returns immediately). After that the thread immediately returns to the caller of GetWebpage. The caller can chose to wait (remember, GetWebpage returns a Task<>) or continue its own execution (and maybe later get the result of the download using the Result property of the Task<>). Once the download completes, the rest of the lines in the async method is executed. Usually these lines of code are executed on the same thread as the calling thread (which means you don’t have to worry about accessing UI controls if the async method was invoked using an UI thread) but there are some cases where it won’t be the case (an example being using a Threadpool thread).

Here are some things to remember. An async method can return only void, Task or Task<>. Otherwise the compiler will complain:

The return type of an async method must be void, Task or Task<T>

Another thing to keep in mind is that you can await only on methods that returns Task or Task<>. This means that you cannot await on WebClient.DownloadStringAsync() since its return type is void.

Recharging my batteries

I took one week off from work to do nothing. Just to sit at home and relax. Usually vacation turn out to be more stressful than going to work. Not that I enjoy travelling, which I do, but more often than not there is very little relaxing going on. So I thought, why don’t I take a week off and do nothing but relax ?

I am 3 days into the relax-acation so far and I am loving it. I am reading stuff that I have been postponing for quite some time, studying some machine learning and big data stuff, watching movies that I want to, waking up early to go the gym, taking naps – it is turning out very well for me.

If you are thinking about one, I highly recommend it.

Mid-year check-in

Summer is already over. 2012 ends in a little more than 4 months. I am now 34 years old  and my nephew, whose birth coincided with my arrival in US, is now 6 years old. We celebrated our 3rd wedding anniversary last weekend. I can’t imagine how quickly time is flying.

Forbes suggests that it is a good thing to do a mid-year check-in on yourself. Sounds like a good idea. So I thought now would be a good time to review my new year resolutions for 2012.

  1. Reduce my weight by 15 lbs – While I haven’t reduced any weight at all, my overall physical health has improved. I believe I have gained some muscles and increased my flexibility by a little bit. That would have been a better goal anyway. But, whatever.
  2. Bring my cholesterol to normal level – Done. Ok, ok.. I cheated on this one. I am now taking prescription medicine which has brought the cholesterol levels to normal.
  3. Increase my knowledge of algorithms and data structures. This will be like part 2 of my resolution of 2011. As part of this I will be taking the design and analysis online class from Stanford - I did not take that course nor did I study anything to improve my knowledge of Algorithms and Data structures.
  4. Write at least 12 blog posts. One per month on average. This is the first one. – I wrote five posts so far, this is the sixth one. Not great, but very much possible to hit the target. Also I am not very happy with the quality of the posts but I think I am going to ignore that for now :-)
  5. Increase my volleyball skills. I have registered for a volleyball clinic with my friends and I hope that will help me improve my skills. Also reducing my weight should help me jump higher which will allow me to play better. – I have made some improvements on this front. But I haven’t played volleyball in a couple of months. I was concentrating on cricket. And I am not sure my knees will allow me to play much volleyball. That is so sad because I love playing volleyball.

As you can see from above, I haven’t made any major strides so far with regard to my resolutions for this year. I was recently reading a book about new year resolutions and one of the points that the book makes is that unless your resolution is about something that you really really want from the bottom of your heart, there is a good chance that we will drop it. I think that is true. I selected things that were good to have, things that I would be happy if I achieved it. But I don’t think any of them were about things that I really really wanted; things for which I would be willing to make sacrifices. I would keep that in mind when I make resolutions for next year.

Learning from Data

A new trend has started with the online Artificial Intelligence class taught by Sebastian Thrun and Peter Norvig in partnership with Stanford. Following the success trail blazed by Thrun and Norvig, quite a few courses have followed suit:

Good times, huh ?

Caltech has followed suit with Learning From Data, a course on machine learning taught by the well known Yaser Abu-Mostafa. The course started on April 3rd and will have classes twice a week. You can watch it live or view the recorded video later.

Adding tracing to your code

One thing you might want in your code is to add tracing to it so that your application log file will contain entries for all function entries and exits. This is tremendously useful when debugging a running application especially when you cannot connect a debugger to it for any reason. It is also very helpful when your application behaved unexpectedly once and you can’t get to repro it while you have a debugger attached. In those cases your log file is your best friend. Assuming that your log file has all the information that you need to debug the issue. Tracing all the function calls is a big help.

You don’t want to do this:

bool SaveUserInfo(User* pUser)
{
    log.Trace("Entering function SaveUserInfo");

    Database.Connect(connectionString);
    Database.Update(GetUserUpdateSql(pUser));
    Database.Disconnect();

    log.Trace("Exiting function SaveUserInfo");

    return true;
}

It should be obvious that this approach is both tedious and error prone, especially with future changes. For example if we add a parameter validation such as the one given below:

bool SaveUserInfo(User* pUser)
{
    log.Trace("Entering function SaveUserInfo");

    if (nullptr == pUser)
    {
        return false;
    }

    Database.Connect(connectionString);
    Database.Update(GetUserUpdateSql(pUser));
    Database.Disconnect();

    log.Trace("Exiting function SaveUserInfo");

    return true;
}

Ofcourse we can fix this in multiple ways. We could add a trace just before returning from the validation failure block. Or we could add a goto towards the end of the program where we trace the function exit. Or we could just restructure the program as below:

bool SaveUserInfo(User* pUser)
{
    log.Trace("Entering function SaveUserInfo");

    bool retValue = false;

    if (nullptr != pUser)
    {
        Database.Connect(connectionString);
        Database.Update(GetUserUpdateSql(pUser));
        Database.Disconnect();

        retValue = true;

    }

    log.Trace("Exiting function SaveUserInfo");
    return retValue;
}

But you get the point, this is not a good solution.

So what is the solution. I had talked about RAII in a previous post. We could employ the same concept here. We can have an object created on the function stack frame. The constructor will trace the function entry and destructor (which is guaranteed to be invoked on function exit) can trace the function exit. The only thing the developer need to know is that a function trace object should be created on the stack and it should be the first line of the function. Here is some sample code.

class FunctionTrace
{
    CString _functionName;

public:
    FunctionTrace(CString functionName)
    {
        _functionName = functionName;
        log.TraceEnter(_functionName);
    }

    ~FunctionTrace()
    {
        log.TraceExit(_functionName);
    }
};

Using the FunctionTrace class is trivial.

bool SaveUserInfo(User* pUser)
{
    FunctionTrace ft("SaveUserInfo");

    if (nullptr == pUser)
    {
        return false;
    }

    Database.Connect(connectionString);
    Database.Update(GetUserUpdateSql(pUser));
    Database.Disconnect();

    return true;
}

As you can see, exiting from the function in the middle is not a problem anymore. The code is much more compact and clean.

One thing I would like to avoid is having to specify the name of the function myself. It is a problem because it is more typing and also because there is a chance that I would rename the function some time in the future and forget to update the string parameter to the FunctionTrace constructor. It is just too easy to miss that. The compiler will not be able to help me catch that oversight.

But the preprocessor can help. Lucky for us, Visual C++ defines a macro __FUNCTIONW__ that we can use to get the function name. One could easily write a macro that encapsulates this knowledge and makes sure that a FunctionTrace instance is created on the stack and passes the __FUNCTIONW__ to the constructor of FunctionTrace. We could even modify the trace to include the file name and line number using the __FILE__ and __LINE__ macros. Now the client code becomes much more simple.

bool SaveUserInfo(User* pUser)
{
    // macro that creates FunctionTrace instance 
 // on stack and passes in the name of the function. 
    FUNCTION_TRACE(); 

    if (nullptr == pUser)
    {
        return false;
    }

    Database.Connect(connectionString);
    Database.Update(GetUserUpdateSql(pUser));
    Database.Disconnect();

    return true;
}

The FUNCTION_TRACE could be implemented like this:

#define FUNCTION_TRACE() FunctionTrace fn(__FUNCTIONW__)

If we modify FunctionTrace class to trace the line number and file name, we could pass those information from FUNCTION_TRACE as follows:

#define FUNCTION_TRACE() FunctionTrace fn(__FUNCTIONW__, __LINE__, __FILEW__)

This assumes that FunctionTrace constructor now takes two strings and an integer.

class FunctionTrace
{
public:
    FunctionTrace(
        LPWSTR functionName,
        int lineNumber,
        LPWSTR fileName);

    ~FunctionTrace();
};

Have fun tracing.

Broken Windows

Once upon a time, in a place far far away, there was an idyllic small town. The town had a beautiful street with elegant houses on either side and big green trees gave shade to the sidewalks. The houses had large french doors and windows made with expensive panes of glass. Kids played ball on the street. It was the most desirable neighborhood in the town.

One day, the kids playing on the streets accidentally broke one of the glass window of his house. The kids fled fearing that the house owner would get mad and would complain to their parents. But that did not happen because the house owner was away on a long vacation. The window remained broken . The kids stayed away from the street for a few days and then they started to return and saw that the window was still broken. Nobody was mad, nobody had complained to their parents. Nobody seemed to care about the broken window. They could take a hint. More windows were broken. Graffiti appeared on the walls. Anti social elements started hanging around. Windows of other houses started to get broken. People started to move to other parts of the town leaving behind houses with broken windows and graffiti on the walls.

You might have got a hint of what I am getting at here with this little story showcasing the broken windows theory.

Wikipedia – “The broken windows theory is a criminological theory of the norm setting and signaling effects of urban disorder and vandalism on additional crime and anti-social behavior.”

So what is my point, you ask.

Well, I am trying to make a case for fixing the broken windows in your codebase.

Poorly designed classes, sloppy coding, deviations from established coding standards and idioms and best practices, cutting corners with error checking etc. are all broken windows in your code. Just one occurrence is all it needs to start the degradation of your code quality. A new person to the team will subconsciously feel what goes and what does not in the codebase and starts behaving accordingly. Fixing the broken windows should be as much of a priority as other engineering excellence activities. You can’t really prevent windows from getting broken. You might write some sloppy code if you were tight on time. Your design might turn out to be unsuitable or insufficient because you didn’t have all the information when you were designing your code. Or the requirements could have changed. Whatever the reason, the quality of the design and code will continue to degrade as the project moves forward unless conscious effort is made to keep it pristine. This rotting of the software over time, when no effort is made to prevent it, is known as software entropy.

So why don’t we all do it ? Why is broken windows in our code more common than it should be ?

Because it is usually a hard sell to your manager. Managers have a commitment to deliver something on time. Their performance reviews, bonus and sometimes even their job depend on delivering. So it is not too hard to understand that from the manager’s perspective delivering is usually more important than refactoring existing code to maintain the code and design quality. I understand that I am making some generalization here. Not all managers are like that.

What is important is that you should make the case for maintaining the code in a pristine condition. You should argue for it. If you don’t win, that is OK. At least you tried. You should be able to sleep better at night. What is unacceptable is knowing that something is broken and pretending that it is not or pretending not to see it or acknowledging it. There is no excuse for that.