There you are in your first computer science class. Intimidated by all of the brain power in the room and confused by the professor who is attempting to teach Python, you are afraid to ask questions and seem stupid. She presents an example:

def baz():
    a_dict = {'a': 1}
    return foo(a_dict)
def bar(*args, **kwargs):
    sum = 
    for key, value in kwargs.items():
        sum += value
    return sum
def foo(a_dict):
    return bar(**a_dict)

Don’t worry.. After years of computer science coursework and professional experience as a software developer, my eyes still glaze over when people use foo, bar, and baz. Why? People will tell you that these placeholder names (also referred to as metasyntactic variables) are meaningless and only used to demonstrate a concept.



For most humans, this is confusing. Naming things is important in software development, education, and just about any other aspect of life. Supposedly the name is unimportant because we are focusing on one particular part of the example, but I like understandable names for at least three reasons:

  1. The habit of using meaningful names is important to develop
  2. Real function and variable names give context to the example
  3. People tune out when they see that you are using fake names

Foo, bar, and baz are a distraction. If I had to choose a function or variable name that meant nothing, I would prefer a single letter like “a” or “b”, because at least that is explicitly unimportant.

def a():
    a_dict = {'a': 1}
    return b(a_dict)
def c(*args, **kwargs):
    sum = 
    for key, value in kwargs.items():
        sum += value
    return sum
def b(a_dict):
    return c(**a_dict)

Alternatively, you could use nonsensical names other than foo, bar, or baz. Even if the function name is unrelated to the specific issue that a teacher is trying to convey, the fact that it is a common word will allow the student to focus on what is being emphasized.

def apple():
    a_dict = {'a': 1}
    return banana(a_dict)
def carrot(*args, **kwargs):
    sum = 
    for key, value in kwargs.items():
        sum += value
    return sum
def banana(a_dict):
    return carrot(**a_dict)

Better yet, instead of using an arbitrary block of code to illustrate your point, find a real example that someone might actually encounter. It will take more time to come up with something that makes sense, but the person you are trying to help will be able to remember what you are telling them.

In the examples above, my friend was trying to explain the power of keyword arguments in Python. If you were going to write production quality code, would you use function names like foo, bar, and baz? Also, wouldn’t you include comments for other developers and docstrings explaining methods?

def generate_class_report(class_name, student_grades, *arguments, **keywords):
    """generate a class report.
    required arguments:
    class_name -- a string such as "biology"
    student_grades -- a dict of names and grades {"billy": 105, "jean": 64}
    optional keyword arguments:
    anything relevant to the report can be included here. in the past teachers
    have used class_pet, favorite_game, statement_of_purpose, etc.
    class_report = {}
    average_grade = calculate_average_grade(student_grades)
    class_report.update({"average_grade": average_grade})
    #if this is not done, the arguments are printed in an undefined order
    keys = sorted(keywords.keys())
    for kw in keys:
        class_report.update({kw: keywords[kw]})
    return class_report
def calculate_average_grade(student_grades):
    sum = 
    for key, value in student_grades.items():
        sum += value
    return sum / float(len(student_grades))
class_report = generate_class_report("underwater basket weaving",
                                     {"sean": 44, "nick": 99, "cara": 73},
                                     teaching_assistant="henry winkler",
                                     big_field_trip="the zoo")
print class_report
this will generate the following output:
{'average_grade': 72.0, 'big_field_trip': 'the zoo', 'teaching_assistant': 'henry winkler'}

It is ok if you love using foo, bar, and baz. At least consider that everyone learns differently and focuses on certain things. Believe it or not, it took me a long time to come up with the last example (maybe 15m). Professional engineers don’t always have that kind of time and there are usually great docs that people can refer to instead. However, if you are going to take the time to try and teach someone, make it memorable.