Pregunta

I've stumbled into one of two classic programming problems. I'm writing a Cocoa framework which is basically a collection of (mostly) helpful methods in categories of the most-used Foundation classes. My question is how do I name these extra methods in my categories for said classes? Should I go with a prefix or prefix-less naming convention (e.g. - (void)doSomething vs - (void)myDoSomething)?

I became unsure when reading the Cocoa documentation:

Use prefixes when naming classes, protocols, functions, constants, and typedef structures. Do not use prefixes when naming methods; methods exist in a name space created by the class that defines them. Also, don’t use prefixes for naming the fields of a structure

and looking at Mike's code examples in friday q&a series (e.g. method names in MARefCounting have prefixes in building reference count article).

¿Fue útil?

Solución

I don't think the documentation you quoted takes into account categories on classes you don't own. It's just trying to say that if you have defined MyClass you don't also need to name your methods my_doThis, because there isn't anything they can collide with.

In this case, it would probably be safest to use a prefix. If you left off the prefix and Apple ended up adding the same method in a future release, then your category implementation would override Apple's, which may lead to unexpected behaviors.

Even worse, sometimes you might be replacing an internally defined method (they don't always start with a _), which may result in internal inconsistencies within the framework, making problems much more difficult to debug.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top