Monday, July 20, 2009

Java and C#


If anyone wants to become a good programmer, then I highly recommend them to learn Java.

Two features that I like most in Java were Exceptions and All functions are Virtual Functions. Incidentally, When I was learning Java, the only feature that I hated was Exceptions. I understood virtual functions only 6 years after learning OOPS (Object oriented Programming and Systems). But, after understanding, I liked it very much.

Microsoft says, making all functions as virtual functions is limiting the developer's freedom. So, they have come up with all possible functions like new, override and virtual. They say, because, they have more no.of features, it gives more freedom. But, in these things, I would say, it gives more options to do suicide.

If we have many classes with may levels of inheritance, and if we use more than one type of function, then the code becomes highly unmaintainable. Giving more options in this case is nothing but giving more options for suicide.

In Java, you never need to worry about Time Zones. Whenever you get a Date, timezone is in built in the date, and we don't need to bother about it. Whether you are calling new Date() in IST or GMT or PST, it does not matter. It is the same date, and the only difference is it is displayed in different formats. But, underlying object would be same. This is the behavior in Java atleast since 2001. I believe it is the same since the beginning of Java, but, I have not worked with Java before 2001.

A free software (which became open sourced later) that was released in 2001 has a good feature, and I expected that the software developed by the biggest software company in the world released in 2008 will have that feature. With that assumption in 2009, in a meeting room, I almost argued with everyone in the team about that. But, later I realized that, I was the fool in that meeting, and my expectations were too high on the biggest software company in the world.

Few days back, I wrote a C# program and asked one of my colleague to review it. The program is something like below.



string q = "some string around 300 bytes";
foreach(object o in someKindOflist.Items)
{
// Do something with o;
}

I was asked to modify it like below.

if(someKindOflist.Items > 0)
{
string q = "some string around 300 bytes";
foreach(object o in someKindOflist.Items)
{
// Do something with o;
}
}


The reason was the garbage collection in .NET is bad, and creating one string is very expensive. So, we should try to avoid these things as much as possible.

Even in 2003, when I was using Java, I did not bother about these silly things. But, in 2009, in C#, I had to write crappy code, just because underlying framework is bad.

In that code, it has very trivial operations and one very simple query. The method will not take any significant time. Any reasonable good Garbage Collection Algorithm should clear it without any problem. But, if .NET does not do it, I don't know what to say about it.

1 comment:

  1. LoL it's the worst discussion I have ever read about .Net and java.

    Ask your friend why is the garbage collection bad.

    ROFL

    ReplyDelete