"Tricks" of the "Trade"

  • I have a few general suggestions for Cocoa, OSX and programming in
    general that I hope prove useful to some people, hopefully everyone has
    a rich amount of suggestions to newbies, and veterans alike:

    1.    For NSArrays, NSDictionarys and their Mutable subclasses, use
    [[ClassName alloc] init] as opposed to [ClassName arrayWithCapacity:4]
    and their ilk, this tip has proved invaluable to my programs as it is
    the only way to allocate them without a hitch.
    2.    Don't try to make things optimized on the first try!  Check out my
    single line of code that resulted from this:
    [[[themeElementPieces objectAtIndex:j] lastPathComponent]  stringByDeletingPathExtension]
    ,@"DName",
    [DSBool bool:NO],@"isExpandable",
    [NSArray arrayWithArray:contents],@"contents",
    [NSString stringWithString:[[[[[[NSBundle mainBundle] bundlePath]  stringByDeletingLastPathComponent]
      stringByAppendingString:@"/Theme     Elements/"]
    stringByAppendingString:[prospectiveThemes objectAtIndex:i]]     stringByAppendingPathComponent:
    [themeElementPieces objectAtIndex:j]]],@"path",
    [NSString stringWithContentsOfFile:[[[[[[NSBundle mainBundle]
    bundlePath]     stringByDeletingLastPathComponent]
    stringByAppendingString:@"/Theme     Elements/"]
    stringByAppendingString:[prospectiveThemes objectAtIndex:i]]     stringByAppendingPathComponent:
    @"path.txt"]],@"prospecivePath",
    nil] forKey:[NSString stringWithString:[[[[[[NSBundle mainBundle]
    bundlePath]     stringByDeletingLastPathComponent]
    stringByAppendingString:@"/Theme     Elements/"]
    stringByAppendingString:[prospectiveThemes objectAtIndex:i]]     stringByAppendingPathComponent:
    [themeElementPieces objectAtIndex:j]]]];
    YES! this is one line of code, technically.  This line took HOURS to
    debug.  Blech.

    3.    Always start with small things.  For something using (in end form)
    only a window and a tableview, don't implement anything, just the Window
    and TableView in the Nib file, see if it compiles and runs, and go from
    there.  Always small steps, it is much easier in the end.

    4.    Press shift while clicking the minimize button to get a slow motion
    version (like Steve does in the keynotes.  Press option while clicking
    the minimize button to minimize all windows in the application (ditto
    for the close buttons and zoom buttons).  Press shift-close to make
    windows dissolve and smear away (just kidding).

    5.    Comment code, always.  You may notice that the aforementioned
    single line of code has no comments, this is one of the reasons it was
    such a pain to debug.

    6.    Use subroutines, even inline ones if necessary, it removes clutter
    from each source code file, and spreads it out.

    7.    Use categories to extend features if possible, only use subclasses
    if necessary.

    8.    Follow the Aqua Interface guidelines to create a good-looking
    interface, it only adds to the user-frinedlyness of your program and OSX
    in general.

    9.    Most important: pay shareware fees!  (not that I have paid for
    OmniWeb yet but...)

    Hope these are helpful, and that more people will give their nuggets of
    wisdom.

    Erik Thorteran
  • --On Wednesday, June 06, 2001 5:21 PM -0700 Erik Thorteran
    <ethorteran...> wrote:

    > 1.    For NSArrays, NSDictionarys and their Mutable subclasses, use
    > [[ClassName alloc] init] as opposed to [ClassName arrayWithCapacity:4]
    > and their ilk, this tip has proved invaluable to my programs as it is the
    > only way to allocate them without a hitch.

    The [ClassName arrayWith] and the related work just as well.  You just have
    to understand the differences bewteen the alloc] init] version an and the
    [ClasName classWith: ] version.  One you own and one you don't unless you
    specifically retain it.

    best
    Chad

    Pengar Enterprises, Inc. and Shire.Net LLC
    Web and Macintosh Consulting -- full service web hosting
    Chad Leigh
    <chad...>            <chad...>
  • > 6.    Use subroutines, even inline ones if necessary, it removes clutter
    > from each source code file, and spreads it out.
    >
    > 7.    Use categories to extend features if possible, only use subclasses
    > if necessary.

    When writing plug-ins (screen savers, docklings, pref panels) avoid using
    categories on 'Kit and Foundation objects. Use static or
    __private_extern__ functions to do the work instead. This helps avoid
    symbol collision errors and erratic runtime behavior.

    > 9.    Most important: pay shareware fees!  (not that I have paid for
    > OmniWeb yet but...)

    I bought OmniWeb last weekend. I figured it was finally time to put my
    money where my mouth is.

    Mike Trent
previous month june 2001 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  
Go to today