Making Code Speak

A weblog of tools, techniques, styles and habits for programming with fluency.

A Plea for Terse Class Definition Syntax

I recently tried to tweet about an idea that doesn’t remotely fit into 140 characters.

Becoming convinced that the wordiness of class definitions in most languages is a major force working against good object-oriented design.

— Moss Collum (@moss) January 9, 2014

I’d like to expand on that.

First, consider the old saying “objects are merely a poor man’s closures”, or, “closures are merely a poor man’s object”. Closures and objects are both part of an evolutionary sequence that code can take:

Conceptually, each of these steps is very small. But in every language I know, the last step adds more code than any of the others, and with it more mental overhead and more maintenance burden. Consider Python:

# assignment
email_link = '<a href=">Moss Collum</a>'

# function
def make_email_link(first_name, last_name, email):
  '<a href="mailto:%s">%s %s</a>' % (email, first_name, last_name)

email_link = make_email_link('Moss', 'Collum', '')

# closure (though briefer alternatives are available)
def linkable_name(first_name, last_name):
  def make_email_link(email):
    '<a href="mailto:%s">%s %s</a>' % (email, first_name, last_name)
  return make_email_link

make_my_email_link = linkable_name('Moss', 'Collum')
email_link = make_my_email_link('')

# but object...
class LinkableName:
  def __init__(first_name, last_name):
    self.first_name = first_name
    self.last_name = last_name
  def make_email_link(email):
    '<a href="mailto:%s">%s %s</a>' % (email, self.first_name, self.last_name)

my_name = LinkableName('Moss', 'Collum')
email_link = my_name.make_email_link('')

Is all this extra visual noise really worth the flexibility we get for it? Why not drop the constructor, and instead explicitly make a class conceptually more like a closure?

# syntax from some hypothetical future language -- do not attempt
class LinkableName(first_name, last_name):
  def make_email_link(email):
    '<a href="mailto:%s">%s %s</a>' % (email, first_name, last_name)

Besides being shorter, this is more readable (to my eye, at least), and also closer to the earlier stages in its evolution, making it a more natural jump for somebody who’s trying to decide how to refactor their code.

So, is there something big that I’m missing? Why don’t more languages have a syntax like this for defining classes? If you’ve got any thoughts, perhaps you can guess where to email me.

posted by Moss on 13 January 2014