Java Generics - half-baked!

July 3, 2008

I hate these situations in coding where common sense fails you. You are supposed to know anything and everything of the language.

Suppose we have an interface Bounceable, and Baloon and GolfBall are two classes that implement the bounce() method of the interface.

public interface Bounceable {
     void bounce();
}
public class Baloon  implements Bounceable {
     public void bounce() {
          System.out.println("I am a Baloon");
     }
}
public class GolfBall implements Bounceable {
     public void bounce() {
          System.out.println("I am a GolfBall");
     }
}

We have a class Person, which takes a list of Bounceable objects and plays them.

public class Person {
     public void playAll(List<bounceable> bList) {
          for(Bounceable b : bList) {
               System.out.println("Bounceable object: " + b);
          }
     }
}

Now, from a main method, lets try to test something. First, lets create a list of Bounceable objects and pass it to the playAll() method of the Person object.

List<bounceable> bList = new ArrayList<bounceable>();
bList.add(new Baloon());
bList.add(new SoccerBall());
bList.add(new GolfBall());
Person person = new Person();
person.playAll(bList);

This works great. The playAll() method is expecting

List

and we just passed bList to it. Perfect!

Since Bounceable is an interface, now we would also hope that it would accept List of any subclasses of Bounceable. At least, thats what I have known of polymorphism in Java. And it works that way with Arrays. However, thats not the case with Generics.

Baloon implements Bounceable. So lets try to create a

List

and pass it to the playAll() method. But this simply doesn’t work.

List<baloon> baloonList = new ArrayList<baloon>();
baloonList.add(new Baloon());
baloonList.add(new Baloon());
baloonList.add(new Baloon());
person.playAll(baloonList);

This is because once we are in the

playAll(List bList)

method, the list is treated as

List

and we should be able to add any objects of type Bounceable.

// See, we just corrupted the baloon list with a golf ball.
baloonList.add(new GolfBall()); 

Put simply, if a method foo() takes an argument

List

, you cannot pass a

List

, because the compiler is scared that you will take the

List

and put stuffs into it that aren’t of type Y, thus corrupting the entire list type.

But there is a way that you are allowed to pass

List

it to the method, but you cannot modify the list. For that we have to change our method signature to:

playAll(List bList)

? extends X means any unspecified type of X. So we can pass

List

to the method with signature

List

and retrieve our Baloon objects back in the playAll() method, because the compiler knows that we aren’t allowed to add anything to the Baloon List.

Sigh!!!!! Interesting, isn’t it? Thats why Java is starting to suck. While Generics is great feature for type safety, the way this feature is inconsistent with other features within java makes it very difficult for beginners and students to learn and feel confident about the language. I have seen experienced programmers fall over such traps.

Java is turning too big and too confusing of a language. And with Java 1.7 (with big features including Closures) knocking the door, Java might be closing the door for a newbie and turning him away to another JVM language or Python or Ruby.

Life in front of Boxes

June 14, 2008

Six o’clock alarm!

Wake up, cram yourself into a corner of the eight-by-eight cubicle, sit in front of boxes - white boxes, black boxes, 8 hours a day, 5 days a week, 52 weeks a year with 2 weeks of sick and vacation time (accrued at 4.62 days for fortnight) that better be approved weeks in advance. Flight plans, deadlines, jiras, why and wherefore of the meetings, demos, deployments, and cheese burgers!

Horns and traffic.

Come back home! Boxes again. Web2.0 and startups. Bloggers and books. Bankruptcy and billionaires. Emacs and Erlang. Eddie and the wolf. This time for yourself. As if the 8 hours weren’t enough. As if there is something addicting about the boxes. The same mailboxes refreshed thousand times a day. Oops, a curly brace or a semi-colon missed again! As if the world is going to break if I don’t sit in front of boxes.

Dreams for sale.

Six o’clock alarm again!

Coding Slave

June 13, 2008

Everyone writes about codes. There are not much books on “Coders”. Ever since I heard about the book Coding Slave from a number of bloggers, I always wanted to read this book. But I was kinda scared that this one might be too venomous and negative, making everybody other than coders look like a villain. I finally grabbed the book and completed it after two nights of reading.

To summarize in short, it’s a story about an enterprise software development team within a typical corporate world of today. This book is about firing and hiring, off-shoring and consulting, project plans and deadlines, BA’s and QA’s all under managers trying to climb up the corporate ladders. If you have ever worked as a developer in a large financial institution working on huge ERP systems that never get completed, this book will resonate with you better.

If I consider this book as a piece of literature like any other books, I have to admit that this book is the worst I have ever read in terms of its presentation, quality of expression and character buildup. But I am assuming its from someone who knows a dirty side of the present IT industry too well, and he is not necessarily a good writer. There is an offensive amount of ’sex’ involved, strip clubs and cam girls, death and suicide. I am not exactly sure why the writer decided to stuff so much of unrealistic sex into the story, but I guess that writer is really clever in knowing who his reader is going to be; a slightly pervert and a geek that is.

IT industry esp. the IT departments within companies are usually under wrong hands. Its always ‘get the code out fast”, and make sure “it sort of works”. Its just about meeting deadline; its just about media coverage, shares and profits. Nobody really cares about customers of the software. A project manager never gets fired for a unrealistic deadline or a bad project plan or hiring a wrong person in a team. But a developer who has produced years of great code in the same team gets fired one day coz he couldn’t follow a bad project plan and meet the deadline. A BA seldom gets fired for misinterpreting a customer’s voice in the project requirements. But if a project fails to meet a deadline, everyone looks eyes crossed at the developer. BA’s come with last minute changes, saying that it’s a minor functionality change. But nobody really understands that what they think is a minor change needs a major code refactoring from the developer. Result? Another late night or a spoilt weekend! After all having worked so sincerely all the way through, you don’t want your manager to give you that shitty “Its all your fault” look at the 9:00 AM project status meeting every morning.

All in all, we are coding slaves. We work under some kind of dictatorship to build the software. We get victimized for everything that fails (be it our fault or not), and someone else gets the kudos and promotion for the success. This is the reality of majority of software development teams.

The author thinks we - coders ourselves are responsible for the mess. We let the people who know nothing about software run the show. No surprise that he doesn’t want you to do the unit tests, documentation or re-factoring, coz it all makes our lives easier and not his. We aren’t enslaved by the corporation or the system, but its our own attitude thats enslaving us. We are the only people who knows the software well, and who are passionate about it like a mother to a child. We care for its well-being and health. We care for its usability and quality, and have the right skills to nurture it. The author finally envisions a philosophy where the developers control and manage all this. We should be the masters of the software, not someone’s coding slaves!!

I think the open source community of today is kind of close to what the author has dreamt. Open source projects are driven by coders, driven by passion, driven by a reason, skill, knowledge and thats why they have been so successful. There isn’t a non technical boss (who knows nothing about software) telling the coders how they should build the software, what it should be like, and when it should be checked into the repository.

The industry is not short of places where good developers are forced to write dull and boring codes.

Erased from History!

May 28, 2008

28 May 2008, Nepal finally became The Republic of Nepal, bringing an end to the centuries old (240 years to be exact) Kingdom of Nepal.

Monarchy in Nepal is an institution that is deeply rooted into the society, and many people still consider the King to be an incarnation of Lord Bishnu, or have some sort of respect for religious, national or cultural reasons. A king is after all much more than a national leader. Two years back when I was leaving Nepal, I wouldn’t have imagined in the wildest of my dreams that a day like this was right around the corner.

Even though Nepal has had clashes with the monarchy a few times in the last 60 years, this major change is primarily an achievement for the Maoist party who had started an armed Communist war in Nepal since Feb 1996 against Monarchy, the then governments, social injustice and poverty. Over 13000 people have been killed since, during the war and Nepal no more remains the Zone of peace (As of 1991, Nepal had been endorsed as the Zone of peace by more than 110 nations). An election was held last month to elect members of the Constitutional Assembly (which Maoists had won by large), and its first meeting today abolished Monarchy from Nepal. 

The process has been a little too unfair on the Royal Dynasty, esp. the way it is abolished without direct voting by people. Hence I take it as a good change done in a  haste in a very undemocratic way, and I won’t be surprised if this injustice on the side of the King and his believers will reveal itself in some violent form few years down the lane, and continue to destabilize Nepal every now and then. 

 In my analysis, there are several reasons how this happened to the King so soon:

1. King Gyanendra had succeeded his brother King Birendra on June 4 2001, after the King Birendra and his whole family (including his Queen, Prince, and all his entire family) was killed in a Palace Massacre. Deep inside, majority of people still view this King as the mastermind behind the whole episode and never really accepted him as the King. He never did an impartial and independent investigation of the massacre.

2. As soon as he was crowned the King, he started provoking the feeling of people by showing his belief in himself, and tyranny through interviews. He was the first King of Nepal to give public interviews to media.

3. When he completely took over the  government power sacking the elected government in Feb 2005 (he was kinda growing popular at that time), he did so in a very undemocratic way, cutting off public access to phone, internet, cable television and all forms of media for days.

4. He overestimated his popularity, and remained too inflexible and egoist to come in terms with the major political parties.

 5. He stood against Peace talks with the Maoists, and showed no urgency in solving the problem in a peaceful manner.

6. He believed nobody, except the same old royal leaders like Tulasi Giri, Sacchit Shamsher, Kamal Thapa, etc who had already been defamed in front of people for their belief in proactive monarchy. When they convinced the King that the Maoist and Political parties had no public support behind them, the King believed them and continued to go their way till the extreme.

What could the King have done?

1. Right after he had taken complete control over the government in Feb 2005, he should have done peace talks with the Maoists, and let them rule the country in their way, but under some form of  Monarchy. This would have been both beneficial for the country and for himself. And looking at the way the Maoists are hungry for power, there is no way they would have rejected the offer then. On another note, he could have given the power back to the political parties, but that would never have solved the Maoist war in Nepal.

All in all, despite being anti-royalist, I am not too sure if this is a good thing for the country at this point esp. with the way people haven’t actually voted for the abolishment of King, the maoists changing colors for power, the ugly politicial parties still there with the same ugly faces,  and the racial violence that has been injected in Terai and other parts.

The civil war which the political governments couldn’t solve in 8 years, the King has finally solved it with his 2 years of stupidity, but at the cost of his history and future. Must be a very hard night for the King today! He chewed up and swallowed in 5 years what his grandfathers had weaved in over 200 years in history.

However its good that children from now on can grow-up wanting to be the President of Nepal. 

Erlang on Mac OSX

April 28, 2008

On a fresh install of Leopard, the following is what I did in order to set up Erlang working on my Mackbook Pro:

1. Download Xcode by going to Apple ADC. Xcode is the Apple’s developer tool set, and its available for free. At the time of my install, the latest is Xcode 3.0 and its little over 1 GB. Double-click the downloaded file (named xcode_3.0.dmg), and follow the graphic package installer. This will install Xcode under /Developer by default. This step is required for Erlang because it will install GCC compiler required to build Erlang from source.

2. Get the latest Erlang source, which will be a file named otp_src_R12B-2.tar. Unarchive the file and follow the instructions:

$ cd otp_src_R12B-2
$. /configure
$ make
$ sudo make install

Now bring up a terminal and issue the command:

$ erl

Erlang (BEAM) emulator version 5.6.2  [smp:2] [async-threads:0] [kernel-poll:false]
Eshell V5.6.2  (abort with ^G)
1>2+3.
5
2>;

I am all set to go…