Wednesday, November 21, 2007

C# 3 / .NET 3.5 / VS 2008

Microsoft released Visual Studio 2008 together with the .NET 3.5 framework and C# 3.
There are some new exciting features included in this release.
Here are some of them:

1. "var" - You can assign any object to a var (for instance: var i = 1). This is not the same as using "object" because the compiler (and VS) will figure out the actual type and make sure you don't make mistakes.
You can see how VS figures out that "j" is of type string and autocompletes the correct methods:





2. Simplified properties - Instead of having to write getters and setters blocks for class properties, you simply have to declare a "get; set;" block. For example the class User:



The compiler will automatically create the needed class fields.

3. Class and collection initializers - after you defined your class properties you can easily create a new class and initialize it with default properties.
So the class User can be instantiated like this:



4. Extension methods (duck typing in other languages) - You can add methods to an already existing class. You define a new extension method and the compiler will attach your method to any type that matches the definition - of course once you define the extension method, VS will become aware of it and will autocomplete it for you.

In the example below I define an extension method for the type "string". I want to add the "encrypt" method to string, so first I define the extension method - this is a simple method that encrypts a string but notice the encrypt method signature:

public static string encrypt(this string plaintext, string password)

It must be static and have the keyword "this" before the first argument. The "this" keyword will instruct the compiler to attach the encrypt method to the "string" class (string is the parameter type of the first argument).
Notice that the class is defined within the "Technital" namespace so only strings that are used inside this namespace will be affected (like strings in the User class defined before).



Here I use the extension method on the Password property from the User class (a string). You can see VS picked up the extension method and now offers it in the autocomplete box.



So here is the final result:



5. Anonymous types - You can declare anonymous types. They are treated as "object" and the compiler will internally assign them with a name. This is a feature which is used quite a lot with LINQ



6. Lambda expressions - C# allows using lambda expressions (which are just another way to define anonymous functions). They are defined like this:



And are especially useful when used together with LINQ (which I guess is the real reason why this feature was introduced):



Now comes the obvious comparison to Java...

When comparing these features (and considering how young C# is) you can't help but wonder how long will Java be able to hold its head above the water? I mean java stakeholders are still debating if stuff like delegates and properties should be included in JDK 7 (you can find all kinds of missing features here)
Not to mention killer features like extension methods or LINQ. We can only dream of having these in Java.

It seems like Microsoft are aware of the popularity of dynamic languages like Ruby - you can see they borrow a lot of the new features from such languages. They have developers in mind.

Sun on the other hand, are thinking of Mathematicians - not developers. They are so careful of making sure that every aspect of the Java language is mathematically valid that they forget that they are dealing with innovative software developers that want their favorite development language to be innovative as well.

The innovative developer that I am (or at least want to be) - I guess my favorite language can't be Java anymore.


Come on Sun, wake up and smell the Java.


Saturday, November 17, 2007

Concurrency on multiple core CPUs

As you are well aware, CPU manufacturers are in a race to create multi core CPUs, since they found that increasing clock speed has it limits. So now Intel aims on releasing 32 core CPUs two years from now.

So, how does this affect popular platforms such as Java and .NET? Well, they both use a shared memory model to support multithreading. Every time you want to acquire a lock you need to stop all threads. This is an extremely bad thing to do on multi core machines - you're waisting a lot of resources.
I guess if you're writing desktop applications this may not be so bad since you will probably be able to rely on the OS to do its magic and run each process (yours being one of them) on a different core. But what if you're developing a website or an enterprise application that will probably have it's own dedicated server? In such a case it becomes your responsibility. So what can you do?

Languages such as Erlang and Haskel are functional languages that use message passing instead of shared memory (sometimes called "shared nothing") - with message passing you don't have a shared memory - so no locks are needed and you're free to scale to as many processors as you'd like.


Now, in my opinion, you have three options:
1. Start learning Erlang or Haskell, which are fundamentally different from what you're used to if you're a Java/C# programmer.
2. Hope Sun and Microsoft rewrite their VM's to support message passing effectively (I highly doubt this would ever happen). You could do message passing even today but since the VM is not designed for that - you will get horrible performance.
3. You can be lazy and wait but there's a chance you will write really shitty software 3 years from now.


There's a good talk with Joe Armstrong, inventor of the Erlang programming language, where he explains the fundamental difference between message passing and shared memory:
http://channel9.msdn.com/ShowPost.aspx?PostID=351659

Tuesday, November 13, 2007

Distributed Version Control and Agile Development

Traditional VCS are always about a centralized (slow?) server where every one in the team has to sync their work. For a number of reasons, this does not fit well with the dynamic, feature based nature of the agile development processes.
In an agile process you want small teams to be able to develop their features independently without breaking other teams work. Unfortunately, we all know how someone makes a change that breaks the build for everyone else (personally I try to sync as little as I can so I won't be affected by other people - not to say its a good solution).

It seems that distributed version control (DVCS) is better suited for the needs of agile teams.
A DVCS does not require a centralized server (but you will most likely have one - to hold the ultimate truth about your project). Instead, in a DVCS environment, each developer has their own private branch on their local machine.

When I'm part of a feature team I can push my changes to my buddies and can pull their changes. This way we collaborate in a peer-to-peer fashion without affecting other teams. I also argue that in such a way we're more aware of what other people are doing because we're actually aware of the changes other people do and we can review them.

When we're ready we can check in our finished feature to the centralized (possibly legacy, SVN, Perforce, Starteam, ...) repository.

Such systems already exist but don't expect a rich UI - for instance Mercurial is popular in the open source community

For a (not so long) introduction to DVCS read this: http://betterexplained.com/articles/intro-to-distributed-version-control-illustrated/

And a (longer) google talk: http://video.google.com/videoplay?docid=-7724296011317502612

Monday, November 12, 2007

Mozilla Prism

Here is another thing that's going to blur the borders between the desktop and the web.
Mozilla is working on Prism which is a platform for development of web enabled desktop applications.
This is a trend - you can find similar platforms for doing this, for example: Adobe AIR.

There is of course the other direction - bringing the desktop to the web (g.ho.st is a nice example).
My personal opinion is that although this is definitely cool - it is not something people will actually use in the near future. Especially stupid is the browser inside a browser these virtual desktops have.

Amazon EC2 and S3

Amazon is hosting so interesting virtualization services called EC2 (Elastic Cloud 2) and S3 (Simple Storage Service).
EC2 is a hardware virtualization service - you basically create a virtual machine and run your software on top of it. You can create as many instances as you want and you only pay for what you use.
S3 gives you a storage API - here too you pay for the space you occupy.

There are some very interesting things which are being deployed on EC2/S3 for example:


NY Times Converting TIFF to PDF

Facebook applications

Hadoop

The Android platform

Google released their mobile phone platform.
http://code.google.com/android/

If you have a really good idea for an application - you might get some of that 10 mil. dollars they're offering for developers.

BTW, although it looks as if Android is running Java, it doesn't - it's running on Dalvik VM - the Java source code is not compiled to Bytecode!!! The main reason for this is the licensing of the J2ME.

To see some more details read this:
http://blogoscoped.com/archive/2007-11-13-n83.html