June 2010

Fri Jun 25 10:31:20 PDT 2010

Java vs C#

This is something of an unfair competition, since the Microsoft team that came up with C# had the benefit of over a decade’s experience with Java to learn from, while the Sun team that came up with Java was pioneering uncharted territory when they developed a byte-code-compiled language that ran in a virtual machine.

Not surprisingly, therefore, C# comes out the winner. It actually surprised me, though, simply because I had written C# off because of where it came from. I’ve seen enough Microsoft products that are inferior to the UNIX/open source world that it came as a surprise to find out that Microsoft seems to have learned something from all the criticism leveled at them over the years. They actually hired a respected software engineer to design the language.

To pick a few examples:

Operator overloading. At first this may seem like nothing more than unneeded cruft in C++, but it ends up being quite important. Without it, any user-defined types end up doomed to being "second-class citizens" in the language.

This can be significant. hurting the quality of the code developers write in one important case. Pretty much every code base of business-oriented Java I’ve looked at handles monetary data incorrectly, by treating it as floating point data. That’s bad, because binary floating point is incapable of accurately representing amounts other than those which are multiples of 25 cents accurately. Yes, the error is slight, but eventually it results in books that fail to balance by a penny or two. It’s the sort of thing that gives accountants heartburn.

This is why even creaky old COBOL had decimal arithmetic as a built-in feature, even back in the early days of computing. Java has a BigDecimal type that can do such calculations correctly, so in theory it’s a good language for business programming. But one cannot use natural arithmetic expressions in that type, because it’s not one of the built-in base types Java provides. Instead of being able to type something simple like:

double new_bal = old_bal - payments + deposits;
One must instead do the awkward:
BigDecimal new_bal = old_bal.subtract(payments).add(deposits);
Not surprisingly, most programmers respond to a language that punishes them for doing the right thing by doing the wrong thing.

In C#, there is no such disincentive to correct programming. One can just as easily type:

decimal new_bal = old_bal - payments + deposits;

Method delegates. In Java, there is no analogue to function pointers in C. You can refer to a class’s data fields individually with variables, but you are prohibited from doing the same for a class’s methods. Coding something like a state machine requires one to put a switch statement inside a loop. That switch statement must list all the possible codes for the machine’s states. If you want to write readable code, you should assign symbolic names to those states. So now you have to maintain two lists, typically at physically distant parts of the source code, one defining the states, and one listing them all in a giant switch statement.

In C#, one can use method delegates, which act much like the function pointers in C and C++. Make the state variable a delegate and the state machine’s loop becomes a one-liner that calls the method pointed to by that delegate. Not only is this more concise and easier to maintain, it actually runs faster: there’s no more switch statement to execute.

A better class library. The standard class library in C#/.NET just seems less awkward than the one in Java. I don’t think this is an accident. When Sun came up with Java, it was at first a solution in search of a problem. They tried to market it as an environment for embedded devices, with only very limited success. Then they tried pushing it as a dynamic language for building web sites with, with considerable success. Unfortunately, they had designed a class library suitable for embedded device programming. Now they had to hurry and add enough new features to make it suitable for Java’s newfound purpose.

A better-designed runtime environment. Java’s embedded-device heritage shows in some other areas, too. For example, there’s no portable way to have a Java program do something as simple as change working directories. File systems and operating systems often aren’t there on embedded devices, so the existence of changeable working directories was taken to be something which cannot be assumed to always exist by the Java VM’s designers. The CLI was designed from the ground up under the assumption there was going to be some sort of OS on the hardware.

Non-painful native function calls. Anyone who’s done battle with JNI knows that it’s a pain to use. When I tried to figure out how to do the same in C#, I got frustrated. I couldn’t find any examples of how to do it out there; they all seemed to be grossly oversimplified to the point of uselessness. Finally, I gave up and coded one of those obviously "incomplete" examples, expecting to at least get some help as to what else needed to be done from the resulting error messages. Imagine my surprise when everything simply… worked!

Compilation. In one way, byte-code compiled languages are a worst-of-both-worlds compromise: they combine the slowness of interpreted languages with the lack of immediate feedback of compiled languages. And no, just-in-time compilation isn’t really compilation; it’s more like a clever and efficient way to interpret code. Sometimes it works fast enough, sometimes not. C# has what’s called “ahead of time” compilation (i.e. traditional compilation down to machine code) available as an option.

Again, none of this is to bash Java, which suffers from the twin misfortunes of being a design that is now nearly 20 years old, and of being the first in its class. But since lessons can be learned from Java, it would be foolish not to take advantage of a language that seems to have learned many of them.

And no, it’s not strictly a Microsoft technology. C# is more than just .NET. .NET is Microsoft’s (and, yes, the first and the reference) implementation of the CLI (common language infrastructure), but CLI is an openly-documented standard and there is at least one very good open-source implementation of it out there. In fact, Mono is what I’m using to run the code I’m writing at the moment.

Sat Jun 26 19:22:47 PDT 2010

A Creepy Bill

This is an utterly creepy bill, and the fact that Obama is pushing for it demonstrates the limitations of working within the system.

An “Internet kill switch” in the hands of the government, particularly one with the extremely limited checks and balances this one appears to have, is a spectacularly dangerous idea.

Remember, Bush, Cheney, et. al. are still free. They are probably never going to be prosecuted for their crimes, unless there is sufficient grassroots unrest and disruption of the status quo from below. (And let’s be frank: there’s no evidence of that happening any time soon. So there is a lesson in what they did, and that lesson is that lying your way into war for personal gain works and is a practical strategy in the USA.

A lot of people made a lot of money on that war. Reward people for doing something, and odds are they’ll do it again. We are, in other words, almost certain to see a repeat of the Iraq debacle. And if that bill passes, the president will have broad powers to muzzle the Internet. Given that the Internet is one of the most significant vehicles for non-Establishment voices to be heard, this means the “kill switch” will almost certainly be thrown, and thrown for the purposes of manipulating public opinion.

Sun Jun 27 11:17:34 PDT 2010

A Bullshit Argument

You’re hearing it now about the protests in Toronto. You’ve heard it before when any sort of widely-covered left-wing protests have taken place. And I guarantee you’ll hear it again in the future: “Those protesters denounce capitalism, but what do they think pays their (or their parents’) wages?”

It’s a patently absurd objection. Just change a few words and it becomes: “Those Solidarność protesters denounced Soviet-style communism, but what do you think paid their wages at state-owned firms?”

Whatever the dominant system happens to be, virtually everyone will be getting life’s necessities via its mechanisms. It’s what makes a dominant system a dominant system, after all.

So virtually anyone who questions any dominant system at any point in history will perforce at be in the situation of denouncing that which arranges for their access to necessities.

Monthly Index for 2010 | Index of Years


Last updated: Tue Sep 13 16:14:10 PDT 2011