Why is it not reccomended to allocate and initalize with id?

The main problem with the code in your example is that in some cases, the -init method returns an object that's different from the one that you created with +alloc. If that happened, then your code would be incorrect because you're not assigning the result of c init to c, and you'd end up working with the wrong object (and one that's not properly initialized at that). This is why the standard idiom is to always combine +alloc and -init on the same line.

The main problem with the code in your example is that in some cases, the -init method returns an object that's different from the one that you created with +alloc. If that happened, then your code would be incorrect because you're not assigning the result of c init to c, and you'd end up working with the wrong object (and one that's not properly initialized at that). This is why the standard idiom is to always combine +alloc and -init on the same line: id c = Person alloc init; Now, you may have written the Person class and have first hand knowledge that -init always returns the same object, but I shouldn't have to be intimately familiar with the inner workings of Person to read your code and have a sense of whether it's correct or not.

Your code is "better" in the sense that anyone can tell that it's doing the right thing if you follow the usual convention. I don't think it's horrible to declare c as type id, but it does seem silly in this case. You know that c will be of type Person*, so why declare it as id and throw away useful information that the compiler can use to help you write better code?

If there's a good reason to use id, that's fine, but if you can be more specific about the type you should do so.

1 +1 for spotting the potential -init bug :) – Eiko Jul 16 at 17:45.

When using the id generic type, you won't get a warning if you try to call a method that doesn't exist. The compiler assumes you know what you're doing. Otherwise, no issues that I can think of.It's common in several situations (think fast enumeration on heterogeneous container type).

1 Succinct, correct answer. – Caleb Jul 16 at 17:47.

Another potential danger is that over time, you may add logic that accidentally only does init for certain cases... leaving an object that is allocated but never initialized means all values in it are essentially random and any setup code it was meant to do is not done.

First of all, type casting problem. Every time you want to use 'c', you may need to (Person *), i.e. Typecast it.

Secondly, if you have any method declared in Person, you can not call it as c aMthod style, you have to use, (Person *) c aMethod.

1 You don't need the second set of brackets ((Person*)c method) – indragie Jul 16 at 17:37 2 I though you almost never have to typecast anything of type id. – Eiko Jul 16 at 18:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions