Chapter 9

Writing Code for the Ages

Shortly after I started working at Microsoft, many years ago, I found myself at a party listening to an old-timer reminisce about days past when real programmers wrote programs on punch cards. Well, maybe it wasn’t that far back, but the stories were fascinating to a programmer wanna-be. The most startling story went something like this. (I never saw the guy again, so if he reads this, I hope he won’t hold me to the quotes or the details.)

“It was my first programming job. I was this green kid working for an old-timer who used to toggle his programs in with switches or something. Still, I thought I was hot stuff. I was smart, I worked long hours, and, most amazing, the things I wrote worked. My boss was short-tempered and generally obnoxious, but I figured that’s just the way programmers were. So one day I was coding away, far gone in some hack, when suddenly I felt this really hard blow on the side of the head. I turned around, and there was my boss. He’d been watching me code for who knows how long.

“Dammit! Don’t ever write the same code twice when you’re working for me.”

This event had made a strong impression on the narrator, and his story made a strong impression on me. I would never walk into your office and slap you on the side of the head, but as an author I can get away with outrageous metaphors. So take this chapter as the literary equivalent of a slap upside the head.

“Dammit! Don’t ever write the same code twice when you’re reading my book.”

Of course, everyone gives lip service to modular code, but writing it in Visual Basic isn’t as easy as you might hope. Have you ever found yourself reusing code with the cut-and-paste-and-modify method rather than writing a single module and including it in various projects? The temptation is hard to resist.

The message-based Windows architecture makes it difficult to write truly modular code. Visual Basic simplifies the architecture with forms and events, but you still need to share a lot of data among different parts of your code. Making all the parts into independent code entities that you can plug into any project isn’t always simple. Versions 4 and 5 added a lot of features that make modular code easier to write. This chapter will examine some of the ways you can write code that works for everybody, anytime, everywhere.