TweetFollow Us on Twitter

March 96 - THE VETERAN NEOPHYTE: Killing Time Killers

THE VETERAN NEOPHYTE: Killing Time Killers

Bo3b Johnson

So I'm sitting at my desk, mouse in hand, digging through the guts of my Mac, trying to track down yet another pathetic bug. The only trouble is, this is getting dull. I've done it a thousand times, and I always win; it's just a question of how much time the old ball and chain is going to eat up this time. Worse, the wind is blowing 20 knots right outside my window, and for the windsurfers in the crowd, you know the exquisite torture of good wind that you aren't allowed to transform into mind-blowing speed.

Maybe for you it's that you'd like to get home to see your insanely great mate. Or you've got kids whose names you can't pronounce. It's even likely that some of you just graduated from college and are still astonished that they pay you for something that's so much fun. But you're thinking, "If I can get this bug fixed quickly, I can get back to writing that rad Marathon hack to make the other net players slower than me." Perhaps your boss has started to notice that you spend a lot of time on the job but you don't really get very much done. Getting a little nervous? What if he's thinking of pulling the plug on your baby because you're too slow?

Or maybe you shipped the 1.0 version, but it had a few too many bugs, and MacWEEK was so incensed that they broke with the tradition of objective journalism and are calling for your head. People who bought your software with actual money have been calling every day, filling your answering machine with unveiled threats. It seems that you left a bug in there that just cost several thousand people each about two weeks of valuable time, reentering their data.

In particular, recognize that the wasted time from programming errors and bugs gets exponentially more expensive the further into the process you get. If I make a syntax error, I just fix it and recompile. If I toss buggy code over the fence to the testers, now I'm wasting their time. If I ship software with occasional crashing bugs that I just can't quite track down, I'm wasting thousands of people's time.

The point is, there are lots of ways to waste time while programming. I'm here today to offer some ideas on how to save time through better programming habits, so that you can take up windsurfing, or maybe the electric guitar, or learn how to pronounce your kids' names, or gain "the power to crush the other kids" while playing Marathon. Whenever I mention windsurfing, substitute your favorite quality-of-life enhancer.

I'll use some real life examples, and we'll see what sorts of lessons we can learn from them. I've categorized these ideas in three ways. First, there are some obvious time wasters that can be eradicated; these aren't really bug related, just daily time wasters. Considering how much time bugs cost, the second category consists of high-value rules that can find bugs quickly and painlessly. Finally, there are the super-value rules that prevent bugs from happening at all. Be sure to consider how these ideas might apply in your specific circumstances.

RIGOROUS, YET REUSABLE

OK, I'm in the midst of writing the MMU tables for Blat, and I realize that I've already got a similar table. Not being a fool, I know not to rewrite code I've already written, so I open that file and casually copy and paste the table into my current work. Oh no, not another copy and paste casualty! Apparently I missed changing those two table entries, and with MMU tables that means the machine hangs before MacsBug loads. Seems like every time I paste in code there's something I miss, making it not compile -- or worse, causing a malfunction.

Even with small chunks of code, I've found it helps to review the pasted code line by line, carefully, and not to assume that since it ran before, it'll run now. Some subtle assumptions may have changed, and even though reusing code is certainly superior to writing it again, don't be misled into thinking this is risk free. A more powerful technique is to seriously modularize code, so that when I copy and paste I take an entire routine, not just a few lines. With a well-defined interface, the chances of blowing it are greatly reduced. This means adopting the habit of writing each routine with the idea that I'm going to reuse it later. This radically improves every routine I write.

New rule: Reuse code modules, not code fragments. If the code has to be altered, inspect it as if it were new (which it is).

VERBOSE, YET LUCID

While in the guts of Font/DA Mover, I ran across some very strange code that didn't make any sense to me at all -- and it wasn't documented. It was never executed as far as I could determine, but I painstakingly figured out that it was looking for System file 3.2 and, if found, would patch the OS to fix a font bug. I would have saved a full day of effort had there been a comment in that funny little splat of code. Like I'm supposed to know what the bugs in System 3.2 are off the top of my head?

Comments really are necessary to make code reusable and maintainable. I always write "strategy" comments, which say what the routine is trying to do, and avoid writing "tactical" comments, like what it's doing line by line. Remember, sometimes the time savings occur in the future, not at the moment. I've found that skipping comments is being penny wise and pound foolish. Usually the strategy comments help clarify my thinking on the routine as well, so there actually is a short-term gain.

New rule: Always write strategy comments. It's possible to decipher intent from the code, but why not just explicitly say it?

PLUMP, YET HONED

I used to think it was important to save every line of assembly code that was possible. The first program I wrote for the Mac was Anaclock, an analog clock program, and I remember thinking that if I changed the order of some routines I could save code. Don't we all get into that mode sometimes? If I just change these two lines, I can save an assignment, and blah blah. It must come from the old 128K Macs and Apple IIs.

Well, guess what? These machines are so stuffed full of junk nowadays that saving just one or two lines is as meaningless an effort as trying to decide how many demons fit on the head of a transistor. Worse, I spent my own valuable time deciding something that has zero impact. Sorry, no can do anymore. My philosophy now is: write it straightforward, easy to read, vanilla. I want to save my windsurfing time, not pretend that I know up front what needs optimizing. In the Anaclock example, the computer had an entire second between screen updates. When I actually measured execution time, all the time was spent in CopyBits updating the screen, and waiting in the main event loop for the next second to arrive. There was zero measurable time in my entire clock calculation and offscreen drawing code.

New rule: No premature optimization. Measure with performance tools first. Then optimize only where it counts.

TEMPERAMENTAL, YET DISCRIMINATING

During System 7 development, we once tracked down a bug, taking seven hours in the middle of the night to find it, and it wound up being a bad parameter passed to a ROM routine. Incredibly, I could have found that bug in about 15 seconds if I'd used the Discipline tool. Nowadays, I never debug something by hand unless it has passed all the debugging tools that Fred Huxham and I talked about in our article in develop Issue 8.

There are lots and lots of tools available now, and I use all of them. I don't care how hard they are to use; if they can find a bug in seconds that might take me hours or days, then I win. This includes such notorious tools as Blat and Jasik's debugger. I know Blat's a pain, since it doesn't work on all machines, but hey, it's too valuable to skip. Same with Jasik's debugger. Sure it's confusing, but it's got features no one else provides. Before throwing the software over to the testers, I make sure it passes all the tools.

High-value rule: Use the best tools, all the time. Don't spend time in a debugger when a test tool will hand you the answer on a silver platter.

SPECULATIVE, YET REWARDING

As part of a contract, my job was to make a program to save, print, and display 300 dpi bitmaps that were scanned in from a fax machine through new hardware. This was to be a low-cost scanner, and my software would be the initial scan-and-display code. Nothing too fancy, but it still required basic functionality. I bid 15 hours for the entire program. Was I crazy? Well, of course, but not for this reason. I used MacApp to give me the application functionality, and the FracApp300 sample program was a good starting point for 300 dpi bitmap handling. All I really did was add an object to talk to the scanning hardware, and I came in under bid!

Sometimes learning those new tough coding tools can really pay off. I generally try to sample every new tool and coding advance that comes along to see if it can help me save time. MacApp was clearly a massive win, because it focused my programming onto teensy parts to be added instead of all the Toolbox calls of a typical application. In addition, it was fully debugged and very robust, giving me a more solid final application. I try not to be wedded to any given style or approach; I just want to use the best stuff currently available.

High-value rule: Try new things. New ideas, approaches, tools, and programming styles can be like winning the free-time lottery.

PAVLOVIAN, YET TRAINABLE

Sometimes it takes a while to recognize bad habits for what they are. While writing Bowser, which turned into Mouser and then MacBrowser, I wrote the source code parser by hand, to look for keywords. This was not a good strategy. It was quick and dirty, and stayed dirty, and was less quick all the time. It would be reasonable to expect that after modifying the parser for the eighth or ninth time to handle some stupid language exception, I would have gotten a clue that this was not the right approach. The right answer was to learn how the lex and yacc tools worked, since parsers for both Object Pascal and C++ already existed in that format.

After seeing similar bugs go by several times, it becomes clear that something must be done to stop that kind of bug. I don't want to spend time fixing the same problem over and over again, so now my goal is to permanently fix bugs so that they can't happen again. By this I mean changing how I do things, so that that specific bug will either be caught quickly or never happen again. It can be as simple as adding a test to a test suite to ensure that bugs of that form are caught immediately, or adding an assert to catch that error. Or it can be as hard as changing my programming habits to never use pointer math. Whatever it takes, I try to learn from each bug and make sure it can't happen again. Especially after I've done something twice, it's time to write a tool to fix that problem.

High-value rule: Learn from mistakes. If my dog gets bonked on the nose every time he gets near the door, he learns to avoid the door. I want to be at least as smart as my dog.

FASTIDIOUS, YET NOBLE

Another slant on the Bowser problem is that I wasn't really trying to make the parser right. If I'd been a little more quality conscious, I wouldn't have gone that route, because it was clear that the hand-built parser was clunky. As noted before, the longer a bug survives, the more expensive it will be. Early bug extinction is my goal, so I consciously try to write with quality in mind. Examples are: using the strictest coding rules, not using any tricky features of the compiler, using type-suggestive variable names, insisting on type checking, not using raw pointer variables, avoiding type coercion, adopting a simple easy-to-read style, writing clear module interfaces, and using full warnings in the compiler.

Since I started noticing how much time bugs cost, I've changed my mindset on them. I no longer automatically accept that code will just have bugs. I hate 'em. I want to kill 'em. Better, I want to kill 'em before they hatch. Since they take up my personal time, I feel it's only proper to take it personally when they show up.

Super-value rule: Write with quality in mind. As they say, the inner game of programming is so important.

UGLY, YET EVOLVED

Once upon a time, I was asked to fix a couple of bugs in Font/DA Mover and make it work with TrueType fonts, as an interim solution before System 7. The program was so disgusting to me that I just had to go in and clean it up. Move this here, change these names, document some pieces, take out the redundant code, modularize some pieces -- ah, how aesthetically pleasing. Oops . . . I just introduced a couple of bugs while I was "improving" the code. It felt like progress, but actually it was just motion. You know, like company reorgs.

What to do? Don't "improve" code, unless it's never been debugged. Any fully debugged code, no matter how shoddily written, is superior to newly written code, no matter how pristine. It went against my grain, but the right answer was to leave it gross. That heavily used Font/DA Mover code had thousands of hours of value in it, with literally millions of testers, that were all lost when I rewrote it. Rewriting it took time that I wanted to spend on something more valuable, like fixing the last few remaining bugs -- and then getting outside and windsurfing! Once I rewrote the code, it was like a new program, and thus needed a full development/testing/debugging cycle. I backed off to an earlier "skanky" version and just debugged that.

Super-value rule: Never rewrite something that's been fully tested. It may be ugly, but evolution is on its side.

BORING, YET ELEGANT

We all know about the "cool" things that C can do, and some tricky ways of using it, but sometimes isn't it a bit like juggling live weasels? When I found that using a #define had added an extra unwanted character to each place I used it, it no longer seemed so clever, and felt more like I was playing tricks on myself. Or how about that favorite of putting an actual assignment in an if statement? It's cleverly camouflaged, but there aren't any natural predators here, so I'm not sure this is needed. These simple examples obviously don't do justice to the possible tricks that we've all seen, but they all cost time and rarely add value.

OK, so it's clear that being "clever" often winds up being a way to play tricks on myself. Is there anything wrong with doing it simply, in a straightforward, vanilla style? I know for sure I'll get it done sooner and, even better, the programmer who has to maintain this code won't have to waste a bunch of time understanding mindless tricks (remembering of course that that maintenance programmer might very well be me, two years after I forgot what tricks I was playing). And let's just forget the malarkey about it saving code. Is it really worth saving 10 whole bytes out of a 16 meg machine, at the expense of wasting my time? I want to count cycles and bytes only in places where it makes a measurable difference.

Super-value rule: Write vanilla code. Doing it simply, and the same way each time, also makes it more likely to be correct.

ASSERTIVE, YET FRIENDLY

Back in the deep dark Macintosh past, I wrote the driver for an external RAM disk called DASCH. This high-speed serial link required some different debugging tactics than I'd used previously, because I couldn't step through the code; it was time critical. Any slight perturbation in speed would overrun and cause an error, but I still needed to debug it. It was like a "look Mom, no hands" type of debugging. Code inspection is OK, but I wanted to be sure it worked as I read it. Have you ever read a piece of code that took a branch you didn't expect?

The answer, although I didn't use the name at the time, was to use asserts. These have been talked about a fair amount, and you've probably used primitive asserts under the name of DebugStr. Nowadays, the most powerful combination I've used is to hook together asserts with a failure handler like MacApp's catch/fail mechanism. Asserts make it easy to build a debug-only version that checks every stupid thing that can go wrong and lets me know right up front during testing, but doesn't compile into the final version. The catch/fail stuff makes it easy to handle every possible error in a graceful way. (See the article "Using C++ Exceptions in C" in this issue.)

If something absolutely positively cannot fail, I use a debugging-version assert to catch the occasional times when it does fail, so that I can surprise myself early and not spend hours tracking down the "impossible" error. One great thing to check with asserts is input parameters, to catch those inevitable times when some routine passes in rubbish.

Super-value rule: Use asserts along with a failure handler. Catching bugs as they happen is vastly superior to backtracking 15 miles after the program crashes.

ENDING, YET BEGINNING

I'm not going to pretend that this is all there is to the idea of saving time, but hopefully the idea seems worth pursuing. It has certainly helped me get better at my carving jibes and, not incidentally, better at programming at the same time. Higher-quality code, fewer bugs, earlier ship dates, happier customers, and more free time. Yup, I'd say it's been worth it. If you've got some additional time-saving ideas, I'd naturally be interested in trying them too, so write me at bo3b@rahul.net.

RECOMMENDED READING

  • Writing Solid Code by Steve Maguire (Microsoft Press, 1993).
  • Debugging the Development Process by Steve Maguire (Microsoft Press, 1995).
  • "Macintosh Debugging: A Weird Journey Into the Belly of the Beast" by Bo3b Johnson and Fred Huxham, develop Issue 8, and "Macintosh Debugging: The Belly of the Beast Revisited" by Fred Huxham and Greg Marriott, develop Issue 13.
  • Zen and the Art of Windsurfing by Frank Fox (Amberco Press, 1988).

BO3B JOHNSON (bo3b@rahul.net) is completely whacked out about windsurfing, and takes summers off in order to windsurf every day. But since it's winter, he's doing consulting so that he can pay for his next windsurf board and windsurf trip to Aruba. Bo3b prefers to be addressed as "Bob," since the 3 is silent.*

Where's Dave? That other Johnson, who usually writes this column, is probably at the public library researching his obsession du jour, taking his dogs for very long walks, or reclining on the couch reading a book. Since he's cut back his working hours, we're having guest Neophytes write this column. We can't promise they'll all be Johnsons, however.*

Thanks to Jeff Barbose, Jim Friedlander, Brian Hamlin, Fred Huxham, Dave Johnson, Jim Reekes, and Patty Walters for their terribly helpful review comments.*

 
AAPL
$100.57
Apple Inc.
+0.04
MSFT
$44.95
Microsoft Corpora
-0.38
GOOG
$584.49
Google Inc.
-2.37

MacTech Search:
Community Search:

Software Updates via MacUpdate

Picasa 3.9.138 - Organize, edit, and sha...
Picasa and Picasa Web Albums allows you to organize, edit, and upload your photos to the Web from your computer in quick, simple steps. Arrange your photos into folders and albums and erase their... Read more
Tidy Up 3.0.15.0 - Find duplicate files...
Tidy Up is a complete duplicate finder and disk-tidiness utility. With Tidy Up you can search for duplicate files and packages by the owner application, content, type, creator, extension, time... Read more
Parallels Desktop 10.0 - Run Windows app...
Parallels Desktop is simply the world's bestselling, top-rated, and most trusted solution for running Windows applications on your Mac. With Parallels Desktop for Mac, you can seamlessly run both... Read more
Apple Final Cut Pro X 10.1.3 - Professio...
Apple Final Cut Pro X is a professional video editing solution.Completely redesigned from the ground up, Final Cut Pro adds extraordinary speed, quality, and flexibility to every part of the post-... Read more
Apple Compressor 4.1.3 - Adds power and...
Compressor adds power and flexibility to Final Cut Pro X export. Customize output settings, work faster with distributed encoding, and tap into a comprehensive set of delivery features. Powerful... Read more
Chromium 36.0.1985.143 - Fast and stable...
Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. FreeSMUG-Free OpenSource Mac User Group build is... Read more
Macgo Blu-ray Player 2.10.6.1691 - Blu-r...
Macgo Mac Blu-ray Player can bring you the most unforgettable Blu-ray experience on your Mac. Overview Macgo Mac Blu-ray Player can satisfy just about every need you could possibly have in a Blu-ray... Read more
Apple Motion 5.1.2 - Create and customiz...
Apple Motion is designed for video editors, Motion 5 lets you customize Final Cut Pro titles, transitions, and effects. Or create your own dazzling animations in 2D or 3D space, with real-time... Read more
A Better Finder Rename 9.39 - File, phot...
A Better Finder Rename is the most complete renaming solution available on the market today. That's why, since 1996, tens of thousands of hobbyists, professionals and businesses depend on A Better... Read more
PopChar X 6.6 - Floating window shows av...
PopChar X helps you get the most out of your font collection. With its crystal-clear interface, PopChar X provides a frustration-free way to access any font's special characters. Expanded... Read more

Latest Forum Discussions

See All

Invaders! From Outer Space (Games)
Invaders! From Outer Space 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: | Read more »
Dementia: Book of the Dead (Games)
Dementia: Book of the Dead 1.00 Device: iOS Universal Category: Games Price: $2.99, Version: 1.00 (iTunes) Description: EXCLUSIVE CONTENT ONLY ON THE APP STORE. Medieval England. Times of knights, witches and hunters. What other... | Read more »
Wan Nyan Slash (Games)
Wan Nyan Slash 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Wan Nyan Slash is an infinite adorable demon slaying slashing action game! Play as the wandering samurai Wan and Nyan as they... | Read more »
Fallin Love - The Game of Love (Games)
Fallin Love - The Game of Love 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: GRAVITATE AROUND LOVE | Read more »
Ancient Battle: Hannibal (Games)
Ancient Battle: Hannibal 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: | Read more »
Cubic Castles Review
Cubic Castles Review By Rob Thomas on August 20th, 2014 Our Rating: :: CASTLE CRAFTINGiPad Only App - Designed for the iPad Some ridiculously frustrating camera issues aside, Cubic Castles is a pretty neat, voxel-based crafting... | Read more »
Space Colors – Tips, Tricks, Strategies,...
Hello Cadets: Want to know what we thought about this hectic space combat/roguelike? Check out our Space Colors review! Space Colors is a cool shooter/roguelike from Team Chaos. You travel from planet to planet across a huge galaxy and complete a... | Read more »
Tap Sports Baseball – Tips, Tricks, and...
Tap Sports Baseball is a pretty simple game to learn, but that doesn’t mean it’s an easy game to master, by any means. To start your batting career off well, we thought we’d give you the heads up on some handy tips and tricks. Hey Batter-Batter:... | Read more »
Tap Sports Baseball Review
Tap Sports Baseball Review By Jennifer Allen on August 20th, 2014 Our Rating: :: LET'S PLAY BALLUniversal App - Designed for iPhone and iPad Tap Sports Baseball is briefly fun but lacks some important features.   | Read more »
Earn to Die 2 Set to Drive in to the App...
Earn to Die 2 Set to Drive in to the App Store Later This Year Posted by Ellis Spice on August 20th, 2014 [ permalink ] Not Doppler has announced that Earn to Die 2, a sequel to their successful game | Read more »

Price Scanner via MacPrices.net

Mac Backup Guru 2.0 Drive Backup/Cloneing Uti...
Mac Backup Guru developer MacDaddy has released Mac Backup Guru 2.0, offering new and enhanced advanced features, such as bootable backups, synchronised volumes and folders, and a Snapshot mode that... Read more
Operate GE’s New Free-Standing KItchen Range...
Think you accidentally left the oven on? Switch it off while on the go. The new free-standing Profile™ Series gas and electric ranges are GE’s second cooking appliances, following their wall oven, to... Read more
Apple now offering certified refurbished 2014...
 The Apple Store is now offering Apple Certified Refurbished 2014 MacBook Airs for up to $180 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is... Read more
Best Buy’s College Student Deals: $100 off Ma...
Take an additional $100 off all MacBooks and iMacs, $50 off iPad Airs and iPad minis, at Best Buy Online with their College Students Deals Savings, valid through September 6th. Anyone with a valid .... Read more
MacBook Airs on sale for $100 off MSRP, free...
B&H Photo has three 2014 MacBook Airs on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only. They also include free copies of Parallels Desktop and LoJack for Laptops... Read more
Razer Taipan Mouse For Gamers And Non-Gamers...
If you’re a serious gamer on either Mac or Windows PCs, a serious gaming mouse is a necessity for first-tier performance. However, even if like me you’re not much of a gamer, there’s still a strong... Read more
15-inch 2.2GHz MacBook Pro on sale for $1899,...
Adorama has the new 15″ 2.2GHz Retina MacBook Pro on sale for $1899 including free shipping plus NY & NJ sales tax only. Their price is $100 off MSRP, and it’s the lowest price available for this... Read more
Mid-Size Tablet Shootout Posted: iPad mini wi...
I ‘m curious about how many iPads Apple is actually selling these days. It’s been widely rumored and anticipated that new models with A8 SoCs, 2 GB of RAM, 8 megapixel cameras, and fingerprint... Read more
The 15 Biggest iPad Air Problems And How To A...
What’s this? Fifteen “biggest” problems with the iPad Air? Does that mean there are a lot of smaller problems as well? Say it isn’t so! My old iPad 2 has manifested no hardware problems in three... Read more
TYLT Syncable-Duo, 2-in-1 USB Cable With Appl...
TYLT has introduced the Syncable-Duo, a universal cable solution for charging and syncing data to smartphones and tablets. The Syncable-Duo eliminates the need for multiple cables by incorporating... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Solutions Consultant - Apple (United...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** Being a Business Manager at an Apple Store means you're the catalyst for businesses to discover and leverage the power, ease, and flexibility of Apple Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** At the Apple Store, you connect business professionals and entrepreneurs with the tools they need in order to put Apple solutions to work in their Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.