Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.
This repository was archived by the owner on May 9, 2023. It is now read-only.

Ivars should be directly accessed in init'ers #6

Closed
@davedelong

Description

@davedelong

Right now the guide simply says to use direct ivar access in dealloc. You should also directly access ivars in init* methods. More generally, you shouldn't really be invoking any method inside init* or dealloc methods. Other methods (including accessors that may be overridden) may expecting the object to be in a fully constructed state. When you're inside one of these methods, that is not the case.

Activity

xissburg

xissburg commented on Aug 2, 2013

@xissburg

More generally, you shouldn't really be invoking any method inside init* or dealloc methods

Care to elaborate?

davedelong

davedelong commented on Aug 2, 2013

@davedelong
Author

If you're trying to be as defensive as possible, then you have to operate under the assumption that someone may have subclassed the class and overridden your public methods. These overrides may be assuming that when the method is invoked, all of the properties and ivars and everything are in a consistent and fully-initialized state.

So, if your initializer or dealloc method causes one of these overridden methods to be executed, then the logic of the method may do something incorrect. The best case scenario is that it works as intended. The worst case scenario is that you do something like dereference a NULL pointer or index beyond the bounds of an array or cause some data to become corrupted, etc.

Whilst you're inside an initializer or dealloc method, your object is in an inconsistent state. Thus, if you're trying to be as defensive as possible, you should avoid invoking methods that might be making assumptions about the state of the object.

mbbischoff

mbbischoff commented on Aug 3, 2013

@mbbischoff
Contributor

Hey @davedelong. Thanks for the great writeup. This is now fixed.

added a commit that references this issue on Jan 20, 2015

Merge pull request nytimes#6 from venmo/chris/add_commit_message_tone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @mbbischoff@davedelong@xissburg

      Issue actions

        Ivars should be directly accessed in init'ers · Issue #6 · nytimes/objective-c-style-guide