Friday, January 31, 2014

Ubuntu basics - What is PPA?

When you are trying to find a software to install on your Ubuntu machine and someone mentions about installing it from a particular PPA, what do they mean?
Ubutu has a release cycle of 6 months. What does this mean for the users? This essentially means that there will be no updates in this period of 6 months. To give you an example, if google comes up with an update for chrome then you will have to wait till the next release to receive this update. This is not at all acceptable and this is where PPA comes in.
PPA means Personal Package Archive. PPA is a repository to which software can be uploaded. This can be done by anyone - company / individual. It is from this PPA you can install your software - either via UI.

Or using the command line

This is a part of the series - learning from stackexchange

Monday, January 27, 2014

Chrome add a little sound icon to the tab in which a video is being played


Chrome adds a sound icon to  the browser tab window in which a video (basically sound) is being played.


If the sound is muted, the icon gradually disappears.


Monday, December 30, 2013

Which Java web framework to use?

While I was doing my investigation to find a suitable Java web framework to use in a personal project, I came across this study done by RebelLabs. They have two comprehensive posts on 8 different frameworks -

  • Spring MVC
  • Grails
  • Vaadin
  • GWT
  • Wicket
  • Play 
  • Struts
  • JSF

They have rated these frameworks on different parameters and also on the type of application to build. Search no further, read these two articles:

The Curious Coder’s Java Web Frameworks Comparison: Spring MVC, Grails, Vaadin, GWT, Wicket, Play, Struts and JSF

The 2014 Decision Maker’s Guide to Java Web Frameworks

I finally went ahead with using Play! framework. In some other post, I will talk more about Play. But, for now I did a fun thing. I went to and tried to find out how other frameworks fight it out with Play in the Google battle. Following results definitely show that Play framework is picking up faster than any other Java web framework.


Sunday, December 15, 2013

Web Fundamentals

This is an excellent video for all those developers who have never tried their hand at web based applications. Building applications for web is a paradigm shift from building traditional thick clients or server based applications.

The video explains many of the basic technology abstractions over Web such as HTTP, Cookies, Sessions etc.

Monday, September 24, 2012

What's in a name? A rose by any other name would smell as sweet. Everything’s in a name when it comes to code and some other things.

I have two incidents to tell you which happened last week. The first one was when a  colleague complained about the quality of existing code he was looking at, particularly variable names. One such variable he was trying to decipher when he came to me was named ‘cache_2008_temp’. And I told him that when variables are badly named, I wouldn’t make any assumption about them and instead look at it as if the variables were named as ‘a’, ‘b’ and like. I mean what was the developer thinking when he named a variable like that – cache in 2008 which is temp. That is utter nonsense.

2012-09-23 12.30.44 Another incident I want to share took place when I wanted to cook some quick pasta for lunch and I asked my wife if we have any readymade pasta sauce (sometimes I can get too lazy to cook fresh pasta sauce). She said that it was in the refrigerator. And I found the bottle. Yeah, that pasta sauce bottle is in the picture.
Now, when I am ready to pour the sauce in the pan with cooked fusilli, I open the lid and dip my spoon to get some sauce. And as I am about to mix it, I realize that the texture of the sauce and the smell is different. I just taste it a little bit and guess what? It isn’t pasta sauce. It is in fact some hot red chili mix. My wife had used a pasta sauce bottle, washed it, but had ignored the label on the bottle. Imagine, what would have had happened if I had used the mix as it is.
So after making the point about importance of names, let me quickly summarize on why is it important to choose good name? Things I have learned over years from my experience and from Uncle Bob’s book Clean Code.

Names shouldn’t spread disinformation.
As in above pasta example or the variable ‘cache_2008_temp’,  there is a certain amount of disinformation being spread. If the variable was named ‘a’ and the bottle didn’t have any label, they wouldn’t tempt anyone to make assumptions.  Variables which spread disinformation are dangerous because there is a chance that their intention might be deciphered wrongly.
Good names make code self explanatory.
I have picked the following code snippet from a previous post of mine :
   1: //code snippet 1
   2: static void Main(string[] args)
   3: {
   4:     var now = DateTime.Now;
   5:     var nyTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
   6:     var nyNow = TimeZoneInfo.ConvertTime(now, nyTimeZoneInfo);
   7:     var nyTodays17Hours = nyNow.Date.AddHours(17);
   8:     var nyNext17Hours = nyNow < nyTodays17Hours ? nyTodays17Hours : nyTodays17Hours.AddDays(1);
   9:     var localTimeWhenNyNext17HoursOccurs = TimeZoneInfo.ConvertTime(nyNext17Hours, nyTimeZoneInfo, TimeZoneInfo.Local);
  11:     Console.WriteLine("localTimeWhenNyNext17HoursOccurs is " + localTimeWhenNyNext17HoursOccurs);
  13:     Console.ReadKey();
  14: }

The code calculates local time when 17 hours will occur in NY. The variable names makes the code quite self explanatory. Variable names like
localTimeWhenNyNext17HoursOccurs  allow you to breeze through the code when you read it. Now let me rewrite the code using bad variable names.


   1: //code snippet 2
   2: static void Main(string[] args)
   3: {
   4:     var now = DateTime.Now;
   5:     var nyTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
   6:     var ny1 = TimeZoneInfo.ConvertTime(now, nyTimeZoneInfo);
   7:     var ny2 = ny1.Date.AddHours(17);
   8:     var ny3 = ny1 < ny2 ? ny2 : ny2.AddDays(1);
   9:     var ny4 = TimeZoneInfo.ConvertTime(ny3, nyTimeZoneInfo, TimeZoneInfo.Local);
  11:     Console.WriteLine("localTimeWhenNyNext17HoursOccurs is " + ny4);
  13:     Console.ReadKey();
  14: }

Now the code in snippet 2 does exactly the same as snippet 1, but the difference is in variable names which makes the code extremely difficult to understand. With code you tell a story, your variables, methods, properties, classes play characters in that story and if they are badly named, they make your story look bad.


If your variable requires a comment to understand its intent then the variable has a bad name.
Variable names shouldn’t require a comment to understand them.

   1: //code snippet 3
   2: List<int> cache_2008_temp //history of records in 2008;

In code snippet 3, we see that the variable ‘cache_2008_temp’ has been commented to tell us that it is used to store history or records in 2008. But, what happens to the code where it is used? Anyone who reads the code using that variable is bound to get confused. It destroys the readability of the code because that name makes no sense – it spoils the code story. So, after sometime a developer who maintains that piece of code thinks that no longer is the 2008 history necessary then she might just rename the variable as ‘cache_2009_temp’ and  use it to store history of 2009 records.  But she might forget to update the comment. As code evolves comments get outdated and hence reliability on comments to understand the code can be dangerous.

Names should be intention revealing.
So instead of naming the variable in snippet 3 ‘cache_2008_temp’ we could have named it ‘historicRecordsOf2008’  or just ‘recordsIn2008’. Names should convey the intent correctly. Such names bring a clarity to the code. Code cannot achieve simplicity if there isn’t any implicit understanding of names.

If you can’t pronounce a name, then the variable has a bad name.
A variable name like ‘cache_2008_temp’ is difficult to pronounce and use in discussions. Instead ‘historicRecordsOf2008’ can be easily be used while discussing the code. Use names which you can pronounce and hence can easily use while discussing the code.

To have good names you need to think, think hard. And that takes time. But as Uncle Bob says choosing good names is hard and takes time but saves more time than it takes in future.

Tuesday, September 11, 2012

Why do we need events when we have delegates?

Ok I understand that the answer to this question is pretty simple. But, I have seen many people faltering in interviews when asked this question.

We have a class with a public delegate and a property which exposes an instance of the delegate.

Code Snippet
  1. public class Temp
  2. {
  3.     public delegate void TestEventDelegate(string input);
  5.     public TestEventDelegate TestEvent { get; set; }
  7.     private void RaiseTestEvent(string arg)
  8.     {
  9.         TestEvent(arg);
  10.     }
  11. }

Now TestEvent is a multicast delegate. We can create an instance of the class Temp and add our method to TestEvent

Code Snippet
  1. var t = new Temp();
  2. t.TestEvent += input => Console.WriteLine("{0} received", input);

Similarly some other class might create another instance of Temp and add its own method to TestEvent

Code Snippet
  1. var p = new Temp();
  2. p.TestEvent += input => Console.WriteLine("Hell0 {0}", input);

And when Temp calls RaiseTestEvent method, all methods added to the multicast delegate TestEvent will be called. Now think about this for a moment. Doesn’t an event do exactly the same thing? Subscribers add their event handlers which get called when the event is raised. So, why do we need events and their fancy syntax? For that you should know that events can be raised only by the class which defines it, unlike the public delegate which can be called from outside the class by using the instance of the class in which it is defined. This is the main functionality of an event, where the class that defines it controls when an event needs to be raised.