2010
01.17

I am learning Objective C at the minute as Mac users seem to actually buy software D= and because I am hungry to learn something completely different and new.  I’m coming from a Java & C# background.  I thought I would share some of my thoughts on the two languages. This is not a “How To” guide nor direct comparison of the two languages.  I am just sharing some thoughts and perspective. I really appreciate comments and feedback no matter how critical.

This post will be complemented with a comparative implementation post where I will show the same solution to a problem in C# using Mono and Objective C using Cocoa. (*Thinks to self, I should use C# to write a Mac App with Mono and Cocoa# if I need some UI and the Objective C solution to run on Windows using GNUstep libraries).

C# -  Extreme Growth and Evolution

I do about 90% of my development with C#.  I love that it is such a Swiss Army Knife of a language and that its constantly growing to become an better multipurpose tool. 

The language first came onto the scene in 2001 with it’s 1.0 release. Now in 2010 we are scheduled to see the official release of C# 4.0 which is a completely different animal indeed.  C# 1.0 was very much like Java, even with version 2.0 the same could be said, but as version 3.0 came out we started to see something very different from Java indeed.

Check out the Language Specs by ECMA if you care that much =]

With Great Power Comes Great Responsibility

The flip side of a language which is gaining more and more features is inconsistent development practises in said language will become an issue. 

C++ is famous for this.  For example, if you have a relatively simple task and got 5 developers of differing experience and ability to each implement the solution with a feature rich language you will get 5 very different solutions.  The junior developer will look at the experts code and it will seem like some crazy voodoo.  [I hope to go into this topic in more detail in another post in the future.]

I would classify myself as a relatively experienced when it comes to modern C#, I am familiar with 99% of the language features and I would generally use the best tool for the job (but I am young and far from perfect it must be noted!). 

But say I was to write a solution using the powerful functional language features such as passing delegates, composing numerous expressions using Expression Trees in a resolution based approach to the problem, or even using a multi tiered LINQ query which used the lambda syntax for delegates… I would not expect a Junior C# developer to be able to effectively grok and maintain such a solution without the overhead of having to learn and master these aspects of the language (and in many cases the underlying framework).

Of course the answer is to define coding policies (possibly enforcing them using a tool like FXCop), but this will enviably be broken and restrictive (as in my humble opinion this always is even if its not at the point of conception).  The best or most efficient or most elegant tool (in this case language feature) for the job may be a company policy NO NO!

Objective C

As I learn Objective C, I see a language which..

“was created … in the early 1980s” 

http://en.wikipedia.org/wiki/Objective-C

and only reached version 2 in 2006.

“At the 2006 Worldwide Developers Conference, Apple announced the forthcoming release of "Objective-C 2.0," a revision of the Objective-C language to include "modern garbage collection, …” http://en.wikipedia.org/wiki/Objective-C#Objective-C_2.0

I also see design patterns and conventions as a first class citizen in the Objective C and Cocoa world.

What is my point? ..consistency by being so lean.

Teams developing for the Mac & iPhone are aided by this consistency.  It is very likely that developers for these platforms could move to a different company and become productive on a code base very quickly and effectively as the way things are done (at a high level) will not vary a great deal. 

The MVC pattern is an integral part of how applications are developed with Objective C, Cocoa and Interface Builder.  This helps a great deal.  With C#, .NET and the associated View technologies such as WPF, WinForms, WebForms, GTK etc the design pattern in place ranges from MVC, MVP, MVVM to none what so ever which is too often the case to the expense of my (and many others) mental health.  This means that the consistency is not in place and design patterns (i.e. separation of concerns) are not seen as a fundamental as much as they are seen as an advanced topic and higher learning.  Microsoft have released their MVC framework for web development which is called ASP.NET MVC.  This is a step in the correct direction, not to mention they released it under the MS-PL license which allows the Mono Team to take advantage of it as part of the Mono core libraries.

Objective C as a language is what it is, a C variant which introduces object orientation and the notion message passing to call methods on instances using a “infix” notation as opposed to “postfix” which most people are used to.

Example: Calling a method in ObjC, C++ & C#

  • [obj method: parameter]; //Objective C – Infix
  • obj->method(parameter); //C++ – postfix
  • obj.Method(parameter); //C# – postfix

As the language is low level enough, even if the solution is quite complex relative to the equivalent C# implementation there is very little that can’t be done.

In my discussion of C# I tried to talk purely about the language, not the underlying framework be it .NET or Mono. With Objective C much of its power comes from the Cocoa libraries, the same can be said about C# and its frameworks I guess, but C# as a language is far more feature rich.

I am really exited about learning Objective C, its dynamic Small Talk-esk nature intrigues me.  It is lean in language features relative to C# and that I believe is its biggest advantage.

Generalised Comparisons of C# & Objective C

C# is powerful and can be very elegant and highly productive (in the correct hands).  The language features such as LINQ, lambdas, anonymous classes, expression composition, statically compiled dynamic madness, covariance & contravariance… are something special, but the languages many advanced features may baffle and confuse those who are not intimate with it.

The development stacks which C# lives in lack structure out of the box it could be said, this can lead to some nasty un-maintainable evil without the correct guidance.  When done properly C# very plays well with tooling allowing for highly accelerated development and refactoring with ease and efficiency.  Objective C (which I am no expert in) seems to exist in a world of structure, best practises and conventions without taking it to the extreme as Rails does with Ruby (i.e. without a 500 page book on conventions).  The language is light when it comes to features but by its nature it is by no means weak. 

C# lives on a higher level, it is about 4 or 5 generations away from C whereas Objective C is 1 generation about C. 

There low level control fits well with the Apple ethos.  The proprietary nature of Apples hardware and the low level nature of Objective C means you can interact with the hardware components, with a large degree of control and in a consistent fashion without worrying if the hardware will work with your app.  So Objective C is a better fit with the Mac in that regard.

In comparison C# and Java live at a higher abstraction.  They are designed to work with a massive range of machines with potentially infinite hardware configurations so the fine grained control is sacrificed for portability. That is not to say you can’t call into C code from C# or Java when the need is there.

//Todo: This post is in need of Refactoring, but all the tests are passing =] (i.e. I think I said all I wanted to)!