0.01 * 10 != 0.1

0.01 * 10 == 0.099999994

...if you are using single precision floating-point variables, also known as floats in C#. The solution? Using the decimal data type (or, if you are a Java developer, the BigDecimal class).
Although I didn't test it, this seems to be an issue in several similar programming languages, which use IEEE 754 data types. The problem is that not all real decimal numbers are directly representable in binary (e.g. 0.01), and as such are approximated. The solution involves using a special binary notation to support the accuracy required for financial applications.

Until next time, may you enjoy being surprised with something you though for sure it was true, but it was not, and you should have known better.


C#: a first look

I have recently started working with the C# programming language and the .Net framework.

The language (C#)

I started off with the notion that C# was Microsoft's version of Java, but that was it. After spending a weekend reading Inside C#, I got a pretty good idea on the language. My impression is that C# philosophy is quite different than Java's. While Java aims at being an elegant, minimalist language, with each new version making few changes in the Java platform; C# is a somewhat more programmer-friendly language, with as many bells and whistles as possible (this is actually a reflex of the different backgrounds and philosophies between Sun Microsystems and Microsoft).
As such, here are some things I liked about C#:
  • smart properties - it was about time someone recognized getters and setters as first class constructs;
  • overriding the '[]' brackets - this little syntactic sugar trick can make source code a lot easier and intuitive to read, by allowing that any class can work as an array;
  • structs - while this may seem not very Object-Oriented, it works well through the unified type model, since it is basically syntactic sugar for a class design pattern for storing data (no wonder it is a pending RFE for Java);
  • unsigned number types - correctly typed variables are very important;
  • delegates - while not strictly necessary, it is a nice alternative to Java's anonymous classes;
  • mandatory break statement in each case - while a small detail, this little rule eliminates a lot of nasty bugs using the switch statement;
On the other hand, there are other things which I didn't like at all:
  • polymorphism - having the possibility of defining methods which are not dynamically dispatched when overridden is a bad idea, since it basically breaks one of the core principles of Object-Oriented Programming: the substitution principle.
  • no throws clause - big mistake there, sure you can use the <exception> tag on documentation, but you don't have to, and still think that the best documentation is the source code itself;
  • the goto statement - I'm not dogmatic about this, but come on, even Kerninghan and Ritchie recognized that you really don't need this;
  • ref and out keywords - while this may be useful sometimes, I think it is a bad programming technique: methods should only return one value, period. If you need more than one, then you either need a new class to group these objects, or you are designing your class wrong;
  • XML documentation - this was almost a good idea. While it interesting to produce XML documentation, it is too verbose to read and write. They could have done something like javadocs and parsed it into XML, for transformation and documentation support.
  • overriding casts - maybe I'm wrong, but it doesn't seem like a good idea...

The environment (Visual Studio)

Regarding Visual Studio 2005 (.Net 2.0), there are a couple of things I would like to point out:
  • Auto completion works pretty well, including some new tricks, like completing parametric classes correctly.
  • Refactoring support is nice, but not as good as Eclipse.
  • Some source code options would be appreciated (namely automatic creation of constructors and getters/setters).
  • Produced documentation is a little too colourful and clumsy. Keep it simple. Take a look at Javadocs.
Until next time, may you enjoy joining the dark side (of software development).