TweetFollow Us on Twitter

January 94 - A Case Against DoCommand

A Case Against DoCommand

Andy Dent

I really want to eliminate DoCommand. This article is a slightly theoretical discussion of the benefits and reasons for removing DoCommand and replacing it with a better mechanism. These theories have yet to be tested in a real program, more for lack of time (as yet) than intention. Responses will be welcomed!

Why Change?

Being one of the fortunate (few?) with a dual monitor Mac, I've come to appreciate the strengths of the Think class browser. With most of your class structure visible, the browser's features are usable. One favorite is the option-double-click on a method name to highlight all the implementors of that method.

Except for DoCommand, which is almost an invisible class hierarchy in itself! DoCommand is messy, and you have to look at each and every DoCommand method directly to see where commands are handled.

DoCommand is also inefficient-a command has to fall through a case statement in each level below the method that finally handles the command. So why not dispense with the obscure and inefficient, and have a method for each command-DoCmdNew, DoCmdOpen, DoCmdClose…?

How Much needs Changing?

Obviously, each implementor of DoCommand in the current hierarchy will have to implement a few DoCmdXX methods. Unfortunately, you can't avoid modifications and just subclass the TCL. At the very least, a stub method must be added to the abstract class CBureaucrat for each of your DoCmdXX methods. (The OODL people are probably looking smug at this point-we C++ and Object Pascal die-hards just have to put up with adding methods to base classes, if we want polymorphic dispatching).

Bear in mind that CBureaucrat is going to have to define these stubs for each and every DoCmdXX method you implement. This implies modifying CBureaucrat for each project, although a fair amount of commonality will reduce the number of changes each time.

Dispatch Blues

Adding DoCmdXX methods is fine for handling the commands, and there are many places where a call to DoCommand is performed, with an explicit command number constant. These can be easily changed e.g.: CApplication::DoOpenOrPrintDocEvent changes from sending:
gGopher->DoCommand( cmdPrint) 

to

gGopher->DoCmdPrint().

There are two general dispatchers in TCL which present more of a problem. CSwitchboard::DoKeyEvent and CDesktop::DispatchClick both make use of the following code to dispatch commands based on a menu choice (either by command-key or mouse action).

gGopher->DoCommand(gBartender->FindCmdNumber)

A reasonably elegant way to cope with this, the only truly general use of DoCommand, would be to have your own table object (or plain function), changing slightly for each project. This may sound like an overhead, but it would contain only a single case statement, as opposed to the cases in every single DoCommand used at present. A sample dispatch table function is shown below, along with the code to call it.

myDoCommandDispatch(gBartender->FindCmdNumber)

myDoCommand( long   theCommand)
{
Str255      theDA;          /* Name of Desk Accessory to open   */
SFReply     macSFReply;     /* Standard File reply record */

if (theCommand < 0) {
    if (HiShort(-theCommand) == MENUapple) {
        GetItem(GetMHandle(
            MENUapple), LoShort(-theCommand), theDA);
        OpenDeskAcc(theDA);
    }
} else {            
    switch (theCommand) {       
        case cmdNew:
            gGopher->DoCmdNew();
                break;          
        case cmdOpen:
            gGopher->DoCmdOpen();
            break;

Wrap-up

So, there's the theory. We get faster command handling and a great improvement in browsing at the cost of having to modify some TCL classes for each project.
 
AAPL
$463.97
Apple Inc.
+4.29
MSFT
$30.20
Microsoft Corpora
-0.04
GOOG
$609.09
Google Inc.
+12.76
MacTech Search:
Community Search:

Reckless Racing 2 Review
Reckless Racing 2 Review By Greg Dawson on February 3rd, 2012 Our Rating: :: RUBBIN' AND RACIN'iPhone App - Designed for the iPhone, compatible with the iPad The original Reckless Racing game set the bar for down and dirty iOS... | Read more »
Five For Friday: Week of February 3
Another week has left us behind along with the first month of the year. As always with the arrival of Friday, we take a few moments to round up five of the most interesting apps and games that we’ve yet to cover in a more extensive form. There will... | Read more »
GHOST TRICK: Phantom Detective Review
GHOST TRICK: Phantom Detective Review By Dan Lee on February 3rd, 2012 Our Rating: :: TRICKYUniversal App - Designed for iPhone and iPad Use “Ghost Tricks” to possess objects and solve a murder.   | Read more »
Launch Center Launches New Third Party A...
Launch Center has gotten a major new update that brings new automatic app detection. While the app launched with support for built-in notifications, now the app supports launching third-party apps with specific commands, that can be scheduled to... | Read more »
Spy Mouse Feels the Love With New Valent...
EA and Firemint’s Spy Mouse has an update out now that’s designed to be more appropriate for this time of year, with Valentine’s Day coming up. Love is in the air, and while the cats in Agent Squeek’s life are still out to keep him from getting his... | Read more »
Panorama 360 Camera Review
Panorama 360 Camera Review By Jennifer Allen on February 2nd, 2012 Our Rating: :: CREATIVEUniversal App - Designed for iPhone and iPad Creating a panoramic image just got a whole lot simpler.   | Read more »
Gravity Lander Review
Gravity Lander Review By Rob Rich on February 2nd, 2012 Our Rating: :: SHORT FLIGHTiPhone App - Designed for the iPhone, compatible with the iPad Get three cosmonauts to land on the surface of Mars safely. It’s significantly harder... | Read more »

Price Scanner via MacPrices.net

Open-box special: 13″ MacBook Air for $230 off MSR...
MacMall has open-box return 13″ 128GB MacBook Airs available for $1069.21 including free FedEx overnight shipping. That’s $230 off the cost of new models. Apple’s one-year warranty and all materials... Read more
Apple now offering refurbished Oct ’11 13″ MacBook...
 The Apple Store is now offering Apple Certified Refurbished October 2011 13″ MacBook Pros for up to $230 off the cost of new models, including free shipping. Apple’s one-year warranty is standard... Read more
MacBook Airs on sale for up to $101 off MSRP
B&H Photo has 11-inch and 13-inch MacBook Airs on sale for up to $101 (6%) off MSRP including free shipping plus NY sales tax only: - 11″ 64GB MacBook Air (MSRP $999): $939 - 11″ 128GB MacBook... Read more
Open-box special: 17″ 2.4GHz MacBook Pro for $487...
MacMall has a limited number of open-box return 2011 17″ 2.4GHz MacBook Pros in stock for $2012.26 including free shipping. That’s $487 off MSRP, and Apple’s one-year warranty remains intact. Read more
27″ iMacs on sale for up to $130 off MSRP
  Apple resellers have 27″ iMacs on sale for up to $130 off MSRP. The following is a roundup of the lowest sale prices we’ve seen from Apple Authorized Internet/Catalog Resellers that are available... Read more
Updated MacBook Price Trackers
We’ve updated our MacBook Price Trackers with the latest information on prices, bundles, and availability from Apple’s authorized internet/catalog resellers: 17″ MacBook Pro 15″ MacBook Pro 13″... Read more
Refurbished Apple iPad 2s available for $100 off n...
 The Apple Store has Apple Certified Refurbished iPad 2s available for up to $100 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free (for the most part, Apple... Read more
Apple offers refurbished MacBook Airs for up to $2...
The Apple Store is now offering Apple Certified Refurbished 2011 MacBook AIrs for up to $250 off the cost of new models. An Apple one-year warranty is included with each model, and shipping is free... Read more

Jobs Board

*Apple* Solutions Consultant-Retail Sal...
Job Title: Apple Solutions Consultant-Retail Sales Profession: Sales -> Sales Representative/Business Development Requisition Number 7119264Job title Apple Read more
Desktop Support (Mac OS/Apple) | HelpDes...
for Apple device user support Technical Qualifications: 1. Mac/VIP Technician 2. Mac OS expert - please include any Mac ... to Soloman@Vdartinc.Com for for instant application and immediate response... Read more
iPhone / Android Developer at Onward Tec...
iPhone / Android Developer Responsibilities: Work with the team to understand and translate these into technical requirements Work with lead architect to develop application design strategy... Read more
Mac Developer at Symantec (Mountain View...
Mac developers who will help us build high quality Mac OS X products. Our Mac products need to be world class ... communication and security framework Be familiar with Apple Mac user experience... Read more
*Apple* Sales Manager - Retail Sales -...
Job Title: Apple Sales Manager - Retail Sales Profession: Sales -> Sales Management Requisition Number 12358447Job title Apple Sales Manager - Retail Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.