Coding Horror

programming and human factors

The Greatest Invention in Computer Science

What do you think the single greatest invention in computer science is? Besides the computer itself, I mean.

Seriously, before reading any further, pause here for a moment and consider the question.

I've talked before about how young so-called modern computer programming languages really are, and it bears repeating for context.

C is roughly as old as I am; FORTRAN is as old as my parents. But what about the new kids on the block? The TIOBE software TCPI metrics page provides some data on language popularity going back to the year 2001. Consider the tender age of many of the newest, hippest programming languages:

Ruby is barely a teenager. JavaScript and PHP haven't even hit their teens yet.

For all our talk about fancy new programming language features, I sometimes think we forget the one fundamental building block underlying all of them: the humble routine. Take it from Steve McConnell, who urges us to Use Routines, Routinely:

Aside from the invention of the computer, the routine is arguably the single greatest invention in computer science. It makes programs easier to read and understand. It makes them smaller (imagine how much larger your code would be if you had to repeat the code for every call to a routine instead of invoking the routine). And it makes them faster (imagine how hard it would be to make performance improvements in similar code used in a dozen places rather than making all the performance improvements in one routine). In large part, routines are what make modern programming possible.

If you're not old enough to remember life before routines, I thought James Shore had a great example of the stark difference in his excellent article Quality With a Name:

Before structured programming:

1000 NS% = (80 - LEN(T$)) / 2
1010 S$ = ""
1020 IF NS% = 0 GOTO 1060
1030 S$ = S$ + " "
1040 NS% = NS% - 1
1050 GOTO 1020
1060 PRINT S$ + T$
1070 RETURN

After structured programming:

public void PrintCenteredString(string text) {
  int center = (80 - text.Length) / 2;
  string spaces = "";
  for (int i = 0; i < center; i++) {
    spaces += " ";
  }
  Print(spaces + text);
}

The humble routine is the backbone of all programming in any modern language. I'm sure you're the very model of a modern programmer, so I won't bore you with a long explanation of why routines are a good idea. The original 1998 IEEE McConnell article covers the rationales behind routines quite well. There's also a greatly expanded version of that material in Chapter 7 of Code Complete 2.

Routines are so fundamental to today's programming that they are essentially invisible. That's the problem with routines: they only take a minute to learn, but a lifetime to master. If bad unstructured programming was possible, so is bad structured programming. You can write FORTRAN in any language. Wrestling with the ineffable essence of a routine is, almost to a first approximation, what programming now is:

  • How long should this routine be? How long is too long? How short is too short? When is code "too simple" to be in a routine?
  • What parameters should be passed to this routine? What data structures or data types? In what order? How will they be used? Which will be modified as a result of the routine?
  • What's a good name for this routine? Naming is hard. Really hard.
  • How is this routine related to other nearby routines? Do they happen at the same time, or in the same order? Do they share common data? Do they really belong together? What order should they be in?
  • How will I know if the code in this routine succeeded? Should it return a success or error code? How will exceptions, problems, and error conditions be handled?
  • Should this routine even exist at all?

Good programmers – regardless of whatever language they happen to be working in – understand the importance of crafting each routine with the utmost care. The routines in your code should be treated like tiny, highly polished diamonds, each one more exquisitely polished and finely cut than the last.

I'll grant you this isn't a particularly deep insight. It's not even original advice. But if you believe, as I do, in constantly practicing the fundamentals, you'll never stop mastering the art of writing the perfect routine.

It is, after all, the single greatest invention in computer science.

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: https://infosec.exchange/@codinghorror