3

I'm a bit confused with Apple documentation relating to the explanation of whether to use prefix for methods or not?

Apple Doc Explanation 1:

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

Apple Doc Explanation 2:

If you are subclassing a large Cocoa framework class (such as NSView or UIView) and you want to be absolutely sure that your private methods have names different from those in the superclass, you can add your own prefix to your private methods. The prefix should be as unique as possible, perhaps one based on your company or project and of the form "XX_". So if your project is called Byte Flogger, the prefix might be BF_addObject:

Anil
  • 2,430
  • 3
  • 37
  • 55
user40910
  • 700
  • 1
  • 6
  • 17
  • 1
    Prefixes are never necessary, they're always optional. But because there's no name-spacing in Objective-C, it's a good idea to prefix anything that will share scope with code you do not control. Eg class names are global scope, and subclasses of a class built into iOS would share scope with the parent class, so it's a good idea to use a prefix incase the parent class changes to have a method with the same name as one your subclass added. – Abhi Beckert Nov 03 '14 at 10:23

3 Answers3

1

For Classes which contains project related storylines/stuffs, there prefixes are not required.

But if we are using Apple Classes by extending few methods as given in example, like UIView to MBView then we should add prefix to methods to private methods in private category ( in .m file).

Mrunal
  • 13,982
  • 6
  • 52
  • 96
1

This is because ObjC does not support namespaces. Instead you should you capital letter prefixes(as you properly read in documentation). You could read this SO discussion why to use them.

Note that Apple uses two-letter prefixes(UI(View), NS(String), etc.) and advises programmers to use 3 letter prefixes.

Community
  • 1
  • 1
hris.to
  • 6,235
  • 3
  • 46
  • 55
0

I think you should use a prefix when you can, it can become a good practice and you can identify, by the name which part of your big software you're playing with.

Let's say your program name is Byte Flogger, then all your classes should start with :

BF prefix. BFBaseList for exemple, and if you want to prevent rejections when submitting your app to the AppStore, it's also a good practice to name your methods bfMyMethodName so that you still respect CamLCase naming conventions.

So for an image, you could name a property bfContentMode without being suspected by Apple to use one private API feature.

Now, let's say you handle some modules, a core module, a network module, etc... If your class name is BFCObject, you could know that you're working with a Core object of your program.

So it is not necessary, but not doing it could force you to refactor your code in the last moment of submission. In a time driven project, I wouldn't even take that risk.

Vaseltior
  • 1,226
  • 15
  • 27