Tuesday, April 10, 2012

Objective C - Dot notation or Message (bracket) notation

Do I prefer the dot notation or the bracket notation. If you spend most of the time in languages like C# then switching to Objective C will definitely generate an opinion or two on the bracket notation. I think it's great sometimes, then I think it's not. What I do like is the way we are forced to think about messages rather than simply calling a method on an object. That is refreshing somehow.

Readability

I'm convinced that the readability of the .notation is somehow easier to read, but then I think it misses some of the action of what is going on. Objective C is about messages and in #2 the message sending seems more obvious.

1. [myObject.doSomething sendAMessage]
2. [[myObject doSomething] sendAMessage]

But is that important when reading someone else's code? Yes I think it is. If I stop looking at the code above as if it were mine, and instead think of it as someone else's, and if I imagine I'm not happy at having to look at this person's code, then #2 does seem easier. Weird.

Attributes and Properties

Well perhaps this is ok for dot notation. For properties it seems to be much better.

1. [display setText:digit]; 
2. display.text = digit;


Structs

You can't send a message to a struct so the dot notation is a simple way of telling the reader that the reference is actually a struct rather than an object. This is a good thing. But, should we really care when we are reading someone else's code. I think not, in general. I think worrying about types is secondary information when understanding code.

Conclusion

I'm probably a fan of Message Notation over Dot notation, but I'm not sure why.
Dot notation for properties, message notation for behaviour is the way to go.




No comments:

Post a Comment