teaching machines

Naming Things

Often in my introductory programming classes I will share a snippet of code that is unnamed or vaguely named. I ask the students to interpret the code and choose better names.

This past week I gave them this snippet:

public static double ?(double x, boolean b) {
  if (b) {
    return x;
  } else {
    return x * 1.055;
  }
}

After giving them time to consider the code, I asked, “What’s a better name for this?” Answer 1: returnX. Answer 2: pinocchio. Answer 3: addSalesTax. No more answers. We went back through these suggestions and discussed their relative merits. Answer 1 didn’t provide any semantic meaning and wasn’t always completely true. Answer 3 was appropriate. Our local sales tax is 5.5%, but not all items are taxable.

I asked the student who offered the pinocchio answer to explain himself. Certain that he was being silly, I was leveled by his justification: “Everytime the expression is false, x gets bigger.”

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *