C Class Prefixes

What's your preference with naming ObjC classes? I'm a little uncertain what would be the most reasonable approach on this so it would be nice to hear some other opinions.

Apple recommends prefixing cocoa classes, because ObjC doesn't support namespaces. The Google ObjC styleguide (which i mostly aim for) does away with them, unless you're extending (category, class extension etc.) a NSClass.

My preference would be NOT to prefix classes, because i also think it's a waste of letters and doesn't contribute to a cause. It should only be used in framework code to signal that this class belongs to it and not to your app's classes, but i wouldn't use it on application level.

What's yours, and most importantly WHY?


My Conclusion (please feel free to add your comments to produce the most informed decision)


Application Level Classes:

  • I decided to go with 1 Letter Prefixes (like CMyClass). The main reasons are for file organization purposes (eg better grouping in Finder), and it still uses less class name letters than prefixes with length 2 or more.
  • Use the prefix ' C ' for cocoa classes (eg CAudioController.h )
  • Use the prefix ' U ' for utility collections (plain C, eg USystemAudio.h )
  • Framework Level Classes:

  • Prefix classes with 2 or more custom letters, preferrably unique, since it will probably be shared with other apps.
  • Categories

  • Categories are named as follows: NSClassName+ExtensionPurpose

  • My general approach is to prefix class names that are part of a framework or loadable bundle ie classes that might be shared amongst several applications and with other frameworks, but not to bother with classes that appear as part of a standalone application.

    If Steve Jobs granted me one wish it would be to have name spaces in Objective-C 3.0 (which would be available tomorrow).


    There is a very good guidline by Scott Stevenson on how objective-C code should look like. Checkout the following links.

    http://cocoadevcentral.com/articles/000082.php
    http://cocoadevcentral.com/articles/000083.php

    These links will also answer your question how you should name your classes and why.


    I use a prefix, even in application code that won't be shared -- mostly for consistently. I generally use a 2-letter abbreviation for the app or framework name in which the code originated, unless a different prefix (eg, 3 letters, or a short descriptive word) makes more sense.

    链接地址: http://www.djcxy.com/p/88760.html

    上一篇: 浏览器呈现严格/过渡DOCTYPE之间的区别

    下一篇: C类前缀