Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is your preferred coding style for dealloc in Objective-C?
    text
    copied!<p>I know that discussions about coding styles tend to end in disaster and endless flame wars, but that’s not what I want to reach. During the last decade I mainly saw two different coding styles for <code>dealloc</code> methods in Objective-C. The first and most common one was to place <code>dealloc</code> at the bottom of the file. This is also the style Apple uses in the Xcode default templates. The logic behind this seems to be that <code>dealloc</code> is invoked when the end of the object approaches, so the end of the file seems to be a nice metaphor.</p> <p>On the other hand a couple of people tend to put <code>dealloc</code> directly below the <code>@synthesize</code> directives. This has two major disadvantages in my opinion:</p> <ol> <li>The top of the file gets cluttered with <em>boring code</em>.</li> <li>It’s harder to find the essential parts in your class, you have to scroll down.</li> </ol> <p>The huge advantage in my opinion is that you have a direct visual connection between properties and the corresponding <code>release</code> message.</p> <p>The other thing is niling already released variables. While I don’t think that this is necessary, especially in object context where the whole variable gets desctructed after <code>dealloc</code> ends, I tend to also nil the variables. I’m used to do this for variables in function scope, so I’m just consistent with my coding style.</p> <p>This is how most of my classes look like:</p> <pre><code>@implementation Bar @synthesize foo; - (void)dealloc { [foo release], foo = nil; [super dealloc]; } // Initializers and other methods… </code></pre> <p>I already mentioned a couple of pros and cons. What do you think about this topic? What is the coding style you use in <code>dealloc</code> and <em>why</em>? Are there other pros and cons I forgot to mention?</p> <p>I don’t want to start a flame war here. I just want to know what style you use and if you have specific reasons for this or if this doesn’t matter for you in the end.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload