5

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 myClassWithLength:(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!

FreeAsInBeer
  • 12,837
  • 5
  • 46
  • 79
Scott Pendleton
  • 973
  • 3
  • 13
  • 32
  • do you mean [MyClass myClassWithLength:... ? – Ross Mar 10 '10 at 15:11
  • 2
    There's never a class method that will be called `initWith...` — init methods are always instance methods. It will always be `[[MyClass alloc] initWith...]`. Classes sometimes offer convenience constructors like Ross is asking about, though. Just to be clear. – Chuck Mar 10 '10 at 16:03

4 Answers4

9

Don't be a hater. :-)

By the way, I'm assuming you meant:

(Edit: removed unnecessary casts)

MyClass oClass = [[MyClass alloc] initWithLength:5 andText:@"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
  • 15,215
  • 5
  • 40
  • 45
  • 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... – Wildcat Mar 10 '10 at 22:09
2

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
  • 7,559
  • 2
  • 28
  • 31
  • 1
    And often a class will provide both alternatives, for convenience (hence the need for a "designated initializer" in the class implementation). – David Gelhar Mar 10 '10 at 15:18
0

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.

TalkingCode
  • 12,759
  • 27
  • 96
  • 141
  • 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 Mar 10 '10 at 16:17
0

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
  • 222,660
  • 29
  • 289
  • 383