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.*

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.