Sohan's Blog

Things I'm Learning

2011: Yet Another New Year

Dear Readers:
Happy 2011. 2010 was a great year. Why?

  • Defended my MSc thesis after a moderate 16 months of grad school life.
  • Published 3 papers in 3 international conferences.
  • Visited 4 new countries - Netherlands, Sweden, Finland and Norway.
  • Visited a few interesting cities - Vancouver, Victoria, Edmonton.
  • 100+ posts on DrinkRails.com.
  • 25,000+ visits to my two blogs from all over the world.
  • Developed a fresh new app, Plexina Central, for Wairever Inc.
  • Lived a healthy life.
  • Bought my first (98 Corolla) and second (2000 CR-V) cars (also sold my first car)!
To live up to this standard, 2011 is surely challenged by 2010. Let’s see how 2011 rolls: To give you a heads up, I am joining ThoughtWorks as a Consultant on January 4th!
However, 2010 was the only year in my life when I didn’t see my parents and siblings. I hope, 2011 does a better job in this regard. It is getting essential now.
This is how I look at the close of 2010
And I want to look leaner at the close of 2011 or even before that :(

DRY - Total 161 Duplicate ArgumentNullException Calls in ASP.Net MVC Source Code

There are 161 occurrences of the following code pattern inside the ASP.Net MVC source code: See details of which class and which line at https://gist.github.com/739523
method(type argument)
{
  if(argument == null){
    throw new ArgumentNullException(“argument”)
  }



}
So, in total this pattern introduces 312 lines of duplicate code without whitespaces and 644 lines of duplicate code including whitespaces. And the pattern is simple, just check a condition and throw exception if its true. Not to mention, there are numerous other duplications where string.IsNullOrEmpty is used to check for string arguments.
This observation is interesting to me. Since, I think it adds a lot of noise to the code. In fact, if you randomly look at any of the methods - you have a good chance of seeing the first few lines doing this exactly similar thing. And this adds noise to me. Not to mention that, the developer needs to write the code, write tests for this code and if one needs to use this method, somehow should know this in advance that passing a null will result in an exception. But of course, this is not explicit from the method signature unless the developer also takes the pain to put a documentation styled comment and mention about this ArgumentNullException.
To remove this clutter, I think C# can introduce a few language keywords such as the follows:
public void Authenticate(required User user)
public void AddNewUser(nonblank string userName)
A compiler can very easily compile the code and put the exception throwing logic based on these keywords. Call it a syntactic sugar or a language keyword, this is the kind of translation that seems to be perfect for the compilers. And I can foresee it being used in almost all methods we write, since if the methods require an argument they do it for a purpose and most of the time null is not expected anyway!
I don’t know if this will reach the people responsible for developing C#, but if it does, I think the community will really love this new language feature.
I know about workarounds using AOP or other hyped-but-seldom-used tricks to solve this problem. But these tricks often make life harder by introducing a learning curve and then punishing the learning by slowing down the application. So, its best to put in as a core language feature. What do you think?

Book Review: Jose Valim’s Crafting Rails Applications

Have you already read this?

The following excerpt  by Jose Valim at the end of the book nicely summarizes it:
Finally, you understand Rails better. You can explore other areas of the source code, study other Action Controller and Active Models modules, check other generators implementations or read the source of Railties, Engines and Applications with detail! - Jose Valim
For the impatient readers, this is the best in-depth Ruby on Rails related book I have ever read and you should read this (only 172 pages). Buy the book here. But wait, you could get 40% discount and get the book for $13. I will tell you how, continue reading!
This book literally opens up the Ruby on Rails framework for the readers. So, now I am crystal clear about Metal, Rack, ActiveModel, Rendering, Templating, Migrations, Gems, Engines and so many core concepts. If you are like me, you have cloned the Ruby on Rails code and took a peek into the code to understand, patch some code or just to learn some hidden treasures of Ruby, but had tough time getting hold of the big picture. Well, this book will show you some highways and also some other useful alleys - you can find the rest by yourself!
This book embraces Test Driven Design, so all code examples, and hell yes there’s a lot of them, are test driven. So, its unit test, functional test and integration test that drives the show. The topics cover customization of key Ruby on Rails components such as: models, controllers, views, mailers, generators and rack integrations. But the examples are really good as it shows:

  • Responding to pdf requests so that the server responds with on-the-fly PDF responses
  • Using Markdown templates for producing HTML and Text multipart emails from a single source template
  • Rendering views that are stored in a database to work as a simple CMS
  • Publishing and subscribing to Rails internal event such as SQL queries
  • Creating a simple rack application
  • Creating a Rails Engine
  • Creating custom responders so that you can DRY up your controllers that use exactly same lines for respond_to do |format|…
  • I18n translations using Redis key-value store and caching
  • Combining Sinatra with Rails in a single Rails app
To ease the process of trying out the DIY code, Jose Valim created an excellent gem called enginex. This gem gives you an incubator to try out the example code while building your own gem and using it from an embedded dummy application, writing tests and even browsing the app. The whole experience of trying out the code is awesome. Before this, I have never read a book that shipped with a gem just so that the readers could easily use the source code. Awesome idea and even better execution.
While explaining the inner workings of Ruby on Rails framework the book also discussed about a few useful gems:
  1. devise - authentication gem.
  2. enginex - produces incubator for gem development and makes it a breeze.
  3. capybara - drives integrations tests and also works with browsers through selenium etc.
  4. rdiscount - Discount is an implementation of John Gruber’s Markdown markup language in C
  5. responders - DRY up your Ruby on Rails application with a number of handy responders.
  6. redis - an easy to use Key Value store.
  7. prawn - Ruby PDF library.
This book also shows a number of cool tricks. Its overall a fun read. I would say, the final version will be  more eye-soothing, but don’t wait for that. Grab your ebook now and you will get the updates as they come!
I would like the book more if Jose Valim could reorganize some of the content. For an example, the text about Generators appear in different places. I think it would be easier to follow if it was in a separate chapter. The same applies for Engine.
I have another observation: the book is written for pro developers with substantial Ruby/Rails application. But I think the topics discussed here could also be equally useful to beginners. The later can be achieved by going a bit slow about some of the topics. Jose Valim, can you add little explanations at places for beginners?
The bottom line is, buy this book and read. To receive 40% discount, sign up at http://pragprog.com and refer to a friend and ask her to complete signup. You both get 40% discount. To claim, go to your account and you will find this as soon as your friend completes the signup.
Thank you!
This review is not sponsored and contains my opinion only.

Comments

Bluesman Alex
Hi!

I have just purchased this book, and I have only glanced at the contents page. I'm glad a book like this finally appeared. I think there's a good need for it on the market.

Also, I'd like to see a section covering rails class loading. I've been lacking info on that several times, when I had weird bugs appearing in development environment and I had a hard time surfing the source code…

Thanks for the book, Jose! Nice work! :)
José Valim
Hey Sohan,

Thanks for the detailed and great review about my book! I will discuss with my editor and see if and how we can organize some content, as you suggested.

Also, do you mind pointing out a few areas where I could add more explanations, to be more friendly to newcomers?

We can continue this discussion in the PragProg's forum.

Thanks a bunch!

Ruby - Some Mysterious Language Features

Here I will share some of the interesting ruby features that you will notice when looking into mature ruby code by advanced level coders.

Comments

Inventory Management Software
Thanks for sharing your post and it was superb .I would like to hear more from you in future too.

Upgrading to Ruby on Rails 3 - Beware!

Ryan Bates had a series of posts (1, 2 and 3) on upgrading your Rails 2.3.x apps to Rails 3.x and sure they are useful. But if you are really doing that, beware of the following changes that you will need to do. It will take a lot of time for sure if you you have a moderate sized app.

Upgrading your models:

  1. Get rid of all validates_xxx_of with appropriate validates
  2. Get rid of all def validate … end methods with custom validator
  3. Find out all occurrences of :conditions, :limit, :order and use new active record methods instead
  4. Replace all named_scope with scope
  5. Make sure your acts_as_xxxx plugins are all updated to use Ruby on Rails 3. I had troubles with Authlogic as it shows Based.named_scope is DEPRICATED. Still looking for a solution.
  6. Ruby 1.9.2 doesn’t work with soap42 wsdl driver. Haven’t found a solution yet as it keeps reporting an error regarding “XML processor module” missing.
Upgrading your controllers:
  1. Find out all occurrences of find method and replace with where.
  2. Find out all calls to Mailer that looks like deliver_xxx and make it xxx.deliver
Upgrading your views:
  1. All erb blocks must start with a has to be changed to  . Do the same for all such erb blocks with a do.
  2. All link_to_remote or remote_form_for or other remote form helpers need to be changed to their non-remote counterparts with a param :remote => true.
Upgrading your mailers:
  1. body method is gone, instead of the hashed params to this method, just define instance variables.
  2. All xxx.text.html.erb now becomes xxx.html.erb in the mailer views.
Upgrading the config files:
  1. The best way is to crate a new rails app and then copy the config folder into yours.
  2. Look at the initializers and clean up any required initializers.
  3. Make sure you have autoload_paths setup to include the lib folder. It is not included by default, so your code from the lib folder won’t be accessible.
  4. Look at deprecation warning and you will see lots of config.action_controller.* need to changed to config.*
Plugins:
  1. All plugin/lib/rails/tasks/*.rake needs to be copied in to plugin/lib/tasks/*.rake
  2. Make sure your plugins don’t use named_scope, if you find any, replace with scope.
  3. Whatever applies to your models should also be applied to your plugins that act on models.
Testing:
  1. Check you have updated shoulda, rspec or whatever lib you use.
  2. Update test codes according to your new lib.
Upgrading IDE to use RVM:
  1. Your IDE is not smart enough to use RVM. However use this to get TextMate ready.

4 Design Principles

Simplicity favors regularity.
Smaller is faster.
Good design demands compromise.
Make the common case fast.

The four principles of MIPS design that applies to most design jobs we do, even with our personal lives. Agree?

OO Design Dilemma: Auditing Changes Across Hierarchical Objects

Here is a sample UML class diagram of the situation that posed me the OO design dilemma a few days ago.
Let me explain with an example,
The Electronic Items catalog has many Televisions. The Sony Televisions come with different specifications, such as refresh rate of 240 Hz, 120 Hz and 60 Hz. However, the 240 Hz ones also comes in different colors - Black and Grey.
Now, a store manager needs to see the recent changes across all catalogs, such as electronic items, musical instruments and so on. So, if someone added a new Sony 240 Hz color of Dark Black or removed the Grey one, the store manager needs to see this. However, a department manager may need to see changes at all levels, for example whenever one of her Catalog, Product, Specification or its Property is changed. So, if she needs to see all changes under Sony Television, you know, she needs to see changes in the name and value of underlying specifications and their properties as well as changes at the higher level, say, the price.

Now, the above design looks good from OO perspective. However, since we need to persist the data into a relational database, we will end up with one table per class and foreign keys to interlink. If you are doing Ruby on Rails, you will possibly use polymorphic association between Auditable and the subclasses. For an example, lets consider the following E-R design (not showing all relations):


Now, given this schema, if you need to find out the latest 50 Log Messages with Author and Associated objects for the Electronics Catalog, how would you write a simple query?

Look, here is an expected outcome of the query:
Sep 15, 2010

  1. Jane added new Color : Dark Black for Sony 240 Hz TV
  2. Shon changed the Sony TV Price from $700 to $600
  3. Jakie added a new Specification for Panasonic TV: Aspect Ratio -> 16:9
Sep 14, 2010



Now, you see, the date wise grouping can be done once the latest audit logs from all across the Catalog hierarchy is found. But how do you find it given a CatalogID to start with? The following query wont work:

SELECT TOP 50 * FROM Auditables WHERE AuditableTypeName=’Catalog’ and AuditableID=my_catalog_id ORDER BY Timestamp DESC

Because it will only produce the audit logs for the Catalog Object, whereas we are expecting to see all the recent audits for this Catalog and underlying products down to the Specification Property level.

However, one work around is to query for the Audit Logs for the Catalog. Next, find all the Products of this Catalog and query for the audits for all these products. However, if we are to select top 50 audits for the catalog and its whole hierarchy, how many should we select here? And the problem gets even worse,  when you have to repeat the above steps for Specifications and Specification Properties. When you have to take the latest 50 audit logs after you run a few queries, say 10, you have to take 50 audit logs for each  of them. Because, the latest 50 results can be from a single query :-(

Looks like the Auditable class and corresponding design doesn’t work well for this situation. So, what is a possible solution?

It’s hard. I don’t readily see a great solution. However, workarounds are there. For example, we can change the Audiable class to hold references to all levels of the Catalog object hierarchy. So, in that case the Auditables table will look like the following:

With such a schema, we need to ensure if the Audit Log corresponds to a SpecificationProperty, we put references to all its higher level objects. So, the query will be simple. With this assumption, the following query will be able to fetch all audit logs for Catalog and its descendants.

SELECT TOP 50 * FROM Auditables WHERE CatalogID=my_catalog_id

Similarly, the following query will produce the audit logs for a single project and its descendents:

SELECT TOP 50 * FROM Auditables WHERE ProductID=my_product_id

However, it has its downsides as well. The Auditables is no longer a general purpose Auditable. If we add a new type of Auditable, we cannot use it unless we alter its properties! High Coupling! Less Reuse. Also, a lot of if-else will be required to show the Audit logs, as it can belong to multiple parents!

How would you solve this design dilemma? Comments welcome!

Know Your Enemies Before They Kill You!

From darekb
You know what, this is a political world. So, enemies will seemingly look like your friends until the moment when.. well, its time for the kill! My dear readers, its time to know the enemies!

I have met with a few enemies off late. I will go one by one here.

If you are a software developer like me, you will often see this enemy, camouflaged variables, methods, classes and namespaces. For example, I have recently seen a Stack camouflaged as Visitor! I really mean this. I found a class called Visitor, so I was expecting a Visitor Design Pattern implementation or something similar, but what I got was a Stack under the hood with two methods Push and Pop! This enemies are very bad for your health, as they will keep you guessing all the time… you never know what they do from looking at their names!

Another enemy you will often see are the very skinny ones, to skinny to have any meat in them. I met some enemies like this as well. What happens when you do over engineering with interface explosion and a lot of one method classes? Is it really that difficult? Is it really a class? Is it really a package? I don’t think so! You can spoil a piece of code by introducing a class/package for a single method. This enemy often surfaces because of the fact that, the design pattern book only shows classes with one/two key methods in them… which is of course not intended. But this is life… you gotta balance between class explosion and God classes… really, or this enemy will kill you someday.

I have just touched two common enemies… but there is another enemy we all are aware of, CMD+c (OS X) or Ctrl + C (Win). Its such a pain to copy a code fragment and use it in a different class… this is exactly the form of reuse that kills everything. Make sure you don’t let this germ to grow, or it will outgrow you and leave you crying.

Have you reviewed your code by someone else today? If yes, keep up the good work. If not, beware of the enemies before they get you. Best of luck!

Comments

jackob
Hi dude,
i read your blog,This is a wonderful blog.I was able to get the
information that I had been looking for. Thanks once again.
Thank You
Ruby Software

The Greatest Show on Earth and What Else?

FIFA world cup 2010 is almost coming to an end. This is definitely the greatest show on earth and I have been all occupied with the matches…

However, the show started almost immediately after I was back from Europe. It was a nice tour, starting a day at Amsterdam. Then to Trondheim (Norway), Stockholm and the best part was the night long sea cruise to Helsinki. An experience to remember for the rest of my life.

The days were eventful to say the least. At XP 2010, I presented my paper. Here is the presentation for my caring readers :-)



But XP 2010 was also a great place to meet people from around the world who care about software, its crafts and of course impacts. In very short, I found people to be very interested and starting to explore the possibilities with Lean and Kanban. This was a little surprising, because until now it has almost always been Scrum and XP to have a covering meaning of Agile. But, the industry seems to be leaning towards Lean. This is the beauty of being agile, to be able to adapt with time!
However, Norway seemed to be an expensive place even compared to Calgary. But, don’t shy away from Norway for this reason, they have spectacular beauty in their landscape. Serene and soothing. While staying in Trondheim, I visited a cathedral from early 1000’s. It was amazing to see the cathedral still standing firmly after so many years… extreme engineering! The city was full of historical buildings… if you haven’t heard, the Harry Potter building is actually the main academic building at NTNU. (kidding! but the locals really call it by Harry Potter building)
This is me and my wife in front of the Harry Potter Building!

The city tour and canal cruise at Amsterdam was also a pleasant experience. It was nice to see the centuries old bridges and roads still serving the people so well. It is indeed a bicycle city, everyone in the city seemed to have a bicycle… believe me, they have multi-storied parking facilities only for bicycles! No wonder, why they are so healthy as a nation.

Next, Stockholm is a city full of life. You will see people dancing, having a drink and enjoying their times with friends everywhere. Especially for people like me, who barely see any crowd in Calgary, will find the place to be very exciting and inspiring.

Our Stockholm stay was rather short as we went to Helsinki on a sea cruise with Viking line. This was very eventful. Firstly because me and my wife lost each other when I was rushing into the ship as it was just about to start… then I stepped out of the ship, rushing back to the checkin counter and heard her crying like a baby… she was so upset and so scared! But we eventually managed to get into the ship may be 1 min before the doors were locked!

But this late entrance came with a surprise! We were given a window side cabin although we didn’t get one while booking… and the awesome journey began. It was around 5:30 in the evening and the view from the deck was so nice. And there were live music and dance uptill midnight, nice romantic trip altogether. When we landed at the Helsinki city, we roamed around the tourist hotspots and I slept on the grass. Really! I took the following photo before I fell asleep..

However, back to normal life and the greatest show on earth started! At office, I have kind of wrapped up the application that I was working on for the last 3 months and now heading towards another project. This time its gonna be Java after a looong time… At the lab, trying to wrap my thesis related implementations by the summer. Discovering lot of things as I am working with Lucene, Solr, Tika, Acts as Solr and these full text search engine related stuffs. Hope to push a post about these things when I get some time!

But, this is summer in Calgary. And I attended the Canada Day fireworks at the City hall. Here is a photo of the event for my reader:
This is pretty much it. Looking forward to the events at Calgary Stampedes. Hopefully this will also be a wonderful time pass. I will see if I can be back with some real “meat” in this blog shortly!