Skip navigation.
 
mlre: split up alloc and init?
FROM : Ben Trumbull
DATE : Mon Feb 11 23:06:12 2008

>Basically, is it safe to split up alloc and init?


It's a really bad idea.

It's a very error prone pattern.  Having a partially initialized
object around is just begging for trouble.  It's pretty rare for
classes to accept any message besides the initializer until after
it's completely initialized.

I've seen some very difficult to track bugs from this pattern.  The
results of +alloc got assigned into some local, and someone else came
through the code later, didn't realize the original author never
initialized that variable properly N lines earlier, and great wrongs
befell.

In my experience, after about 6 months, "someone else" could just as
easily be future me for all intents and purposes.

It also just seems bizarre.  Why not say what you really mean, and
pass the *Class* around.  When you want a real instance, send the
class the +alloc message and Do The Right Thing.  Classes are almost
first class objects.  Objective-C is pretty handy that way.
--

-Ben

Related mailsAuthorDate
mlsplit up alloc and init? John Terranova Feb 11, 09:04
mlRe: split up alloc and init? Kyle Sluder Feb 11, 09:28
mlRe: split up alloc and init? Scott Andrew Feb 11, 09:46
mlRe: split up alloc and init? Quincey Morris Feb 11, 09:52
mlRe: split up alloc and init? Kyle Sluder Feb 11, 15:32
mlRe: split up alloc and init? John Terranova Feb 11, 17:14
mlRe: split up alloc and init? John Terranova Feb 11, 17:25
mlRe: split up alloc and init? Scott Andrew Feb 11, 17:36
mlRe: split up alloc and init? Alastair Houghton Feb 11, 18:50
mlRe: split up alloc and init? Bill Bumgarner Feb 11, 19:06
mlre: split up alloc and init? Ben Trumbull Feb 11, 23:06