April, 2007

  • The Old New Thing

    Given a choice between two options, you influence the result by adding a third, inferior, alternative


    Shankar Vedantam wrote a Washington Post article and also appeared on NPR to discuss The Decoy Effect: Given a choice between two options, introducing a third, clearly inferior, option can influence your original decision. You won't pick the third option, but a clever choice of the bad third option can sway the decision toward either of the other two.

    Rationally, an inferior third option should have no effect on your choice between the two other options, but psychologists (and marketing majors) have discovered that human beings are not rational decision makers. (Anyone who has interacted with a two-year-old child is already well aware of this.)

  • The Old New Thing

    Psychic debugging: When reading unfamiliar code, assume it's mostly correct


    You may be called in to study a problem in code you've never seen before or be asked to look over a proposed change to some code you've never seen before. When this happens, you have to take shortcuts in your analysis because following every function call to the bottom would not only take far too much time, but also take you so far away from the code in question that you will probably forget what you were looking for in the first place.

    For example, suppose you're looking at some code that goes like this:

    Gizmo *gizmo = get_gizmo_from_name(name);
    if (gizmo) {
     Gizmo *parent = gizmo->get_parent();

    You might have some questions about this code.

    • What if name is NULL? Is it legal to pass NULL to get_gizmo_from_name?
    • What if the gizmo doesn't have a parent? Is there a potential NULL pointer dereference here?
    • Are the gizmo and parent reference-counted? Did we need to do something like gizmo->Release() or a parent->Release() to keep the reference counts in balance and avoid a memory leak?

    Finding the answers to these questions may take some time. For example, you might have access only to the diff and not to the entire project, or a grep for the definition of get_gizmo_from_name in the same directory that has the function in question doesn't turn up anything and you have to expand your search wider and wider in an attempt to find it.

    This is when you invoke the "Assume it's mostly correct" heuristic. The theory behind this heuristic is that whoever wrote this code has a better understanding of how it works than you do. (This is a pretty safe bet since your knowledge of this code is approximately zero.) The problem you're looking for is probably some small detail, an edge case, a peculiar combination of circumstances. You can assume that the common case is pretty solid; if the common case were also broken, the problem would be so obvious that they wouldn't need to ask an outsider for help.

    Therefore, look at the other parts of the code. For example, you might find a code fragment nearby like this one:

     // rename the gizmo
     Gizmo *gizmo = get_gizmo_from_name(oldname);
     if (gizmo) {

    That already answers two of your questions. First, you don't have to worry about checking the name against NULL because this code fragment doesn't check, and by the heuristic, the code is mostly correct. Therefore, NULL is most likely an acceptable parameter for the get_gizmo_from_name function. Because if it weren't, then that code would be broken too! (This is sort of the counterexample to what Mom always told you: If everybody else jumped off a bridge, then it is probably okay to jump off bridges.)

    Second, this code doesn't do anything special when it's done with the gizmo so it's probably okay just to abandon the gizmo without need to do any special reference count management. Because if you had to dispose of it in a special way, then that code would be broken too!

    Now, of course, this heuristic can be fooled, but if you're operating with only partial information, it's often the best you can do. Get it right often enough and people will believe that you too have psychic debugging powers.

  • The Old New Thing

    The wisdom of seventh graders: Designing an elective course


    Last month, I spent a few hours reading essays written by seventh graders (ages 12 and 13) on the following subject:

    Electives are often fun and different from our normal core classes. Imagine you have been chosen to create one new elective for our school. In a multi-paragraph essay, explain what the new elective would be like and what students would learn.

    (Students were given two hours, plus one additional hour upon request.)

    Before I present you some of the responses, I'd like to take some time to address the students on the subject of writing. I know they won't read this, but I have to tell someone.

    • Stay on prompt; in other words, make sure you address the assigned topic in its entirety. Don't get so carried away describing the class itself that you forget to explain what students would learn. Going off prompt is an automatic disqualification.
    • Stay on mode; in other words, write the type of essay requested. This example demands an expository essay, not a persuasive one. You aren't trying to convince other students to take your elective. As with going off prompt, going off mode is an automatic disqualification.
    • Do not introduce new material in your conclusion. Your conclusion is for tying your argument together; it's not a place for "oh, wait, I forgot something."
    • Take it easy with the exclamation points! It's really distracting!
    • Inside these questions there is often hidden another question, usually a Why. Here, the hidden question is "Why is it important that students learn that which this elective teaches?" Addressing the hidden question takes your essay to the next level. This tip is only for advanced writers; don't even consider addressing the hidden question until you can handle the explicit one!
    • Mind your frame of reference. Don't start with students and then switch to you. Example of error (exaggerated): "Students who complete this class can show off to your friends." I'd say that over half of the essays made mistakes like this, though none so blatant.
    • Mind your antecedents. When you use a pronoun, make sure the noun to which it refers is unambiguous. Example of error: "If teachers or anyone else hears insults, they will be thrown out of class."
    • I'm sure there are adjectives other than cool and fun. Try using them once in a while. It's cool and fun.
    • Many rules of writing can be broken, but don't break a rule until you've first mastered it. Richard Wagner can write an orchestral prelude that consists of a single chord for over four minutes, but that's because he's Richard Wagner, and he knows what he's doing.

    What electives would seventh-graders design for themselves? Here are the ones from the essays that I read (plus some that other readers shared with me, marked with asterisks), broken down roughly into categories, and illustrated with selected sentences from those essays or closely-related essays.

    • Sports and exercise
      • Skateboarding.
      • Fencing.
      • Cricket.
      • Soccer. The student explains that the problem with standard gym classes is that you don't get to spend much time focusing on any one sport. "This class will have more variety than any other class."
      • *Basketball. "Shooting is a exceedingly important part of basketball because if you didn't score you would have no points. Shooting well helps your team's chances of winning the game."
      • *Swimming. This essay had an excellent introductory paragraph that ended with "We need more electives. We need variety. We need swimming." It's a joy to read from students who have such control of writing that they can explore rhetorical devices.
      • Paintballing [sic]. "There is not another elective where the goal is to shoot another student to win." (How about photography?)
      • A single sport (chosen by the student).
      • Exercise for students who are overweight or have physical disabilities.
      • Weight training. It "strenthens strength."
      • *Skydiving.
    • Arts
      • Glass blowing.
      • Metal shop. (The school already offers woodworking.)
      • Drawing. "There is, quite literally, a fine line between a sketch of a drawing and the actual drawing."
      • Cartooning.
      • Architecture.
      • Creative writing (2×).
      • *Rock music performance. "Would you rather be shreding (on the guitar) or playing Betovon."
    • Sciences
      • Electronics.
      • Material science.
      • *Writing video games. "Each kid would pay a fee of $20 to pay some real progromers to teach the class for a year."
    • Personal development
      • Make-up and beauty.
      • Style and fashion. Specifically, shopping for clothes.
      • Self-image improvement. Specifically, by improving one's hairstyle ("Braids, twists, and buns, OH MY!"), make-up, and fashion sense. "From color choices to what fashion style best suits them, it can get very confussing."
      • Information. A combination of writing and public speaking. The student's recommendations for presentations? The second slide should contain ten bullet points. "The third slide would be a five paragraph essay about the topic."
    • Languages and cultures
      • Foreign travel. "The students might have to take quizzes." The class concludes with trips to seven countries, each lasting one week. "It would be worth it to say, 'I have been to all seven countries just in Middle school.'" The student acknowledged that this class would be expensive. "It will cost you 1000 dollars to get your seven tickets to fly to all those countries."
      • Foreign language (unspecified). (Most schools in the United States do not introduce foreign languages until high school, typically age 14.)
      • Mandarin Chinese (2×).
    • Life skills
      • Introduction to automobile driving and safety. Students are too young to drive cars, so they will train on go-karts. "Go-karting would allow you to practice those basics so much that driving a car would be a walk in the park."
      • Wilderness survival. The final exam is a practical.
    • Careers
      • How to get a job. Focusing on the mechanics, like filling out an application.
      • Choosing a career. Students research various careers to learn more about them.
      • Veterinarian. "This elective would teach students all that they need to know to become a vet such as the basics of being a veterinarian and how to treat animals." The basics are all you need to know!
      • Doctor. Students will attend class in surgical scrubs and perform dissections and autopsies. Field trips include going to a local hospital and assisting an actual doctor. (You are responsible for your own malpractice insurance.) "In conclusion, having a class about what a doctor does is what we should have at our school because it is informative yet amusing."
    • Wildcard
      • *"I sugjest reshersh and repor class."
      • *Adobe Photoshop. This essay was a one-page advertisement for Photoshop, going into the product's features in fantastic detail.
      • Fire.
      • Inventing. Students will create an invention over the course of the term.
      • Television appreciation. "Our current electives require thought and work. ... This class is a much needed easy A+. ... This class will prepare our students for the real world. You can learn more life lessons in an episode of Spong Bob than the average person can learn in his life. ... Watching mindless television for a whole period for credit will be a great elective."
      • *Being lazy. This student cut to the chase. Not like that "television appreciation" student who tried to disguise it as something remotely educational.
      • "Social hall." This class is limited to fifteen students and consists only of students you choose to take it with. One of the school buildings will be converted to "social hall," with one classroom for doing homework, another classroom converted to a movie theater, and others available just for hanging out with your friends. Food and beverages will be provided, of course. There is no examination. "I think Social hall would be an excellent elective. Students will inhance social skills, physical skills and eating ideas."
      • "Anything you want." Students are free to roam the campus for this double-length period. "It would be the greatest elective yet to be invented. There will be no teachers, no rules, no fighting."

    Other sentences, taken completely out of context for maximum amusement.

    • *"Sure fit people are in good shape but they nee exersise too."
    • "Students would prefer it for its ingenuous content."
    • "There is only so long a 7th grader can sit in a cluttered room and write." (A comment on the assignment, perhaps?)
    • *"In this class, students will learn how to vacation."
    • *"We would be able to go to the lake three times a week, Tuesdays and Thursdays."
    • *"It would most likely shock the students that when you have a job you have to do just about everything by yourself."
    • *"Art is hard and easy but not both."

    Spelling corner: See if you can guess the word before reading the answer.

    soddering hazardist conclonstion
    gragwwhts Joner of Achievement
    tords enchurens

    Best new word: "confussing"

    Note: Hover over dotted words for explanations/answers.

  • The Old New Thing

    The format of bitmap resources


    The next entry in the continuing sporadic series on resource formats is the bitmap. You thought accelerator tables were simple; bitmaps are even simpler.

    A bitmap resource takes the same form as a bmp file, except that there is no BITMAPFILEHEADER. That's all.

  • The Old New Thing

    Perhaps the movies made too literal a translation


    One person in our group that went to see the movie True Lies back in 1994 is a native of the Middle East and knows Arabic. Our friend was therefore able to give us a side commentary on the quality of the Arabic used in the movie.

    The opening sequence takes place at the embassy of a Middle Eastern country, and Arnold Schwarzenegger's character infiltrates the affair, in part by speaking what the movie subtitles reassure us is "perfect Arabic". Our friend leaned over and confided in us, "That Arabic is awful." This was, of course, hardly surprising.

    Towards the end of the movie, Arnold's character has infiltrated the bad guys' secret hideout and listens in to a fiery speech given by the head bad guy. Jamie Lee Curtis's character asks him, "What's that guy saying?"

    Arnold's character responds, "We shall triumph over the evil Americans, blah blah blah."

    We asked our friend, "What is that guy really saying?"

    "Believe it or not, he's saying 'We shall triumph over the evil Americans,' and the rest is gibberish."

    The translators must have left that in as a little bonus joke for the Arabic speakers in the audience.

    In 1995, the Windows 95 team dispatched pairs of employees to meet with IT departments around the country and install betas of Windows 95 on their computers to see how well it integrated with their networks, how well it handled that company's hardware configurations (Windows 95 being the first Microsoft operating system to support Plug and Play), ran the company's line-of-business applications, and generally see how it fared in a "real-world environment". (There were also follow-ups to see how Windows 95 was doing after being up and running for a while.)

    My friend happened to be on one of those trips at the Pentagon on April 19th, 1995. They were installing Windows 95 on a set of machines, when somebody came into the room and said, with a very serious expression, "You need to leave now. Stop whatever you're doing and come with me." If you're in the Pentagon and somebody tells you, "You need to leave now," you don't ask any questions. You just leave.

    They went to a nearby pub for lunch and saw on the television the reason why everybody at the Pentagon was so nervous: A bomb had just gone off in Oklahoma City, and public suspicion was initially cast on Middle Eastern terrorists. Being a Middle Eastern person in Washington, DC right after the Oklahoma City bombing was "very uncomfortable", my friend told me.


    April 19th is a particularly bloody day in United States history. April 19, 1775 marked the Battles of Lexington and Concord, generally considered to be the opening battle of the American Revolutionary War. Four score and six years later, on April 19, 1861, the Pratt Street Riot in Baltimore marked the first death of the American Civil War. More recently, April 19, 1993 marked the deadly end of the siege of the Branch Davidians, followed two years later by the Oklahoma City bombing.

  • The Old New Thing

    Trivia: Pea, marble, dime, nickel, quarter, half dollar, walnut...


    Pea, marble, dime, nickel, quarter, half dollar, walnut, golf ball, hen egg, tennis ball, baseball, teacup, grapefruit, softball.

    What is this a list of?

    Answer. Note that the list is different in Britain. (Original story that tipped me off to this list.)

  • The Old New Thing

    Psychic debugging: Why you can't control page navigation when using PSP_DLGINDIRECT


    Here's a problem that floated past a few years ago.

    We switched our wizard from using dialog resource IDs to using PSP_DLGINDIRECT because [reasons deleted since they aren't important]. But once we did that, the Next button doesn't work! Anybody have any ideas what's going on?

    I made things a little easier by deleting the information that isn't relevant to the problem. See if you can solve it before reading further.

    Here's my reply:

    My psychic powers tell me that your wizard navigation code is still trying to navigate by ID even though you aren't using IDs any more.

    The PSN_WIZNEXT and PSN_WIZBACK notifications allow you to control navigation by returning the dialog identifier of the page you want to go to. If you change from dialog identifiers to indirect dialogs, you have to remember to update your page-switching logic as well.

    But how do you specify pages when you aren't using dialog resource IDs?

    Let's take a step back and look at the way dialogs are specified. There are three ways to do this:

    • By dialog resource ID: psp.pszTemplate = MAKEINTRESOURCE(n)
    • By dialog resource name: psp.pszTemplate = TEXT("name")
    • By dialog resource indirect: psp.pResource = lpTemplate. If you use this method, you also have to set the PSP_DLGINDIRECT flag.

    If you look at the property sheet page structure, you'll also notice that the pszTemplate and pResource members are actually unioned together; they are just alternate names for the same thing.

    If you specified your page via dialog resource ID, you can return that dialog resource ID; but what if you used a dialog resource name or an indirect dialog? Well, since the dialog resource ID, resource name, and indirect dialog are all stored in the same place, you just pass whatever you passed in the PROPSHEETPAGE.pszTemplate / pResource originally. All the property sheet manager does is compare the value you pass in with the value you specified in the PROPSHEETPAGE. (As of this writing, the documentation doesn't make this clear; I've submitted a doc change request to fix it.)

    This technique works with PSN_WIZNEXT, PSN_WIZBACK, and PSN_SETACTIVE. It should work in principle with PSM_SETCURSELID and PSM_IDTOINDEX, except that there was a bug on 64-bit Windows XP (fixed in Windows Vista) that prevented it from working: The value you pass in was accidentally truncated to a 32-bit value. Oops.

  • The Old New Thing

    No farting please, we're Americans


    Apparently, when translating their Pokémon game from Japanese to English, Nintendo changed a question in an in-game personality test that asked you what you would do if you farted. In the English-language version, you merely broke a rotten egg. What, do they think kids in the U.S. don't fart? Or that they don't enjoy fart jokes. Allow me to clear up any confusion: Kids in the United States love fart jokes.

    Or maybe British kids don't fart. Perhaps that's it.

    (For those who don't get the subject, it's a snowclone of the form No X please, we're Y. Learn more here.)

  • The Old New Thing

    The Notepad file encoding problem, redux


    About every ten months, somebody new discovers the Notepad file encoding problem. Let's see what else there is to say about it.

    First of all, can we change Notepad's detection algorithm? The problem is that there are a lot of different text files out there. Let's look just at the ones that Notepad supports.

    • 8-bit ANSI (of which 7-bit ASCII is a subset). These have no BOM; they just dive right in with bytes of text. They are also probably the most common type of text file.
    • UTF-8. These usually begin with a BOM but not always.
    • Unicode big-endian (UTF-16BE). These usually begin with a BOM but not always.
    • Unicode little-endian (UTF-16LE). These usually begin with a BOM but not always.

    If a BOM is found, then life is easy, since the BOM tells you what encoding the file uses. The problem is when there is no BOM. Now you have to guess, and when you guess, you can guess wrong. For example, consider this file:

    D0 AE

    Depending on which encoding you assume, you get very different results.

    • If you assume 8-bit ANSI (with code page 1252), then the file consists of the two characters U+00D0 U+00AE, or "Ю". Sure this looks strange, but maybe it's part of the word VATNIЮ which might be the name of an Icelandic hotel.
    • If you assume UTF-8, then the file consists of the single Cyrillic character U+042E, or "Ю".
    • If you assume Unicode big-endian, then the file consists of the Korean Hangul syllable U+D0AE, or "킮".
    • If you assume Unicode little-endian, then the file consists of the Korean Hangul syllable U+AED0, or "껐".

    Okay, so this file can be interpreted in four different ways. Are you going to use the "try to guess" algorithm from IsTextUnicode? (Michael Kaplan has some thoughts on this subject.) If so, then you are right where Notepad is today. Notice that all four interpretations are linguistically plausible.

    Some people might say that the rule should be "All files without a BOM are 8-bit ANSI." In that case, you're going to misinterpret all the files that use UTF-8 or UTF-16 and don't have a BOM. Note that the Unicode standard even advises against using a BOM for UTF-8, so you're already throwing out everybody who follows the recommendation.

    Okay, given that the Unicode folks recommend against using a BOM for UTF-8, maybe your rule is "All files without a BOM are UTF-8." Well, that messes up all 8-bit ANSI files that use characters above 127.

    Maybe you're willing to accept that ambiguity, and use the rule, "If the file looks like valid UTF-8, then use UTF-8; otherwise use 8-bit ANSI, but under no circumstances should you treat the file as UTF-16LE or UTF-16BE." In other words, "never auto-detect UTF-16". First, you still have ambiguous cases, like the file above, which could be either 8-bit ANSI or UTF-8. And second, you are going to be flat-out wrong when you run into a Unicode file that lacks a BOM, since you're going to misinterpret it as either UTF-8 or (more likely) 8-bit ANSI. You might decide that programs that generate UTF-16 files without a BOM are broken, but that doesn't mean that they don't exist. For example,

    cmd /u /c dir >results.txt

    This generates a UTF-16LE file without a BOM. If you poke around your Windows directory, you'll probably find other Unicode files without a BOM. (For example, I found COM+.log.) These files still "worked" under the old IsTextUnicode algorithm, but now they are unreadable. Maybe you consider that an acceptable loss.

    The point is that no matter how you decide to resolve the ambiguity, somebody will win and somebody else will lose. And then people can start experimenting with the "losers" to find one that makes your algorithm look stupid for choosing "incorrectly".

  • The Old New Thing

    See the Seattle Symphony on television in high definition


    If you missed it on Saturday and Sunday night, you have a few more chances left. Set your DVR to record Seattle Symphony from Benaroya Hall: Brahms, Kernis and Kodály on one of its handful of repeat airings this week (the next one is tonight at 6pm on KCTS-HD). According to the story in the Seattle Times, it includes close-ups of nearly every member of the orchestra so you can watch their fingerwork or their facial expressions, plus a behind-the-scenes cam so you can finally find out what the conductor and soloist are doing when they step backstage during the curtain calls.

Page 2 of 4 (38 items) 1234