Wednesday, October 13, 2010

Dear C# Programmers, please use 'using' and 'IDisposable'

In C++ world, RAII (Resource Acquisition Is Initialization) is a widely accepted idiom. It used by std::auto_ptr<>, CComPtr<>, CWaitCurstor and all smart pointer implementations, streams, etc etc manage the lifetime of resources (e.g. memory, files, cursors, database connections etc). RAII ties the life time of a resource to a life time of a 'local object' (i.e. a object on stack). C++ guarantees that destructors of the objects on stack get call in face of exceptions, multiple returns from functions etc. However, the technique 'as it is' is not directly useful in C# since in C# all objects are created on heap.

Since it is a very useful technique, Microsoft added an interface IDisposable and a keyword 'using' to .Net. It gives all the advantages of RAII.

Recently I saw code similar to following
Stream astream=null;
    astream = new FileStream(...)
    // do some processing on stream  
    if( astream != null)
The developer was trying to ensure that stream is closed in case of any exception is thrown (or multiple returns). However, there is much simpler way to do achieve the same result. Rememer 'Stream' objects implement IDisposable interface.  So you can achieve this same effect simply by
using (astream = new FileStream(...))
    // do some processing on stream   
It is smaller, simpler and easier to understand. This code clearly indicates, what is the expected life of 'astream'.  With Garbage collector .Net (somewhat ) automates managements of one types of resource i.e. memory. However, there is no good way to manage other types of resources (like files) in face of exceptions where a 'guaranteed release' at certain point is required. Since GC doesn't really guarantee at what object will get garbage collected, tieing resource release to object lifetime doesn't work well. IDispose and 'using' keyword address this concern.
Even though this functionality is available in C#/.Net for a long time, many C# developers don't seem to use it. Probably because they are not really aware of the benefits. Hence my dear C# programmers, please learn about IDisposable/'using' keyword and use it regularly. It will make your life easier. In fact, I will recommend that as much possible make your own classes IDisposable

Sunday, October 10, 2010

The Design of Design : New book from Fred Brooks

Some time back I blogged about the 'Mythical Man month'. Recently I have started reading the new book from Fred Brooks.' The Design of Design'. Like Mythical Manmonth this book is also a collection of essays and it also a book where you read a chapter and then you need to think and reflect on the ideas/thoughts in the chapter and your own experiences before continuing to next one.

First thing that struck me about this book is Authors gives examples from diverse fields  like music, civil engineer and construction apart from Software. These examples illustrate the author's knowledge about these other fields. I am greatly impressed by the case study about design of auhor's house and kind of design notes that he kept through out the process. I wish I was this systematic.

The book is divided in 6 sections
  1. Models of Designing
    So models of design talks about the 'engineer's view of design, about Water fall model and why it doesn't work and what are possibly better design models.
    In my 15 years of experience, I have never seen 'water fall model' work in practice. However, all the software development processes defined in software companies implicitly assume 'water fall model'. End result is developers hate 'these processes' and organization cannot get expected benefits of process implementation. Someone (I think Albert Einstein) once said that 'Insanity is doing the same thing, over and over again, but expecting different results'. By this definition, many common software development 'processes' in software companies are 'insane'.
  2. Collaboration and Tele collaboration
    Now a days any significant sized project is about 'collaborating with other' for design, coding/construction, testing etc. In the world of 'outsourced software development', tele-collaboration is increasing critical. As expected, Fred provides some insights on what works and what doesn't.
  3. Design Perspectives
    Talks about various 'views' about the design, user models, aesthetics, constraints, examplars (or design patterns), where things go wrong.
  4. Dream system for design houses.Authors thoughts and ideas for a 'dream system' to design houses and why of those ideas
  5. Great Designers.
    Talks a common argument that software development should be like a 'factory'. Good Product development process will produce 'good designs'. I have heard this argument many times from Software Engineering Process Groups (SEPG) in the companies, CMM and ISO style processes etc.While, Fred argues that 'Great Designs come from Great Designers'. He also goes into the depth of why we need Product processes and how to make product processes that encourage and facilitate great design. I think this is a chapters that every manager should read.
  6. Case studies.
It is not possible to reviews the ideas in this book in one article. So I am hoping to write multiple articles as I assimilate ideas from this book.

Monday, October 04, 2010

Net banking & Security and Customer Service

Few days back, I was trying to make payment using Netbanking through HDFC bank. As part of internet payment process, some security questions were asked. I wrote the wrong answer. My account got locked. Now this is a good security features. However what happened next is classic example of 'how not to treat your customer'.
  1. In many cases, when account is locked because of wrong password entry, it is automatically unlocked after 24 hours. So I tried to make a payment after 24 hours. Again it failed. At this point a general expectation is HDFC will send me an email or SMS with intimation that my account locked and how to unlock it. I did not get any intimation. 
  2. So finally I sent an email HDFC customer support. I got a reply 2 days later. The reply was to contact HDFC phone banking to unlock it.
  3. So I called phone banking, the phone backing support executive took the call and gave me a 'support request number' and told me it will get unlocked in TWO days. Why it takes 2 days to unlock the account???. I thought its just a matter of minutes.
  4. So I said I want to talk with the supervisor. A lady came on phone and told me 'HDFC don't have any process to inform customer that their net backing secure payment access is locked'. I understand HDFC want to add more security. It is good for bank and its good me as customer. But then 'not having a process to inform customer' is a bad way to treat the customer. HDFC already has alerts for Netbanking and third party transfer transactions. So they already have all the necessary information to inform me about account getting locked.
  5. I have to make alternative arrangement to make the payments. I could make those arrangements a lot earlier if I get account locking intimation on time. 
It was frustrating experience. I also have few other interesting experiences/tidbits about HDFC.
  1. HDFC bank credit card department kept calling me even after I got a credit card from HDFC and they kept offering me a credit card for almost another 2 weeks.
  2. I have already registered my number to 'National Do Not Call registry' long time back. But HDFC doesn't seem to respect National Do not call registry. So I registered my number to HDFCs 'do not call' registry. There is an interesting line on this page "Please give us 45 days from the day we receive this request to action the same.".  I started getting calls about credit card almost within few days of opening account with HDFC. So that happened in less than a week but to act upon a 'Do not Call' registration takes 45 days.That's interesting.
  3. After I opened account with HDFC I wanted to register for bill payment. So I checked 'Payment Services' page. On this page there is 'Bill Pay', 'Visa Bill Pay', 'Register & Pay', 'Pay Now', 'Insta pay' etc etc. I am still trying to figure out which one makes sense for me. Give it a try. Its an interesting exercise.
  4. Every time I visit the bank there are new notices on the branch notice board usually of the form 'from so and so date Rs xxx charge for yyy service'. HDFC's profit after tax in FY 09 was Rs 2,282.54 cores. Interesting bank still wants to charge even photo attestation, signature attestation. Even cash transactions are charged (5 cash transactions at branch will be allowed free per month. Every additional transaction will be charged @ Rs 100 per transaction effective 1st July 2010)
HDFC is not the only one this kind of mistakes. I have seen some really bad Net banking user interfaces, weird policies etc. in other banks as well (Nationalized, private and cooperative banks). After Sangli bank was merged with ICICI bank, my cousin closed our family hardware shop's current accounts with them because of bad /irritating policies of ICICI.
I have also experienced some excellent customer service from HDFC and ICICI. I don't want to change the banks frequently. I want to be a happy customer of HDFC bank. If HDFC bank learns to take care of such small but really irritating to customer issues, I will be a happy customer of HDFC. If it doesn't I may remain a customer but not the happy/loyal one or I may not remain a customer at all.

Lets see what happens.