Reserved method prefixes in Cocoa. Is there a reference list?

  • I'm aware that certain prefixes like "set" are bad news to use to start method names for obvious reasons, but with the caffeine levels in my brain cell running low, I'm at a loss to recall some of the others (is return verboten?).  Is there a list of reserved method prefix strings that you simply shouldn't use to start your methods?

    Just want to have a little list handy of "don't start methods with these words".

    My sleep deprivation thanks you.
  • On May 29, 2013, at 10:37 AM, Alex Zavatone <zav...> wrote:

    > I'm aware that certain prefixes like "set" are bad news to use to start method names for obvious reasons, but with the caffeine levels in my brain cell running low, I'm at a loss to recall some of the others (is return verboten?).  Is there a list of reserved method prefix strings that you simply shouldn't use to start your methods?

    There aren’t any forbidden prefixes that I know of, but there are ones that should be used only for particular purposes, because the compiler and/or runtime will make assumptions about them when they see that prefix.

    It’s fine to have a method -setFoo:, it’s just that it will be interpreted as a setter for a property “foo”. Which is usually what you meant anyway.

    ARC makes assumptions about special refcounting behavior of methods named “copy” or “mutableCopy” or that start with “init” or “alloc” or “new” — all of these are assumed to return an object reference that needs to be released by the caller. There might be others but those are all I can remember; the ARC docs may have a complete list.

    —Jens
  • On Wed, May 29, 2013, at 11:28 AM, Jens Alfke wrote:
    >
    > There aren’t any forbidden prefixes that I know of, but there are ones
    > that should be used only for particular purposes, because the compiler
    > and/or runtime will make assumptions about them when they see that
    > prefix.

    Apple selfishly reserves the underscore prefix for its own private
    methods, and even goes so far as to suggest prefixing your own methods
    with ugly unique prefixes:
    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Codi
    ngGuidelines/Articles/NamingMethods.html


    Because clearly we don't need namespaces in a modern object oriented
    programming language.

    --Kyle Sluder
  • Now that my brain cell's caffeine levels are normalizing, I seem to recall somewhere in the Cocoa docs stating that starting a method name with a string like "return" is somewhat frowned upon.

    Yeah, it must have been in the ARC docs.  I remember that using init or new are not good ideas.

    That pointed me in the right direction:

    Here's part of what I was concerned about from:
    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Memo
    ryMgmt/MemoryMgmt.pdf


    Basic Memory Management Rules
    The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. If an object has no owners, the runtime system destroys it automatically. To make sure it is clear when you own an object and when you do not, Cocoa sets the following policy:

    •    You own any object you create
    You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

    I'll just try to note any rules or recommendations that I come across.  Thanks.

    On May 29, 2013, at 2:28 PM, Jens Alfke wrote:

    >
    > On May 29, 2013, at 10:37 AM, Alex Zavatone <zav...> wrote:
    >
    >> I'm aware that certain prefixes like "set" are bad news to use to start method names for obvious reasons, but with the caffeine levels in my brain cell running low, I'm at a loss to recall some of the others (is return verboten?).  Is there a list of reserved method prefix strings that you simply shouldn't use to start your methods?
    >
    > There aren’t any forbidden prefixes that I know of, but there are ones that should be used only for particular purposes, because the compiler and/or runtime will make assumptions about them when they see that prefix.
    >
    > It’s fine to have a method -setFoo:, it’s just that it will be interpreted as a setter for a property “foo”. Which is usually what you meant anyway.
    >
    > ARC makes assumptions about special refcounting behavior of methods named “copy” or “mutableCopy” or that start with “init” or “alloc” or “new” — all of these are assumed to return an object reference that needs to be released by the caller. There might be others but those are all I can remember; the ARC docs may have a complete list.
    >
    > —Jens
  • On May 29, 2013, at 11:46 AM, Kyle Sluder <kyle...> wrote:

    > Because clearly we don't need namespaces in a modern object oriented
    > programming language.

    Oh boy. First off, to pre-empt any lengthy threads about this, people should go and read last year’s discussion over on the objc-language list, which started with a very detailed proposal for how to add namespaces.

    Second, adding namespaces is definitely nontrivial, mostly for compatibility reasons. Any naive way of adding namespaces would break many of the dynamic features of Objective-C and consequently things like KVC/KVO and target-action.

    Anyway, people, don’t reply here. If you read that earlier thread and still think you have more to add, go do it on the objc-language list, not here.

    —Jens
  • On May 29, 2013, at 2:46 PM, Kyle Sluder wrote:

    > On Wed, May 29, 2013, at 11:28 AM, Jens Alfke wrote:
    >>
    >> There aren’t any forbidden prefixes that I know of, but there are ones
    >> that should be used only for particular purposes, because the compiler
    >> and/or runtime will make assumptions about them when they see that
    >> prefix.
    >
    > Apple selfishly reserves the underscore prefix for its own private
    > methods, and even goes so far as to suggest prefixing your own methods
    > with ugly unique prefixes:
    > http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Codi
    ngGuidelines/Articles/NamingMethods.html


    Be that as it may, this is exactly the document I was looking for - and also a little eye opening.  It looks like there are many more conditions to pay attention to when naming methods.  Thank you Kyle.
  • Clang objected to a variable that started with "new" when we turned it into a property, because then there was a method named "new" that wasn't doing what it expected. I don't remember the details; I renamed it a long time ago.

    On May 29, 2013, at 10:37 AM, Alex Zavatone wrote:

    > I'm aware that certain prefixes like "set" are bad news to use to start method names for obvious reasons, but with the caffeine levels in my brain cell running low, I'm at a loss to recall some of the others (is return verboten?).  Is there a list of reserved method prefix strings that you simply shouldn't use to start your methods?
    >
    > Just want to have a little list handy of "don't start methods with these words".
    >
    > My sleep deprivation thanks you.
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today