tags:

views:

61

answers:

3
- (id)initWithFrame:(CGRect)frame viewController:(LevelViewController *)aController;

What's the small a for? When I see a small k, I'm assuming it's for constant.

+1  A: 

"a" means it is an "argument" when used as Hungarian. Alternatively it can be treated as an English article, and so will be become "anObject" or "someThings" (which would be aObject and aThings in Hungarian).

In either case it is to make it clear that you were passed this object, and so should be careful about modifying it in unexpected ways.

Regarding "k", yes that's for constant.

Rob Napier
I don't think the "aFoo" you see in Apple's API is Hungarian notation as they don't make use of "real" Hungarian anywhere (at least don't know an example). I guess they really mean the article.
DarkDust
Apple definitely means the English article, but not all ObjC coders do, so I wanted to explain both common uses. Apple definitely uses Hungarian more recently when they've started adding "k" to constants (as you note, most older constants just use NS, but newer frameworks seem to generally use "k"). IMO "k" is exactly what Hungarian was meant to be: it expresses the meaning of the variable rather than the type of the variable.
Rob Napier
+2  A: 

It's only convention. Usually, we name our instance variables in a clear/logical fashion:

@interface Foo : NSObject {
  Bar * bar;
  Baz * baz;
}
@end

However, we also like to be verbose with our method names, so that they are also explicit in their purpose:

- (id) initWithBar:(Bar *)bar baz:(Baz *)baz;

The problem with this is that when implementing this method, we run in to naming conflicts:

- (id) initWithBar:(Bar *)bar baz:(Baz *)baz {
  if (self = [super init]) {
    bar = [bar retain];
    baz = [baz retain];
  }
  return self;
}

The problem here is that the parameter "bar" hides the instance variable named "bar", leading to a conflict and undesired behavior. To rectify this, we tack on an article (when necessary) to the parameter to indicate that it can be any Bar, or any Baz:

- (id) initWithBar:(Bar *)aBar baz:(Baz *)aBaz {
  if (self = [super init]) {
    bar = [aBar retain];
    baz = [aBaz retain];
  }
  return self;
}
Dave DeLong
That makes sense. So in the following example the "a" just makes it easier to identify this particular instance variable right?
Andy Elliott
Person *aPerson = [[Person alloc] initWithAge: 53];aPerson.name = @"Steve"; // NOTE: dot notation, uses synthesized setter, equivalent to [aPerson setName: @"Steve"];NSLog(@"Access by message (%@), dot notation(%@), property name(%@) and direct instance variable access (%@)", [aPerson name], aPerson.name, [aPerson valueForKey:@"name"], aPerson->name);
Andy Elliott
+1  A: 

By convention, variables (and arguments to a methods are also variables) start with a lowercase letter. In this case, the "aController" argument could also be named just "controller" or "fluffyBunny". The author chose "aController" most likely because of aesthetic reasons.

The Apple convention to start constant variable like kCFStreamStatusAtEnd is also just that, a convention. They could have chosen any other character instead or none at all (e.g. the constants for Cocoa start with NS without a lowercase character, as in NSStreamStatusAtEnd).

Then there's the so-called Hungarian Notation. It's a convention that encodes meta-data into the variable, like its type. It's use is quite controverse, there are lots of people who like and use it and others who hate it. See the Wikipedia article for arguments for both sides.

DarkDust
Good examples. I would remind the reader that in ObjC, convention is extremely important. The compiler gives a lot less protection against foolishness than in other languages. To write high-quality ObjC, you need to follow good naming conventions, even more than in other languages. Several naming conventions (memory management and KVC/KVO) are mandated by the frameworks, even though the compiler doesn't enforce them. So learning and following the conventions that Apple uses is a key part of learning the language.
Rob Napier