Friday, October 10, 2008

@superfluous

A week or so ago, I decided to stop my Cocoa musing and focus instead on properly following Aaron Hillegass' Cocoa Programming for Mac OS X. The idea being that this will give me a more proper initiation to Cocoa, especially on the subject of KVC, KVO and the famous bindings. I started Chapter 8 last night and went through the implementation of the first version of the RaiseMan tutorial. This chapter also introduce the usage of the new Objective-C 2.0 @property, which can be use to automatically create setter and getter methods. Now, it's all good, I don't mind saving some time by writing less code, however I'm running into a bit of a personal conflict with this.

Like many other developers that have been around for sometime, I've grow over the years my own coding style, made up of all the things I have encountered and came to appreciate. One of the thing I find necessary is the prefixing of any variable by a lower case single character which give some clear hint on the origin of the variable (e.g iSomeNumber, aObject, lCount ...). In case you are wondering, I got into that habit from my time on Symbian. Now, there's no problem with using such notation with Cocoa unless you happened to want to define an instance variable as property.

What @synthesize do behind the scene is to generate the setter/getter methods using the name of the instance variable. So far so good. Now, if you happens to name your instance variable something like iSomeSumber, the setter will end up being -setISomeNumber: and the getting -iSomeNumber ... Not very elegant, right? (What I want really is the methods to -setSomeNumber: and -someNumber ... well really, I'd like the getter to be -getSomeNumber ... but that's another issue ...)

Since there is a refactoring tool in Xcode which will generate the setter and getter of a given instance variable, I do find the @property somewhat superfluous (Yes, I do know that the property allow for more than what the current refactoring tool does).

2 comments:

Nicoya said...

Prefixes are just the first step down the slippery slope to hungarian notation. Do not fall foolishly into the traps that have claimed so many programmers before you. ;)

Oddly I've found that the more programming I do, the less I gravitate to any particular style.

CocoaGeek said...

Well, I try to keep things as simple as possible, but I do appreciate what the prefixing bring in the balance.