views:

255

answers:

4

Is one of these two ways to create and initialize an object preferable?

MyClass oClass = [[MyClass alloc] init];
oClass.length = 5;
oClass.text = @"Hello";

or using a class method that contains about the same code but looks like this:

MyClass oClass = [MyClass initWithLength:(int) 5 andText:(NSString *) @"Hello"];

I hate it when I see things done more than one way and I have no idea if one is better than the other, or why!

+1  A: 

If the object is unusable without the length and text, then the second option might be better. If those fields are optional, then the first one is better.

However, I don't think there is absolute truth to this question.

Tuomas Pelkonen
And often a class will provide both alternatives, for convenience (hence the need for a "designated initializer" in the class implementation).
David Gelhar
A: 

If you have a class with many properties it's very unlikely to initialize them all in one single line of code. Both ways work fine for me.

Holli
There's no reason to assume a one-to-one mapping between properties and initial values, especially given that +alloc guarantees that all instance variables (other than isa) are initialized to zero, which for many properties is a reasonable default value. But in cases where a larger number of initial values is actually useful, you might consider implementing an -initWithDictionary: method.
jlehr
+3  A: 

Don't be a hater. :-)

By the way, I'm assuming you meant:

MyClass oClass = [[MyClass alloc] initWithLength:(int) 5 andText:(NSString *) @"Hello"];

The reason for multiple init... methods is to make it more convenient for developers to create properly initialized instances. So, for example, if you find that developers often need to create instances of MyClass with a length and text, you make their life easier by providing an API that allows them to do that in one step. And if you find that developers also frequently need to create instances of MyClass with just a text string, you might also provide an -initWithText: method.

And if the instances created this way are frequently used as temporary objects (i.e., not stored in instance variables or static variables), you might also add a class convenience method like +myClassWithText: that returns an autoreleased instance of MyClass initialized with the provided text string.

As to which one is better: it's always better to fully initialize an object when possible, so if the object needs both values to be properly initialized, use the method that allows you to provide both arguments. And if you don't need to store a reference to the instance you're creating, use the class convenience method so your code doesn't have to deal with memory management.

jlehr
Good point. But it seems that code example should look like MyClass oClass = [[MyClass alloc] initWithLength:5 andText:@"Hello"]; without (int) and (NSString*) before corresponding arguments...
kemiisto
A: 

If an initWithSomething: method is available and you want to provide initial values for those properties, I would always prefer it just because it's simpler. It also will always work even with immutable versions of a class.

But neither init method is inherently "better." Classes usually have one or two designated initializers and all the others just call those with default values — it doesn't necessarily leave the instance's properties untouched. The documentation for a class should indicate what its initializers do and which is the designated initializer for the class. For example, [[NSDate alloc] init] uses NSDate's designated initializer, initWithTimeIntervalSinceReferenceDate:, to create a date object representing the current date and time.

Incidentally, this also means that when you're subclassing a class, you only need to override its designated initializer. Since the others just call that, they get your new behavior for free.

Chuck