12C. Choosing Function, Parameter and Variable Names

Choosing sensible and descriptive names is an important part of the documentation of a program. Well chosen names greatly enhance the readability of a program. Poorly chosen names lead to confusion.

If a function compares two strings, a reasonable name for it is compareStrings. If a function reads a graph, call it readGraph.

If a variable is your dog's name, you might call it myDogsName. You can feel free to use short names such as x and y for parameters and variables that only exist in a small function.

But watch out. Suppose you describe a one-way road by two parameters, where the road starts and where it ends. If you call those two parameters u and v, it is easy for you to confuse the two, and that is just asking for more mistakes and more debugging. If you call them roadStart and roadEnd, you will avoid those mistakes.


A few examples of poor function names

Here are few misleading function names that past students have used in their programming assignment submissions. (Yes, I read your submissions.)

  • A function called insertCell that does not insert anything into anything.

  • A function called compare that does not compare things.

  • A function called printNode that does not print a node.

  • A function called WriteGraph that does not write a graph; it reads a graph.

  • A function called ProcessEvent that does not process an event.


Standards

The standards require you to choose sensible function names. Most importantly, do not choose function names that are misleading about what the function does.