Friday, August 7, 2009

The Dot Notation Controversy

Sorry! It was Joe Conway, not Aaron who wrote the post. I've corrected the article below. The salient points are unchanged, but my apologies for mis-attributing

I knew that some developers didn't particularly care for Objective-C 2.0's dot notation, but I didn't realize how strongly some of them felt about it. Joe Conway of the Big Nerd Ranch has a very strongly worded post about the horrors of dot notation.

I have a lot of respect for Joe and the Big Nerd Ranch, and reading his post, I understand his complaints. He has identified some situations where dot notation can lead to confusion. In practice, however, I don't personally find the use of properties confusing in the slightest and think telling people to never, ever, ever use them is misguided imposition of a personal preference rather than sage advice. If there is anything close to a consensus on dot notation in the Objective-C developer community, it is that dot notation is a positive addition to the language.

Although, in terms of how they are implemented, dot notation and properties are orthogonal, I tend to think of them as working hand in hand. I always use dot notation for properties and bracket notation the rest of the time. Instead of making my code harder to read, I personally find that it makes it much easier. The only possible point of confusion I've had with this approach comes when accessing struct or union members owned by properties, but in practice, it just isn't a meaningful problem for me and it's offset by the benefit of separating exposure of state from exposure of behavior, something that has no language-level support in Objective-C prior to 2.0 and no language level support at all in most OO languages (though C# does, so kudos to Microsoft for that).

My only issue with dot notation, if you could call it an issue, is that since it doesn't mean exactly the same thing as it does in many other OO languages like Java or C++, it can be a bit of a stumbling block for experienced programmers who are new to Objective-C (and with the iPhone, there are a lot of those lately). It can be difficult for these people to make the transition because they subconsciously transfer their understanding of dot notation to Objective-C and think they understand something that they don't. The result of that is typically memory management problems that frustrate the hell out of them. But that's not really an issue with the language, it's just a training issue and hardly an insurmountable one. There are many happy, well-adjusted former C++ and Java programmers among the iPhone developer ranks.

So, keep in mind that although there are a few well-respected and knowledgeable developers who strongly dislike dot-notation, there are many more well-respected and knowledgeable developers who do use and like them (and I do too). You should definitely read Joe's post and give his opinion weight. He has been working with and teaching the language for quite some time and he's a really, really smart guy. But keep in mind that what you're reading is just one opinion.



16 comments:

Dzamir said...

I worked with a lot of programming languages (pascal, c, c++, c#, java, php) and I absolutely loves the way the dot notation works in Obj C.
When I started programming for the iPhone, a did read a lot of documentations, and I didn't have any problem at all with dot notations.

Eric J said...

I have to confess that the use of dot notation was my biggest gripe with your book. As a newcomer to Obj-C, the mechanics of bracket notation were immediately obvious and helped me get quickly familiar with the language. Dot notation reduced it back to voodoo.

On the plus side, slowing down to convert your sample code to bracket notation helped me read more critically than rote transcription, I think.

Jeff LaMarche said...

Eric:

One of the problems we had writing the book was that we knew we'd have a wide audience that included people with Objective-C experience as well as people without, and those without would be coming from a variety of backgrounds.

When it came down to it, we looked to Apple for our coding standards. They used properties in their sample code, so we decided to use them. We tried to address the possible disparity with a tech block on properties.

I'm really sorry that that confused you. If we could have come up with a solution that would have been perfectly clear to all readers, believe me, we would have used it. To be honest, I see far more complaints among new iPhone coders about brackets than I do about dot notation. The complaints about dot notation seem more likely to come from experienced Objective-C programmers.

higgis said...

Of course the .NET syntax of baz = foo.Bar; is just syntactic sugar for foo.get_Bar(); and foo.Bar = baz; is shorthand for foo.set_Bar(); it's up to each language whether to implement this, but the intermediate language doesn't have the notion of properties other than some metadata for reflection and compiler/IDE support.

In that sense, it's very close to Objective C's implementation with @property and @synthesize

Jeff LaMarche said...

Higgis:

C#'s properties are almost identical to Objective-C's both in use and how they are implemented. I was thinking more about Java and C++ programmers when I was talking about programmers coming from other languages.

Patrick said...

Two things.

First, the beef Joe has about dot notation really seems to be more due to bad naming than the notation.
-(int)value really should be -(int)getValue or -(int)getValueFromNetwork
Which tells you what it's doing. Blaming a new syntax for not being a crutch for your bad habits is silly.

Second, the whole point of breaking out functionality into methods is to not care about its internals a whole lot. If you're getting a value, why do you care how it gets it? If you have to know, proper naming of methods/messages will help. One should really think of your properties as inherently being a getter and setter instead of "direct memory access" which is kind of a relic from C. Some languages let you override those implicit getters and setters anyway, not to mention operator overloading.

K. A. Barber said...

As someone who works in a multi language, multi OS, multi IDE shop the dot notation is no more confusing than anything else I deal with daily. I like your "benefit of separating exposure of state from exposure of behavior" reasoning. Properties are very convenient in my C# projects and I use them the same way in my Obj-C projects. I do like the fact that everyone is so passionate about it though.

Jeff LaMarche said...

Patrick:

Actually, the use of:

-(int)value;

is actually, by long-standing naming convention borrowed from SmallTalk, the preferred accessor naming convention in Objective-C for accessor methods.

-(int)getValue;

is understood by KVC and properties, but it's documented in several places that value is preferred over getValue. In fact, the terms getter and setter weren't widely used in the Objective-C community until fairly recently. To old Cocoa farts like me, they are "accessors" and "mutators". :)

Seriously, though, I think Joe's issue really is that he sees the potential for confusion in the dot-notation for getting to accessors and mutators. That, combined with a lot of experience doing it the other way.

Jeff LaMarche said...

K.A. Barber

If you're used to working in multiple languages, you're more sensitive to the fact that things can look the same yet can have different meanings in different languages.

However, there are an awful lot of people who are coming to the iPhone after spending years working almost exclusively in one language. For those people, it really is hard to re-think something as basic as dot notation because its use is second nature.

Michael said...

I love dot notation...but maybe its because I've never known anything different - taught myself C and Obj C only last year. Anyway seems completely logical to me.

K. A. Barber said...

I remember having an equally distasteful experience with the managed C++ handle operator "^". Just couldn't get use to it at first. The more I used it (the more experienced I gained) however the more comfortable I became with it. Everything has a learning curve.

I took a calculated risk when I went out and picked up all of the resources necessary to write software for OSX and eventually IPhone. Lots of developers in my core friend group thought I was crazy and should wait for a C++ based platform. Most have relented and are now warming up to the idea of OSX Obj-C based development.

I do understand that sometimes using new technology is frustrating. I am however under the opinion that it is in our job descriptions to be a little frustrated sometimes.

Jeff LaMarche said...

K.A. Barber:

I agree wholeheartedly. I think a tolerance level for frustration is one of the (many) prerequisites for becoming a great programmer.

However, many of the people that I, specifically, deal with, are not to the level yet where they have a "job description" that includes Objective-C, and those people have experiences ranging from no programming knowledge up to expert level in some other field. I try to be sensitive to how difficult this stuff can be and to remember that it's not the same things that everyone has problems with. That's not always easy once you're over the hump yourself.

I, personally, think it's a good idea to use properties and dot notation to separate behavior from state. But this is just one possible way of using these language-level tools (one I stole from Chris Hanson, actually), but it makes perfect sense to me. It fits the way I think and work. Obviously, it doesn't fit the way Joe works, which is perfectly cool. I don't want to say Joe's wrong, I just want to say that just because one expert doesn't like something doesn't mean it's inherently evil.

Mark said...

How would you make arguments against Google's Objective-C Style Guide:

http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml?showone=Properties#Properties

Jeff LaMarche said...

Mark:

See my latest blog post where I specifically address their arguments.

Jeff

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels