I just read Jeff Yearout's recent post titled The Beginner's Garden of Concepts. Not directly related but it got me thinking about programming idioms.
I've been using the phrase "programming idiom" for years to describe a short useful recurring code construct. I didn't realize that it was officially "a thing" until doing a web search on the phrase years later.
As our students grow from newbies on I think it's helpful for them to see recurring and related patterns and programming idioms gives us a name to apply to many beginner patterns. An early idiom might be "finding the smallest in a list:"
dataset = [5,3,8,12,2,7] min_index = 0 for i in range(1,len(dataset)): if dataset[i] < dataset[min_index]: min_index = i
Another is the very similar and more general "do something on every item in a list:"
for i in range(len(dataset)): # do something to or with dataset[i]
By talking about constructs like these as idioms it helps students see and develop coding patterns. It also helps them to build mental abstractions. Each of the above idioms are a few lines of code but each are also a single concept. Students learn to think of them as the concept.
When students learn about list comprehensions in python they'll rewrite the "do something…" more like this:
[ f(x) for x in dataset]
but the pattern or idea is the same.
Other early idioms might include swapping variables:
tmp = a a = b b = tmp
and loops until an exit condition are met:
while (not_exit_condidtion): # do stuff modify variable that checks exit condition
Even more difficult concepts like recursion can be described in an idiomatic way:
def f(x): if BASE_CASE: return something else: new_x = modify_to_eventually_get_to_base_case(x) f(new_x)
Patterns like these, or idioms, come up over and over again. We don't have to explicitly mention them in our teaching but I think it's helpful to our students if we do.