25 June 2007

Bittersweet C#

I definitely love C#, in many ways - simplicity of its syntax, the rich library that comes with it and more. Nevertheless, its just drives me crazy when I find some conventional features are missing. And some parts are awfully designed or implemented.
Here are some examples.

At first I thought no checked exception was a good thing to have, it saves some keys strokes when you don't want to bother with wath exception will be possibly thrown. But later, I find it really a bad idea to remove checked exceptions, if you are serious about writing reliable code rather than quick code. At the time when you start to care about exceptions, it not easy to find what are the exceptions are will be thrown. It all relies on intellisense or the MSDN documention (if you are not using visual studio, the online MSDN is the only option left). Without the checked exception, it takes more time for a programmer to realize that "compile and run doesn't mean the program is correct". While in java, the compiler will complain if you don't explicitly handle checked exceptions. I know it a good intension to disable checked exception to make programmer's life easier. But there are some programmers who need at least warnings for unhandled exceptions, or an option to enable the warnings.

Another example would be the ScrollableControl, which a lot programmer complain about. It a typical example of poor designed & implemented class in .NET library. Sometimes they just make doubt whether they are designed and written by some intern students.

I like Generics very much, but C#'s generics is crippled when handling Collections of primitive types. There is no built-in way of say, create a list of gerenic numbers (can be integer or floating point) and write a Sum() method for it. Because or numbers types do not inherit something like "INumber" or "NumberBase", and there is no common method you can call except those from Object class (ToString(), GetHash() ...). Java has a better approach on this matter.

And the following is the new fresh problem I met today. There is no elegant way to determine if a path (as a string) is a file or a directory, like File.isDirectory(path) in java. The equivalent way in C# is:
if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory)
It really made me miss java more. It is not a few key strokes that I care so much, but it is the effectiveness of calling API's. Sometime libraries can be so well written that you don't even have

0 comments: