Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] Commenting source code



On Wednesday, August 27, 2003, at 02:09 AM, Jan E. Schotsman wrote:
I can think of one justification: if you just want to quickly revisit the
function you can just read the red type and think "Oh yeah, I remember
now". Kind of leaving in the original first version pseudocode.

This is one of the best immediate benefits of using Objective-C or Smalltalk: The keyword-style argument syntax in message expressions *really does* create self-documenting code.

For example, say you want to write code to create a menu in Cocoa.

You'd write something like this (off the top of my head):

NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];

NSMenuItem *itemForMyMenu = [mainMenu addItemWithTitle:@"My Menu"
action:nil
keyEquivalent:@""];

NSMenu *myMenu = [[[NSMenu alloc] initWithTitle:@"My Menu"] autorelease];

NSMenuItem *myItem = [myMenu addItemWithTitle:@"My Item"
action:@selector(doSomething:)
keyEquivalent:@""];

[itemForMyMenu setSubmenu:myMenu];

This gets the main menu (the menu bar), adds a new item to it, creates a new menu named "My Menu", adds an item to it named "My Item", and adds the new menu to the menu bar. The only tricky bit here is that to add a menu to the menu bar you have to create both an item (representing the "slot" in the menu bar) and a menu (representing the menu itself); you don't need comments to do things like remember what the various parameters mean.

-- Chris

--
Chris Hanson, bDistributed.com, Inc. | Email: email@hidden
Custom Mac OS X Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email: email@hidden
_______________________________________________
carbon-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/carbon-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: [OT] Commenting source code (From: "Jan E. Schotsman" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.