Hello World using MonoTouch

Posted by ESCOZ on Thursday, March 25, 2010

At the MSDN Blogs, Shawn Burke has recently decided to do a comparison of the iPhone SDK and the Windows Phone 7 Series SDK (what a horrible, horrible name!) by writing a simple Hello World application and showing the difference between the two.

While I don’t think it’s really fair to compare a C-based platform with a .NET based one, Shawn’s point that the it takes a lot more code to write the same application is real. It’s also real to say that the iPhone SDK could run extremely well in a 400MHz processor three years ago, while the Microsoft will need a 1+Ghz processor and still isn’t on the market, but that’s a different conversation.

Anyway, I work daily with MonoTouch, a port of the Mono platform to the iPhone, and thought it would be a good idea to show how concise iPhone development can be when you write code on top of the platform. Here are the steps:

  1. Open MonoDevelop, select File->New Solution->Type in name->click Forward->Click OK.
  2. Double-click MainWindow.xib
  3. In Interface Builder, drag and drop a UIViewController into the xib, and then a UIView inside the UIViewController
  4. Drag and drop a UITextField, a UILabel and a UIButton into the UIView, and change the text on the controls accordingly.
  5. Define three outlets in the AppDelegate class (for simplicity), and connect them to the UIViewController, UILabel and UITextField added previously.
  6. Add and action to the AppDelegate called "OnButtonClicked" and connect to the TouchUpInside event of the button.
  7. Control-S to save, Control-Q to close Interface Builder.
  8. Back in MonoDevelop, open up Main.cs and inside the FinishedLaunching() method, add this line of code: window.AddSubview(ViewController.View);
  9. Outside of the method, type "partial", and select the OnButtonClicked option.
  10. Finally, write the code to set the text: partial void OnButtonClicked (UIButton sender) { label.Text = String.Format("Hello {0}!", String.IsNullOrEmpty(textField.Text)?"World" : textField.Text); }
  11. You're done! Press Command+Enter, and the application will run on the simulator.

You can take a look at the entire solution here. You won’t be able to open the solution in Visual Studio, but you can look at the files in any editor and see the code I wrote above. It even includes copy/paste! (yeah, low punch, I know).

So, at the end, how many lines of code have we written? 3 or 5, depending how you count. If I had written the code the same way as Shawn, it would be more like 7-8. So it’s more code than when using the Windows Phone SDK, right? Well, I would say wrong. Here’s why:

Although we used XML on the solution (the XIB file is XML afterall), we absolutely never touched the file at all. For all purposes, the code in that file doesn’t exist at all. I’ve been developing for 5 months using MonoTouch now, and I never had to open the XIB in anything other than Interface Builder.

So when Shawn mentions that he can write the entire thing in only 4 lines of code, (or even zero!) that’s not entirely true. You’ll be hand editing the XML directly daily, to the point of even writing untested, non-testable business logic in it (like Shawn did in his example). And that XML will have bugs in it (yes, it will). And if you’re going to do it, you better count those lines of code as well. In Shawn’s case that adds 15 lines and a totally different language developers will have to get used to.

Isn’t it funny how you never really hear objective-C users complain about their platform? The iPhone SDK is a really well developed API, and really simple to use to create applications. It’s different from Microsoft’s solution, but that doesn’t mean its bad (some would say its actually better just for that, I prefer to stay in the middle).

Before anybody says anything, yes, I used MonoTouch, and not Objective-C like the Shaw’s example. My opinion here is that the language doesn’t really matter: the framework does. What’s the problem if it takes 5 more lines of code in objective-C than C# to do something? I bet a lot of those obj-c developers can write those 5 lines a lot quicker than I do.

I love .NET, and I’ll likely be writing many apps in the WP7SSDK (worst acronym ever, I guess) in the future. From what I’ve seen so far, the platform sounds very interesting, and it’ll be great to reuse code from MonoTouch and just have to rewrite the view code for the new phone.