The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,902,099
since: 19 Jan 2005

Objective-C 2.0's Accessor Dot-Notation : Friend or Foe?

posted Tue 03 Jun 08

When Objective-C 2.0 came out, I was as giddy as a school girl. Mainly because it came with some features that, having spent the last 7 years of my life writing .NET code, I could not live without. These features included garbage collection, the concept of properties, and the ability to refer to those properties with dot-notation syntax. All of those combined made Objective-C 2.0 feel very familiar and gave me warm fuzzies when I would switch from C# to ObjC and back again.

Last night I was reading a little of Aaron Hillegass' book (third edition) and I noticed that he spends about a paragraph illustrating that there is a dot-notation syntax for accessing object properties, but he actually refers to that syntax as silly and then goes on to say that he will not be using that syntax at all in his book.

Looking at it from his point of view, the point of view of someone who has worked exclusively with Objective-C for a very long time, I can see his point of view. However, from the point of view of programmers looking to make the switch from the .NET world to the Cocoa world, this syntax isn't silly, it's pure gold.

Take a look at this line of C# code:

this.Power = bob.Power+12;

Pretty straightforward. Nothing out of the ordinary - just setting the Power property of the current object to Bob's power plus 12. Everybody who is familiar with C# will know exactly what this line of code does. Now take a look at that line of code in traditional Objective-C:

[self setPower:[bob power]+12];

I can totally see where Aaaron is coming from - this line of Objective-C is very clear and very self-documenting. You are setting the "power" property of "self" (equivalent to "this" in C#) to the power property of the bob object plus 12. There are a couple of things here that a C# programmer will dislike. First, it uses the "setXXX" syntax which looks so much like Java it triggers my gag reflex. I have nothing against Java personally, I just dislike using it for the vast majority of tasks (though for some back-end tasks it still remains quite useful, but that's another topic for another post).

The real problem here is grammar. A C# programmer expects properties to be nouns and they expect methods to be verbs. Not just C#, but any object-oriented programming language with robust property syntax that abstracts getters/setters (e.g. not Java) will have the same grammer assumptions. Objective-C, on the other hand, treats everything like a verb. Messages passed to objects are verbs, and accessing a property for get or a set is still treated like a verb.

With that said, let's take a look at what the same line of code looks like in Objective-C using the dot syntax:

self.power = bob.power+12;

A little more straightforward, isn't it? (at least to a programmer who did not learn how to program using Objective-C).

In short, I can see why programmers who cut their teeth on Objective-C would feel that the property notation is silly, because their grammar has always been about verbs (messages). They think in messages. However, programmers coming to the Mac from other platforms such as the .NET Framework think in terms of verbs and nouns, and the dot syntax removes that one extra translation step that needs to be done every time they access a property and goes a long way toward removing the intimidation factor of Cocoa and Objective-C 2.0.

So, I will continue to use the dot syntax and I think that people who refuse to use the dot syntax have plenty of valid reasons for doing so. However, doing so in a public way runs the risk of alienating a lot of potential newcomers to the Mac platform by adding confusion when none is necessary.

tags:            

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Rob left...
Tue 03 Jun 08 8:08 am :: http://www.bluespire.com/blogs

Speaking of .NET and ObjC...Are you still working on a book that uses both? I'm looking for a good way to transition my .NET experience to ObjC. If you are still working on the book and would like some feedback from someone with that perspective, I'd be glad to assist.


2. Kevin Hoffman left...
Tue 03 Jun 08 8:24 am

I am not working on that book, no... I'm working on another book that I think people will find ridiculously useful (provided you're a .NET programmer). I am still looking for a better, alternate medium for delivering my dogma on converting from .NET to Cocoa... so if you have any ideas, I would love to hear them.


3. MrScrith left...
Tue 03 Jun 08 9:19 am :: http://www.trouserenthusiast.com

I would really would love to see something specifically for transitioning from .NET to ObjC, I'm trying to do that on my own time and having a hard time.

I started out programming VB6, moved on to PHP, learned a little C and Java (enough to read, not do all that much), and when .NET came out learned VB.NET and C#, now I'm trying to transition from .NET and Visual Studio to ObjC and XCode... and not having much luck.

I think even having a site/blog/wiki on transitioning various aspects between the two would be awesome (ie. this is how you do this in VS/C#, this is how you do this in XCode/ObjC). Just so I can get past specific roadblocks that leave me with headaches. I think most of the code is portable since it's C based (logic and flow control structures), so you don't have to bother with that, just where C#/VS and ObjC/XCode diverge would be helpful.


4. Marcus S. Zarra left...
Tue 03 Jun 08 11:29 am :: http://www.cimgf.com

The dot notation is a foe without a doubt. While it may appear easier and cleaner to coders coming over to Objective-C it is masking the reality of what is going on. when a coder calls self.blah = 12, it is NOT setting a property! It is sending a message to the self object of setBlah:12. This can be tested by creating a setter method manually and logging the call.

Developers coming over to Objective-C should not be using dot syntax until they understand the language. Walk before running and all that. Using dot syntax from day one will burn a new developer coming over to the platform because they think they are working with an iVar directly when in reality they are not. Learn the message passing and WHY it is different than the C++ variants. Learn WHY Objective-C does things the way it does them instead of trying to make it behave like the language you are coming from. Only then will the developer be in a position to decide the best way to write Objective-C code.

If new developers try to make Objective-C into the new C# they will do a disservice to themselves and everyone who has to use their code.


5. Kevin Hoffman left...
Tue 03 Jun 08 11:39 am

Every C# developer knows that setting a property is NOT working with an iVar directly, also. They know for a fact that bob.Size = 21; is actually calling the object's internal setSize method (you can see this in the IL code that is generated when you a compile an object). If the C# developer doesn't know this about their own native languge, they have no business learning a new one :) In short, your point is valid, but my point is that C# developers don't currently, and never have, thought that setting a property was a direct iVar access - they've always known that under the hood the setter code is being invoked.


6. leeg left...
Tue 03 Jun 08 12:19 pm :: http://iamleeg.blogspot.com

I think there's a subtle distinction between the two styles, such that one isn't redundant syntax for the other. The accessor notation (/[foo setBar:]) does indeed call methods. But the property syntax (foo.bar) might not be calling methods. It may indeed call accessor methods, but then again it might be doing KVC, it might be doing some clever lazy property magic, it might even be doing some secret future property magic which is only available in the 10.7 runtime. The point is that whatever it really does, you can be confident that the _property_ bar of the _object_ foo has indeed been used.

I'm personally in favour of the concept of properties, and indifferent to the expression of properties through the dot notation. But I was going to write about exactly this paragraph, so thanks for saving me the time! ;-)


7. jswain left...
Thu 05 Jun 08 4:01 pm :: http://www.i-graph.com

The problem with the . notation is that it looks like a noun, or looks like it is setting a property, but it is actually sending a message. The property notation evolved out of public member variables (or properties as they were called in some languages). The idea of public member variables is bad because it ties your implementation to your interface. To get around this developers started using setters and getters to protect their member variables and to give them the flexibility to add side effects or validation. The trouble with getters and setters is that you end up doing a lot of typing for something that is a very common operation. So now you have property constructs that save you the typing but still create the getters and setters behind the scenes. This could be thought of as the best of both worlds as you save the typing but still allows for a flexible implementation behind the interface. The trouble is that properties imply member variables and may lead to assumptions by the users of your interface, the main assumption being that setting a property is side effect free. This is not always the case, therefore it is more correct and less confusing to use the method call (or message) syntax because that is what is actually happening in your code.


8. Kevin Hoffman left...
Thu 05 Jun 08 7:09 pm

That's awesome. I've got to say that's one of the best explanations of the dot notation in terms of Objective-C that I've seen/heard. I'm beginning to like the dot notation less and less ;)


9. Sanjay Samani left...
Sun 08 Jun 08 9:43 am :: http://daytimesoftware.com

The problem with dot notation in Objective-C is not with the simple example that you give, but when you mix it up with standard Obj-C notation in larger more complex code snippets. Particularly when you are dealing with structs, the notation can actually be harder to read as you are dealing with two, very different notations:

For example, in the following example amongst lots of other code, it can be confusing at first glance whether you are dealing with a struct or an object.

Although this is also a problem in C++ / C#, the problems are slightly mitigated as there is consistent notation for method calls / message passing and property accessors.

In Obj-C it is not unheard of to have complex spaghetti code is not uncommon in Obj-C and can actually be perfectly readable with consistent message passing notations, but from real-worl, real app, experience, I personally find the mix of notations to be less readable. As a result, I decided not to adopt dot notation for my Objective-C properties.


10. Steven Noyes left...
Sun 08 Jun 08 10:51 am :: http://www.pbase.com/snoyes

I tend toward foe. Having learned Basic then 6502 then MS Basic then Quick Basic then C then 8086 then C++ and finally Objective-C (and others along the way), I finally found a language that was easy to understand what was going on just by looking at the code. I am not a fan of code obfuscation (some call it templates, operator over-loading, function overloading and such) and find the "dot" syntax of properties in Obj-C 2.0 to be another form of obfuscation. Is it referencing a C++ class? a struct? an Obj-C Object? I simply don't know and I have to go find out if I ant to know. Many times it does not mater, enough of the time... It does.

I am a firm believer in being able to know what each line of code is going to do just by looking at it and find the "dot" syntax a step away from that.

JMHO of course.


11. Kevin Daly left...
Sat 14 Jun 08 7:46 pm :: http://www.kevdaly.co.nz/weblog

Come on kids, hop off the high horse. We had *exactly* the same pontificating from the Java snobs when C# appeared, on exactly the same subject. This from a group of people who subsequently failed miserably to implement generics in a way that is actually useful. But I digress. Properties provide a useful abstraction - and when setting a property involves side effects, those side effects should be part of the abstraction and intrinsic to the user's understanding of what the property represents (and yes, used sparingly). There is no more reason to reflect the message sending in the syntax than to directly refer to registers and addresses.


12. Anonymous left...
Sun 31 Aug 08 2:12 am

There's a little problem with ambiguity with this syntax, that does not exist in other OOP languages such as C# and Java. Namely it's that of structs vs objects. When you look at the code "self.power = bob.power+12;", how do you know whether self (well, this is a bad example, ignore the obvious) and bob are objects or structs? You just can't. "struct { int power; } self, bob; self.power = bob.power+12;" would work just as well.

There's also the confusing situation when a variable is public. Now I know variables shouldn't be public an all that, but let's say that a variable is public within a class. In Objective-C you can access it directly by using myObject->myVar. If it's a property, you can also use myObject.myVar to use the setter. Any C programmer would be completely confused looking at the two.


13. Kevin Hoffman left...
Tue 02 Sep 08 5:33 am

Hmm, small nitpick, but C# has the same issue. You can't tell the difference, just by looking, that bob.name is a struct member access or a class member access. That said, you rarely see structs used in the wild in C# other than special-purpose occasions.


14. Thorn left...
Mon 01 Dec 08 6:17 am

First, I agree with author - DOT syntax much simpler and easier to read. For everyone. But apologists of Obj-C looks like fanatics of 'passing message' religion. Actually, 'messaging' is just a CONCEPTION, nothing else. It's 'engine' of language implementation, but who said that SYNTAX must be absolutely the same? Syntax made FOR HUMANS, not for engine. This is why we have widely used 'syntactic sugar' everywhere - because it clears code => clears your mind. If is a message 'b' to the object 'a', who cares how it looks? 'a.b' is not a crime, not a conception violation - just simplifying of your life. Hope ppl will share my opinion, not as usual just closing eyes.


Tag Related Posts

Would you like to touch my mono?

Mon 23 Nov 09 2:59 P GMT-05

Upgrading your Leopard install to Java SE 6 64-Bit

Mon 12 Jan 09 1:38 P GMT-05
tags:            

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

My Leopard Installation Experience

Sun 28 Oct 07 12:57 P GMT-05
tags:    

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags: