Design for Understandability

This is a talk I gave at Google’s internal Code Health Summit in November of 2025. I’m sharing it here in the hopes that it sparks further learning and discussion.

Abstract

In a world where LLMs generate much of our code, understanding that code is more important than ever. But with a system as large and complex as Google’s codebase, SWEs can easily fall into a pattern of learned helplessness where the best you can do is tweak the code and pray that it passes the tests. Without understanding, LLMs are little more than complexity-generating machines running at top speed.

There’s a better way to go about this. Just as our industry has learned to design software for testability, we can also design it for understandability. We can use ideas like design by contract, class invariants, modularity, and more to create systems that we can understand one piece at a time, rather than piling more onto the big ball of mud and hoping it doesn’t all collapse.

Is it easy? No. But is it worthwhile? Absolutely. These ideas aren’t new, but they’re not nearly as widely taught or well understood as they deserve to be, and mastering them leads to a world where we can have more confidence in our code and increase velocity at the same time.

Slides and Transcript

Hi everyone, thanks for having me! Let’s get started.

I’d like to start this talk with a question: If LLMs can code, then what is left for human SWEs?

The tech industry is quickly looking to large language models to write all of our code. But you might say: isn’t writing code our job as software engineers? Where does this leave the responsibility of SWEs? What is the essential human element that SWEs add on top of LLMs?

Every one of you is going to have a different answer to this question, and that’s fine. But here’s my take.

That necessary human element is judgment. Judgment of whether the code is maintainable. Judgment of whether it is healthy.

LLMs can follow patterns and generate code that looks like what it was trained on. Sometimes they can produce useful tips and critiques for a piece of code. But that critical piece of judgment, of deciding whether some code is suitable for the task it was designed for, is still up to you, the human.

Because here’s the thing: LLMs today are still a sloppy intern. Sometimes the code they produce is good, but sometimes it’s completely off-track and needs correcting. You wouldn’t incorporate a sloppy intern’s code into your system without a close review first, so you shouldn’t do so for an LLM’s code either.

YOU are responsible for the quality of your code, regardless of whether you had an LLM generate it for you or wrote it the old-fashioned way with fingers on a keyboard

So where does that leave us? If LLMs are going to write a lot of the code for us, then we’re looking at a future where we humans are going to spend less time writing code, and more time reading code and understanding it, in order to make sure it passes our quality bar.

This means it’s more important than ever to get better at understanding code, and to speed up that reading and understanding process.

Now: if you want to get better at something, it’s helpful to set an aspirational goal for yourself. Something that’s above your current skill level, and drives you to get better and better every day.

Here’s the goal I propose: we should be able to predict the behavior of every line of code, without running a single test.

Let me say that again: We should be able to predict the behavior of every line of code, without running a single test

Now, I know you may be thinking: “Understand every line of code? Without testing? Are you serious?! There’s way too much complexity and legacy code for me to ever be able to do that!”

You might think so. And if we take our hands off the wheel and relinquish all control, you might be right. But I’m here to tell you there’s a better way.

We can design our code to be understandable.

Consider that a decade or two ago, we used to think that writing multiple tests for every public method in an API would have been infeasible. It wasn’t because we didn’t want to, or that we weren’t trying. It was because we hadn’t designed our code to make testing possible at that level. But gradually, across the software industry, we changed our practices to design for testability. Today, designing our code for testability is so ingrained into our culture that doing anything else is practically unthinkable. We can do the same for understandability, so that our code enables us to understand it better.

Our own Code Health guide makes a strong case for understandability. It says “For code to be sustainable, it must be understood. Change cannot be made unless a developer knows what the system currently provides, and what needs to change to adapt to new circumstances. As a result, key components of code health include good documentation, easy to understand error messages, and clear, simple code”

Not only is designing for understandability important, but it’s a learnable skill. It’s not some magical ability that you either have or you don’t. There are specific, concrete actions you can take to improve the understandability of your code.

I’m going to introduce you to a few of those today. Think of it as a “starter kit” for designing for understandability. We’ll also talk more about a general theme that runs through all of these practices, so that at a certain point you can find even more ways to design your code for understandability.

The ideas I’m going to show you today are not new. But they’re not taught as well as they should be, when they’re taught at all, and most of us have only a rough understanding of how to apply them. My goal today is to sharpen that understanding just a little bit, and make it just a little more practical, so we can kickstart a process of making our code more understandable over time.

So let’s get started on each of these Design for Understandability practices.

The first one is Design by Contract.

To motivate the idea, let’s ask the question: when calling a method, what behavior should you rely on?

Let’s say we have this binary search method, but its documentation is not terribly helpful (you’ve probably seen something like this before).

Binary search is reasonably well understood, but as a caller of the method, we have some questions we want to ask about this specific implementation. Such as:

  • Will the returned index always be the first occurrence?
  • What happens if t is not found?
  • What happens if the list is not sorted?

The documentation doesn’t answer these questions. So what can we do?

First, we could guess. But we’ll probably be wrong.

Second, we could read the code. But that might take a long time, especially if the particular behavior we care about is buried several levels deep.

Third, we could test the code on some examples and see what happens. But just because it works a certain way on some examples doesn’t necessarily mean it works that way for all such inputs. Furthermore, even if it works that way now, there’s no guarantee that it will continue to work that way in the future.

The point is that without a clear statement of what this code is intended to do, there’s no distinction between implementation and intent. The SWE who wants to use this code has to guess at which behavior the method commits to, versus which behavior is an implementation detail that might change over time.

This is where contracts come in.

There are many ways to define a contract, but for the sake of this talk I’m going to define it as a formal-ish description of the observable behavior that each participant in an interaction commits to.

In the real world, a basic contract is something like “If I cook dinner, then you will wash the dishes”.

In programming, an example might be: “If the caller provides a non-negative integer n, then the square-root function returns the square root of n”

Let’s see how contracts can help us with our binary-search example.

Here’s a possible contract for this function. It says, “If the list is sorted, binarySearch returns the index of the first occurrence of t in the list, or -1 if not found. Behavior is undefined for unsorted lists.”

We can see that this contract answers the questions we asked about it:

  • Will the returned index always be the first occurrence? Yes, the contract explicitly says this.
  • What happens if t is not found? The contract says binarySearch will return -1 to signal that case.
  • What happens if the list is not sorted? The contract doesn’t say anything, so this behavior is intentionally left undefined. And that’s a valid design choice! There may be performance reasons to not check whether the list is sorted at run-time. So this contract tells the user that they shouldn’t rely on any particular behavior if the list is not sorted. Instead, the obligation is on the caller to check whether the list is sorted before calling binarySearch.

The power of a contract is that it defines a clear boundary between the author’s intent and the implementation. The caller doesn’t have to guess at the behavior anymore: they can just read the contract, and if a specific detail of the behavior isn’t there, they will know that it’s unspecified.

In reality, this isn’t always perfect. Whether intentionally or accidentally, we still tend to end up with code that relies on implementation details rather than specified behavior. But contracts at least give us a way to minimize this behavior.

Now that we understand what a contract looks like, let’s talk about how contracts can help us reason about and understand our code better.

Suppose you just wrote a method. I’m going to draw that method as a little circle here.

You want to know if it’s correct.

I’m going to use a purple circle to stand for methods we’ve read the code for

Now, our method doesn’t live in isolation: it calls some other code.

So, you think “Okay, well, I need to understand what those do. I’m going to read the code for those methods.”

Well, those methods call other methods, and we start thinking about this, and figure out how we’re going to reason about those. And at this point you can maybe see where I’m going.

Oh no. Now we have lots and lots of code we have to read across our entire code base.

Why do we need all this reasoning just to reason about one single method? That’s all we’re trying to do! How did we get so out of control?

So, let’s back up and try again, using Design by Contract this time.

You still have to read your own code. We can’t get around this part; that’s the change we just made.

And we haven’t changed the code. It still calls those other methods.

But now, the trick is that we can read just the contract on those called methods, not the code.

And that’s it! We can stop here. We don’t need to worry about all of that other code. The contracts tell us all we need to know about calling these other two methods.

The rest of that code still exists, but we don’t have to think about it. The contract abstracts away all that other behavior that doesn’t matter to us as the caller of these two methods.

So that is the power that contracts give to us to reason about our code.

Finally, I’ll wrap up this section with a few tips for writing contracts:

First, document the behavior that you’re willing to commit to. This is usually the same as what you want your callers to rely on. For example, you probably want to document the return value of your method. But the exact human-readable text in your error messages? Not so much.

Second, describe your error handling in your contracts. A contract does not and should not be just about the happy path. If your method might go wrong in some way, tell the caller under what conditions it might go wrong, and how you’re going to signal that to the caller, whether it be through a special return value, or an exception, or something else.

Finally, keep in mind that the more callers your method has, the harder it is to change the contract. If it’s a private method used only by your team? Sure, that’s probably an easy case to change both the contract and change the callers to match. If it’s a public method on a popular framework used by hundreds of other projects? Then renegotiating the contract is a much more expensive effort, so you should evaluate the costs and benefits carefully before deciding to do that.

So that was the first element in our Design for Understandability starter kit.

The next one is data invariants.

Let me explain this idea with a little story.

Early in the COVID-19 pandemic, when my wife and I started working from home, we missed having free soda at work. So we got in the habit of keeping cans of Diet Coke in the fridge.

But there was a problem: we were great at taking cans out of the fridge, but not so great about putting new ones back in. So if you’re working hard and looking forward to taking a break with a nice cold beverage, it was a big disappointment to open a Coke-less fridge and resort to drinking room-temperature soda from the pantry instead.

The problem was that we hadn’t set any rules on how we retrieve Diet Coke and how we replenish the fridge.  Without any rules governing access to the fridge, we eventually got into an unwanted state: in this case, an empty fridge.

This happens in code, too. For example, let’s say you have this proto to represent a customer’s order [Note: “proto” is Google jargon for a Protocol Buffers message, similar to a JSON object]. If you receive this proto, how much do you trust that the total actually indicates the total amount on the items?

Probably not much, right? This proto could have come from any part of your code, so it’s easy to imagine that someone forgot to set that field appropriately.

Protos give you completely uncontrolled access to the underlying representation of your data. Protos are great for transferring data back and forth across the network, but they’re poorly suited to situations where you want to rule out certain states.

To solve these kinds of problems, we can use data invariants. A data invariant is a statement about a piece of data that is always true.

Data invariants are a principled way to enforce these kinds of valid states in your data. If you use them well, you can avoid a lot of bugs in your code.

Let’s see how we can use invariants to solve the Diet Coke problem, and similar problems you might come across in your own code

First, identify the invariant you want to maintain.

Ask yourself: What’s the property you want to rely on for this piece of data? Where are the places in the code where you explicitly validate and return an error if invalid? Alternatively, where do you make the assumption that it’s invalid, but you’re not actually sure?

In this case, the invariant we want is “the fridge always contains a Diet Coke”.

Second, you need to document the invariant.

It’s important to note this is a separate step. It’s one thing to have an invariant in your own head. It’s another thing to write it down so that you communicate it to everyone else who might touch this code. Not to mention that if you come back to this code months or years later, you yourself might have forgotten which exact invariant you meant to maintain.

Write it down where people will see it: probably close to the relevant data members, or maybe in a class-level comment at the top.

Third, establish the invariant at creation time. Usually this will be done in a constructor or factory method. In this case, we’re constructing the fridge with a few drinks already added.

Fourth, every time you change your the data, make sure you maintain the invariant.

This is the part my wife and I didn’t do for our fridge. What we should have done is established a rule where if you take a Diet Coke, you put another one back in the fridge to make sure there’s another for the next person.

You can maintain the invariant many ways – that part is up to you. The important thing is that you always maintain it. Without that, you have to think to yourself “Wait, are there going to be Diet Cokes in the fridge? Did I already take them out earlier? Let me go back and check all my callers to see if they did that already”. With an invariant, though, you just know: If I have a Fridge object, then it definitely has Diet Cokes, regardless of the sequence of operations that led to this point.

Finally, one last step, and this might be the hardest one: you need to maintain the invariant as the code evolves. It’s relatively easy to maintain the invariant for a single version of code. But over the weeks, months, and years, complexity will creep in, new requirements will force you to change the code, and you might accidentally break the invariant or forget you even had one.

Establish the culture on your team that maintaining invariants is just a thing you do. If you have a mental checklist of things you look for in code review, make sure that maintaining the invariants is on that checklist.

So how do data invariants help us design for understandability? Let’s first consider what we have to do when we don’t have an invariant.

Recall this representation of all the methods in your codebase, calling each other in various ways. Suppose you have a proto-based version of the fridge, and you want to know if it has any Diet Cokes in it. Because it’s a proto, you don’t have a way of knowing what state it’s in.

So, you need to check all the callers of the current method.

And you do the check, crawling across the codebase, and you find that there’s one particular case where you didn’t enforce it.

Of course you can fix these issues one at a time as they crop up, but all that searching sounds like a lot of work, right? What do you think are the chances you’re always going to find that one particular buggy spot? What are the chances that new methods added to this web of code are going to maintain that condition?

In contrast, let’s try the version with our Fridge class and its invariant.

In this case, the class has an invariant: the Fridge always has at least one Diet Coke, no matter what operations have been done previously or where we came from. By maintaining that invariant, we’ve removed the need to check what every single caller does.

This is what data invariants do for you: they let you focus on the state you’re in now, and not how you got there.

That wraps up the section on data invariants.

The final practice I want to discuss today is isolating side effects and dependencies on global state.

To motivate this idea with an example. Let’s say there’s a supermarket point-of-sales system with this method to compute sales tax. Now ask yourself: what are the inputs and outputs for this method?

Seems pretty obvious, right? It takes in a customer’s grocery order and returns the sales tax as an integer.

But that’s not the whole story. If you dig into the implementation of this method, you’ll find that it reads in a set of sales tax rates from a separate file. That’s effectively a new input channel! The code depends on that data to compute the tax just as much as it depends on the content of the grocery order itself.

And if you look further, you’ll find that the method also records the computed tax in the store’s account database for bookkeeping purposes. That’s a new output, but it’s nowhere to be found in the type signature!

So functions have two kinds of input and output: the ones that are visible in the type signature, like the order and the computed sales tax, and the ones that are hidden, like the tax rates file and the entry in the accounting database.

The hidden outputs are more commonly known as side effects. Side effects generally come in two flavors: mutation, so modifying something like a global variable or one of your input arguments, and outputs outside of your process. That would be things like database writes, remote procedure calls, printing to the console, etc.

I don’t know a common term for the hidden inputs, but I would call these dependencies on global state. Again, there are two flavors: dependency on the state of some global variable, or a dependency on the state of the system your program is running in. That’s things like permissions to access the database, the existence of a given file on the file system, and even the ability to read the system clock.

So why are these hidden inputs and outputs a problem? They make code hard to test, hard to reuse, and hard to reason about.

Hard to test because you have to make sure you have all the necessary dependencies are set up before you can even run the method in your test. And then once you do run the method, side effects often force the test to rely more on interaction verification than state verification, which can be brittle.

Hard to reuse because the code now makes assumptions about what kinds of resources you have available to you. If you need to reuse some method in another part of the codebase, but the input data needs to come from a different source, or you need to store the results in a different place, those side effects and global states are going to create a tight coupling between your computation code and the specific sources and destinations for that computation, which will make your life harder.

Hard to reason about because, by definition, these inputs and outputs are hidden! If you don’t even know that your method is making these kinds of assumptions on the system that it’s running in, then you’re going to make some faulty conclusions about how your code will behave.

Now I’m not saying all side effects or global state are bad. And I’m not saying we should all immediately switch to a pure functional programming language like Haskell.

Effects have to happen somewhere. Without side effects, computation is just the process of making a metal box hot for a little while
(and even that heat is a side effect! although not usually one we care about).

But by default, we tend to sprinkle them everywhere throughout the code. Need some input? Just read the file at whatever point you happen to be at in the code. Need to store your result? Go ahead and do it 4 layers deep into your business logic.

Without a principled approach to side effects and global state, we’re constantly running into the kinds of problems we just described.

There’s a better way, which is to isolate the side effects and the dependencies on global state. Much of our code is just logic on top of the data (once we get it) and creating new data based on that. Divide your code into the computational parts on one hand, and the parts that deal with side effects and global state on the other.

There are a few common patterns for this.

One of them is Command/Query Separation. It’s a pattern where each method is either a command, which performs some side effect, or a query, which is a pure function that returns its output as the return value. But you maintain a strict separation so that no function does both of those things.

Another pattern is called “Functional Core, Imperative Shell”. This is a pattern at the architectural level. You focus the inner modules of your system on the business logic and computation, not performing side effects – this is the “functional core”. The outer modules coordinate the effects of your system with that computation, so that the complexity of the side effects is isolated to just that outer layer, and the majority of your system is simpler due to the functional purity.

As with the other two practices, side effects and global state have a big impact on how we think about code.

For example, you might be working on a method and need to delete a file. Your assumption is that it should be okay, you think no one is using it.

But then it turns out, some other part of the codebase was relying on it, and you just broke them. The code was so far away and unrelated that you didn’t even think to check there.

On the other hand, if you isolate your side effects and global state such that they only happen in a known region of the code, then you can have confidence that you really aren’t going to break anyone else.

So, those are the three starter practices for Design for Understandability: Design by Contract, Data Invariants, and Isolating Side Effects and Dependencies on Global State.

Now, I’ve presented these as independent practices, but in fact they’re not so distinct. There’s a unifying theme to them. That theme is local reasoning.

What do I mean by local reasoning?

Local reasoning means you can come to conclusions about some chunk of code by looking only at that chunk of code, plus just a small amount of the neighboring code, like the specifications or contracts of the components that you directly interact with.

This is as opposed to global reasoning. That’s the kind of reasoning where to answer even a simple question, you have to read a large chunk of the codebase, because the answer could be in any method, at any level of the call stack.

The benefit of local reasoning is that it puts a hard boundary on how far you have to look in order to figure something out about your system: there’s always a limit to how far you have to search.

So let’s talk about how each of the practices we talked about enables local reasoning. You may have noticed we covered this already, but let’s review.

Design by Contract defines the expected behavior for a caller and a called method. That means that to understand what behavior a caller can rely on, or what behavior a called method is obligated to implement, the only place we need to look is the defined interface for that called method. Without that, we would revert to global reasoning to find all the parts of the code that might depend on some aspect of this method’s behavior.

Data invariants often go hand in hand with contracts, but they usually say something about an entire object rather than a single method on that object.

If you have a piece of data and you want to know something about its validity, like “does this fridge have any diet cokes?”, an invariant is what lets you immediately conclude yes, without looking at the callers that produced that data.

Finally, by isolating side effects and global state, you minimize the number of places in your code where a change in one part of the code can cause a problem in a completely unrelated part. So it lets you say things like “this is the only part of the code that’s allowed to touch this file, so I know for sure that it’s safe to delete”.

Another way of looking at local reasoning is to say that it minimizes the amount of context we need in order to reason about the system.

With global reasoning, you might need to know about any arbitrary part of your codebase, because the part that breaks your assumptions could be anywhere. You need to have the whole system loaded into your head in order to understand it.

Local reasoning keeps the amount of stuff you need loaded into your head to a reasonable size.

And guess what else benefits from small contexts: LLMs!

A key portion of making an LLM useful for your work is giving an LLM the right context to give good suggestions. But contexts costs tokens, and your tokens are limited. So LLMs give us another great motivation to use local reasoning and minimize our context.

In other words, if you want your LLMs to be useful to you, then design your systems for understandability. Keep the necessary context small to make it easier for both you and your LLM to work with your code.

At the beginning of this talk, I compared “Design for Understandability” to “Design for Testability”. I’d like to revisit that for a moment.

Before designing for testability was the norm, the default testing approach was application-level testing. Our code wasn’t set up for practices like dependency injection, so there was no way to run your code without setting up a huge set of dependencies as well. It wasn’t until we started designing and building our systems with testability in mind that we were able to make unit testing practical.

That’s how it is with understandability and local reasoning, as well. If you don’t design for it ahead of time, it’s somewhere between hard and impossible to use local reasoning. But if you design for it along the way, you can make it work.

If you’ve never heard of unit testing, then application-level testing seems obvious, and straightforward. But it’s not sufficient for modern software engineering. The same is true for global reasoning. I say it’s time we made designing for understandability a required part of our work, just like we did for testability.

So you may be thinking: that all sounds good, and these practices sound good on paper, but is designing for understandability feasible in practice? You’re probably not working in a brand-new greenfield project. You’re probably working on old code, with lots of old warts and complexity, and the situation may seem hopeless. Can you really do this?

I’m here to say yes, this is feasible, if you take it one small step at a time. I’ll share some tips for getting started.

First off, take little bits at a time. Don’t try to boil the ocean. Don’t say “We’re going to take two weeks off from our normal work, make all of our code perfectly understandable, and then we’re done!”. That’s not going to work; you’re going to have a bad time.

Instead, try applying some of these practices to just new methods that you write. You might have some old code out there that’s hard to change. So be it. Start with just the new methods and build some momentum.

Teach your teammates, as well. Maybe share this talk, or find other ways to help your teammates design for understandability.

And finally, you might consider group programming sessions where you all get together and discuss: what does designing for understandability mean in your context? The Monarch team at Google did this with their Design by Contract practice and it worked well for them.

Here are some further resources to learn more about Design for Understandability. I’m not going to go into them in detail, but please feel free to go revisit the slides after the talk and check these out.

[Public links reproduced here for the blog post:]

In conclusion: design your code for understandability. Whenever you’re working on some code and realize that you’re falling back to global reasoning, pause for a moment. Ask yourself: can I do this with local reasoning instead? Would a contract or an invariant help here? Or what if I moved the side effects and global state somewhere else? Or, maybe you’ll think of some other practice that fits here – you probably know a lot of local-reasoning practices, even if you didn’t have the terminology to call it that until today.

It’s time to make design for understandability just as much a part of software engineering as designing for testability is today. Understandability is not just helpful – it’s crucial, especially with Google-size systems. And not just that, but making your code understandable is doable if you design for it along the way.

Trust me: you’ll be glad you did.

Why I’m Reluctant to Use or Promote LLM-Based AI

Someone on lobste.rs recently asked what the “root cause” is for programmers feeling depressed about the introduction of LLMs to our industry. I of course can’t speak for all programmers, but something about the question motivated me to finally put my general feelings about LLMs into words. I’m reproducing my reply here, copied from the original.

This has been on my mind a lot. I want part of my job to be teaching people to be better software engineers. But I have serious reservations about LLMs, and it’s hard to imagine a company these days hiring someone to teach software engineering without expecting them to encourage heavy LLM use. I don’t have a good answer yet on how to resolve that tension. Maybe this post will get me a little closer.

As always, this post reflects my personal thoughts and opinions, not those of my employer.

For me, the root cause is that I see LLM use as unethical (mostly due to the environmental and intellectual property issues involved in training LLMs, but for other reasons as well). But much of the tech industry (including, it seems, the vast majority of employers) are saying “If you want to stay competitive and survive in this industry, you have to use LLMs to create software”. So the choice is seemingly (1) use the big unethical thing, or (2) leave the industry.

Coupled with that is the fact that I care deeply about software quality, but LLM-generated code is making many people care even less about quality (or notice it less) than they already did. There’s a general sense of “the LLM wrote it, so it’s probably good enough”. I deeply disagree with that sentiment, but making that argument is increasingly an uphill battle.

I do still enjoy the “programming” task itself, in the sense of writing code to do what I want, both at work and as an infrequent hobby. I’ve had to use agentic coding at work enough that I can see the value in it, and I definitely don’t feel a sense of loss or like I’m being replaced or anything.

If the ethical issues above were resolved, I’d be happy to use LLMs as part of (but not replacing all of) my work as a software engineer. But as it stands, I feel like most tech companies don’t share my values in the way that I thought we once did. So I feel stuck, and maybe even a little betrayed.

(Yes, there have always been ethical issues in tech, especially Big Tech, so maybe I was somewhat naive. But somehow this feels like a bigger, more overt shift in values than we’ve seen before.)

Specifications, Not Determinism: Why LLMs Are Not Compilers

There are many legitimate criticisms to be levied at LLMs. The environmental damage, the unauthorized use of mass quantities of intellectual property, the decreased trust in our sources of information, and more are issues we should be deeply concerned about.

However, there’s one problem that I think we’re mis-identifying: the notion that LLMs are nondeterministic.

This most often comes up in the context of code generation. Some LLM enthusiasts will claim that asking an LLM to generate code from a prompt is no different than asking a compiler to generate assembly code from a higher-level language. Therefore, we shouldn’t have to review the LLM’s output. “It’s just another level of abstraction,” they say. “You don’t hand-write your assembly code or review the compiler’s output line-by-line, right?”

The other side is quick to rebut: “But an LLM isn’t deterministic like my compiler is! It might generate different code each time I run the prompt, so how can I know what to expect?”

The sentiment of that rebuttal is right, but the diagnosis is wrong. Something about LLM-based code generation is qualitatively different than traditional compilation. But the problem isn’t nondeterminism. Consider this statement:

A compiler could translate that statement down to assembly code that looks roughly like this:

[/crayon]

But the order in which we evaluate the expressions in parentheses doesn’t matter, right? So a sufficiently smart compiler could decide it’s more efficient to output this code instead:

Same high-level program, two different assembly programs. That’s a nondeterministic transformation. Yet I bet you would accept either one without a second thought.

“But the behavior of the program is still deterministic!”, you say. “Either way, we end up with x = 21.” Ok, then how about this program?

You can’t know which thread is going to execute first ahead of time: that depends on your OS’s thread scheduler. So this program might print “foobar” or “barfoo”. Either one is completely valid.

So a traditional compiler has nondeterminism in both the transformation to assembly code, and in the observable behavior of the resulting program. Yet this still feels different from asking an LLM write a program from a prompt. Why?

This feels different because programming languages and compilers have specifications. A specification is some statement of a program’s behavior that the author guarantees that the program provides. Sure, different language specs may be at different places on the rigor/ambiguity spectrum from “fully formalized, mathematically defined spec” to “whatever the compiler does is the spec”. But in any of those cases, you can reasonably expect specific behavior from those compilers, and you’re justified in filing a bug if the behavior doesn’t match expectations.

LLMs, though, don’t have that kind of specification. If we defined an LLM’s interface as a function “executePrompt” of type “String -> String”, what could the author of executePrompt guarantee to the user? Not much. We can say that probably the output will be something that looks like a plausible response to the input. But even that weak specification can’t be guaranteed to the same extent that we guarantee “(1 + 2) * (3 + 4)” results in 21.

So that’s where we land, and that’s why “prompts are just another level of abstraction” doesn’t make sense to so many engineers. The whole concept of software engineering is built on the idea of abstraction and modularity: programming against a specified interface so that we can separate implementation details from intended behavior. When there is no specification, then there’s nothing to rely on, so we’re left trying to assemble a reliable system from unreliable components.

Is “nondeterminism” versus “specifications” a nitpick about terminology? Yes, a bit. But if we’re trying to figure out how LLMs fit into the practice of software engineering, then I think it behooves us to correctly identify their shortcomings. Only the best possible diagnoses will help us find the best possible resolutions.

So next time you’re talking about LLMs and the ways they might fit into software engineering, remember: nondeterminism per se isn’t the problem. We’ve been wrestling with nondeterminism for decades, and while it’s not easy, we have methods for handling it. But a lack of any kind of specification to describe what we can expect from that non-deterministic output? That’s a big problem, and requires a totally new kind of approach to handle.

Clarifying Meaning with Venn-Diagram Thinking and Concrete Scenarios

Recently, my team was discussing our team mission, strategy, etc. and the question arose whether we should write down our team’s values. My teammate George was skeptical: in reality, when your back is up against it, won’t the decisions you make be based on whatever it takes for your team to survive, rather than some high-minded set of written values? My response was, “What if you replaced our whole team with a brand new set of people and gave them the same mission? Would they make the same decisions we do? If not, doesn’t that say that our values are something distinct from our mission?” You could see the light bulb go off in his head, and he immediately said yes, we should write down our team’s values.

After the meeting, George pointed out that this is a strength of mine: to put an idea into such a clear perspective that what was once a confusing and controversial idea becomes clear and easy to come to consensus on.

This is a skill I picked up in grad school, but I can’t say I learned it explicitly. It was just something that senior folks in the department did, and the rest of us picked it up slowly through osmosis. We gradually learned that being able to put your finger on an important part of a problem was a kind of superpower, and could accelerate progress towards key insights. I distinctly remember when one of the senior professors in the department (Mitch Wand) told me I had asked a really good question during a research talk and I realized I had picked up this superpower too.

On reflecting what this skill even is, I think it’s this: I clarify meaning through Venn-diagram thinking and concrete scenarios.

Let me explain with an example. Suppose I’m a software engineer in a meeting with my team, and the tech lead says “From here out, our focus is on reliability”. Immediately my internal ambiguity alarm starts firing: this is a vague statement and can mean very different things to different people. Should we drop any and all work that is not reliability-focused? Or should we adjust our ratio of work so that, say, 75% of our work is on reliability but we still leave 25% for new features? 

I think of these various interpretations as overlapping sets in a Venn diagram (see figure 1): in many situations, the decision is the same (either way, at least 75% of the work is on reliability), but not always.

Figure 1: Overlapping interpretations of the same statement

So the goal is to figure out which of these overlapping sets they’re referring to: A or B. And the way I do that is to think of a concrete scenario that’s in A, but not in B (see figure 2). So in this situation I might ask the tech lead, “Focusing on reliability can mean a lot of different things. For example, that new feature the product manager just asked for is a top priority request from one of our largest customers, but it has nothing to do with reliability. Are we still going to work on it?”. Once I’ve put my finger on a concrete example of the ambiguity in the statement “our focus is on reliability”, the conversation usually moves on to a productive discussion of what is really meant, so that the whole team ends up sharing the same interpretation.

Figure 2: A concrete scenario in one interpretation but not the other

Once I learned how to apply this skill, I started to see in how many situations it applies: defining team priorities, writing a mission statement, explaining the overarching philosophy in a design, assigning ownership of project components, and more.

A key part of the skill is to detect that there’s some ambiguous meaning to clarify in the first place. Unfortunately, I don’t have any better advice other than to apply the above method in your head as you’re listening to someone describe an idea and determine if something seems missing. See if there are concrete scenarios where a statement might have multiple interpretations, and if so, try to tease out which one is meant.

Even though it wasn’t the direct goal of my PhD, this technique was one of the most important skills I picked up in grad school. But no one ever sat down and explained it to me, so it took a long time to learn. I hope by laying out the process I follow helps others pick up the idea faster. I really do believe this is a big point of leverage for senior folks in tech, so hopefully it’s useful to many of you out there.

Career Mission: Reasoning About Programs

For almost as long as I’ve been programming, I’ve had a gut feeling that writing software should be easier. But I couldn’t name this feeling and didn’t understand it. I couldn’t describe it to others. So for a long time, this vague idea languished and remained as nothing more than a feeling.

Over the years, this feeling has grown into a calling. It’s something I have a hard time not thinking about or working on. Now that I’ve been done with my PhD for a while, I decided it was time to understand this idea and figure out exactly what it was that I wanted to work on.

This post is my attempt to transform that gut feeling into a coherent idea that I can explain to others: specifically, a “mission statement” for my career. There’s no better tool than writing for turning muddled thinking into crisply defined ideas, so that’s what I’m doing here.

Secondarily, I’m also writing this post in the hopes that other people might have tips to share on how to do this kind of work in industry, or pointers to topics I should look into.

My career mission is “to give software developers knowledge and tools to help them reason about their programs”. Read on if you’d like to know what I mean by this, why it’s where I want to focus my life’s work, and what I see as my next steps.

What This Mission Means

Let’s unpack some of the terms in that mission statement.

“Reason About”

First, what exactly do I mean by “reason about”? I mean that a programmer should be able to hold some model of their program in their head that helps them understand how the program works and predict what it will do. This is effectively what is known in science as “predictive power”.

To make this concrete, here are some examples of the kinds of things that help a programmer reason about their program in the above sense.

  • Lexical scope: helps the programmer determine what variables are in scope with just a simple scan of the abstract syntax tree.
  • Structured programming: enables the program to be subdivided hierarchically into “chunks”, so that the programmer can infer facts about individual chunks and then compose those facts into a single fact about the larger whole (i.e. it enables “compositional reasoning” about program chunks).
  • Static type systems (especially sound ones): automatically check the programmer’s intent against what they wrote, ensuring that certain kinds of errors can never happen at run time.
  • Rust’s borrowing system: prevents certain classes of errors not caught by traditional type systems, like use-after-free and data races (with the exception of code marked with “unsafe”).
  • Abstraction and information hiding: enable the programmer to predict how some chunk of code interacts with the rest of the program by reasoning only about that chunk’s interface and specification, rather than its entire implementation.
  • Proof assistants: automate some of the deductive reasoning about programs to help the programmer prove certain theorems about their programs. The theorems might be freedom from a certain kind of error, or a guarantee that the program implements a specification, or anything else the proof assistant’s logic supports.

Conversely, here are some examples of tools and practices that a programmer might use to gain confidence in their program, but don’t enable the kind of reasoning I want to work on.

  • Testing (e.g. unit tests, integration tests, load tests, etc.): tells the programmer whether the program has some desired behavior, but only after executing it. An over-reliance on testing leads to a kind of guess-and-check style of programming, where the programmer writes some code without understanding it, then just runs the test to see what happens. (I’ll talk more below about why I think this is a bad idea.) You see this most often with students just learning to program, but occasionally in professional programmers, too.
  • Runtime verification: checks for certain error conditions while the program is running, but by the definition of “runtime” it can’t predict what will happen.
  • Model checking: predicts whether the program will obey some specification (e.g. the program will never deadlock), but doesn’t necessarily help the programmer understand why that’s the case. If the only possible results from running a model checker are just “yes” or “no” (or “unable to determine”), then relatively little information is gained. In this sense, model checking is similar to testing in terms of the reasoning ability it provides: it can say something is wrong in the program, but it can’t say where the problem is. (Some model checkers provide concrete counterexamples in the “no” case, which might change how I categorize them.)

These examples and counterexamples are separated mostly along static/dynamic lines, but model checking is an exception to this rule. It’s generally considered a static technique, but it doesn’t increase a programmer’s understanding of their program in the way I’m looking for. It might be that what I’m looking for is really static, compositional reasoning, but I need to think through some more examples before I’m convinced that that’s true.

“Knowledge and Tools”

Now that we’ve covered what I mean by “reason about”, let’s talk about the “knowledge and tools” aspect of this mission statement. I wanted to explicitly call those out as distinct concerns because while we know that tools can help a programmer reason about their program, it’s equally important to know that their ability to reason about their program is also determined by how they design and write their program. Some program structures are much easier to reason about than others (consider spaghetti code versus a well-structured program), and some tools work only if the program is written in a certain way. Programmers need to know how the design of a program affects reasoning ability, and that can’t come (exclusively) from a tool: programmers have to know this from the start and use this knowledge while creating their programs.

Concretely for me, listing both “knowledge” and “tools” in my mission statement reminds me that this mission involves not just tool-building, but a certain amount of teaching as well.

Why This Particular Mission?

Software developers need to reason about their programs in order to do any kind of meaningful work. While it’s sometimes possible to write code that “works” without really understanding it (e.g. with the “guess-and-check” approach mentioned in the above bullet about testing), that’s usually a bad idea. Here’s why:

  • Test cases can tell you that your program works for those specific cases, but without a predictive model, you can’t be sure that the program will do the right thing in all cases.
  • If you ever need to modify your program, you’ll have no idea what should be modified unless you understand how it works.
  • When something goes wrong in production (as it inevitably will), you’ll need to understand how the program is supposed to work if you want to have any hope of finding the root cause of the issue.

By default, we try to do this kind of reasoning in our heads. For the really tough problems, we might break out the pencil and paper. That’s how it happened historically, and that’s how most of us do it when starting out today.

But as Fred Brooks wrote in No Silver Bullet in 1987, “software entities are more complex for their size than perhaps any other human construct.” Douglas Crockford made a similar observation in JavaScript: The Good Parts in 2008, saying “computer programs are the most complex things that humans make.” And not only that: programs are getting more complex every year, with the shift from single-machine to distributed programs, programs that have to run on heterogeneous platforms (desktop computer, mobile phone, smart TV), and other advances in modern software.

Human reasoning alone can’t keep up with this rise in complexity. Some people are able to hold more information in their head than others, but we all hit our limit at a certain point. Software complexity will only continue to surpass the ability of the human mind to reason about it.

Computers, on the other hand, are designed precisely for these kinds of heavy-but-mechanical symbol-processing tasks. Even a run-of-the-mill laptop today is orders of magnitude faster and less error-prone at this kind of work than the best human reasoner. We need computers to automate some of this work for us if we want to have any hope of continuing to reason about modern-day software.

The good news is that there is a lot of untapped potential in this space. The last couple of decades have seen a number of advances from formal-methods/programming-language/software-engineering research trickle down into industry, like SAT-solver-based verification and more advanced type systems such as the ones in Rust and Haskell. Other techniques like dependent type systems appear to be becoming more usable, and there seems to be a growing interest in this kind of work from software developers. I expect this trend to continue.

My hope is that in some future year, a programmer will be able to sit down at their desk and write a program in concert with a kind of uber proof assistant. The programmer would set goals and guide the overall direction of the work, while the proof assistant would take care of the mechanical reasoning tasks and answer the programmer’s questions about their code. By offloading the heavy burden of reasoning over to the computer, the programmer’s brain would be free to take on the more creative and intuitive tasks. Thus the human and the machine would complement each other’s skills, leading to an end result that neither could have achieved on their own. I’m heavily inspired in this by J. C. R. Licklider’s Man-Computer Symbiosis and Thomas A. Limoncelli’s Automation Should Be Like Iron Man, Not Ultron.

What Are My Next Steps?

Even with the specific definitions used above, my mission statement is still pretty broad. That’s intentional: I’m trying to identify the kind of work I want to do and think would be valuable to the world, but be able to adapt that purpose to different environments and contexts depending on where I am in my career.

That said, there are a number of general concepts in this area that would be good to know regardless of the specific project I’m working on. Here are a few things I plan to do in the short-ish term:

  • Finish reading The Little Typer and do the exercises, as a way to learn the basics of dependent types.
  • Read (or at least skim) Ringer et al.’s recent survey of proof engineering, QED at Large.
  • Learn about compositional reasoning in general, and specifically how to design programs that are amenable to it. I’m sure there’s a ton of work in this area, but I’m hopeful there are some common/fundamental ideas I should know about (suggestions?).
  • Maybe experiment with a theorem prover like Coq or Agda, since I don’t actually have that experience yet. I’ve heard that Software Foundations and Programming Language Foundations in Agda are both good places to start.

Finally, I’d also like to figure out how to do this kind of work in industry. Academia wasn’t a good environmental fit for me, and I prefer to build useful tools that get into developers’ hands quickly rather than seek out fundamental truths that can take a long time to find their way into practice. But much of my mission sounds more like a classical research program than an industry project, so I’m still figuring out how to actually do this work outside of the typical academic setting. Perhaps there’s room for a kind of tech-transfer role in this area, but I don’t know what that looks like yet or how to make it happen. (Note that I intend to stay at Google for the time being, but who knows what could happen in the longer term?)

Do you have suggestions for additional topics I should learn about? Ideas on how to do this kind of work in an industry setting? Let me know! I’m @SchuCodes on Twitter (I’ll start a thread about this post), or you can just email me at jonathan.p.schuster@gmail.com. Thanks!

Reading Classic Software Engineering Research in Industry: Successes and Challenges

Over the summer, I started a group at my local Google office to read through the papers in Seminal Papers in Software Engineering: The Carnegie Mellon Canonical Collection. This is a collection of about 25 of the most influential papers (and other writings) in software engineering research, curated by the Carnegie Mellon software engineering faculty. The goal of the reading group is to get a sense for what the academic side of software engineering can teach us practitioners and what universal truths we can extract and use in our work. (As a side benefit, it’s also been a great forcing function for me to read more papers in my post-academic life.)

Having run the group for half a year now, this seemed like a good time to stop and reflect on how it has gone so far. (Disclaimer: what follows are my opinions alone; not necessarily the opinions of the group.)

How It Works

Every month we read the next paper in the list ahead of time, then we meet to discuss it. The reading order is based on this Twitter conversation with Jonathan Aldrich, which is roughly the order he and others use when they teach a class based on this list. To add some variety, every 6 months the group picks a paper not in the CMU collection (preferably one with an author from an under-represented group) and discusses that instead.

To help jump-start the discussion, we started posting our questions about the papers ahead of time on an internal tool called Dory. Dory lets everyone up-vote or down-vote each question, so it provides an aggregate signal for what the group would like to discuss. If the conversation during the meeting comes to a lull, we can use some of the top questions from Dory to keep things going.

What Has Gone Well

  • Posting questions on Dory ahead of time has helped give a collective sense ahead of time of our general impression of the paper and where we got confused. It has also helped structure and guide the conversation overall.
  • Reading these papers has gotten me thinking more about some fundamental questions in software development, like how to better divide programs into chunks and create clean abstraction boundaries between them. This probably affects my day-to-day work more than I realize, at least at a subconscious level.
  • I’ve been exposed to a new paper (Dilemmas in a general theory of planning) that I really like. It introduces the notion of a “wicked problem”: basically a social problem that can’t easily be solved by simple math and engineering optimizations. It touches on a lot of ideas, such as how do we even define success for such an issue, how do we get feedback while working on an ambiguous problem, and how do we make plans in these situations. I’d like to read it at least one more time to really wrap my head around it.

Challenges So Far

  • We usually don’t know much historical context around each paper, and without that it’s sometimes difficult to understand what makes that paper significant. For example, in the Levels and Abstraction chapter from Siewiorek et al.’s Computer Structures, we struggled to extract a more meaningful message than “organizing your system into a hierarchy helps with abstraction”. Was that less obvious to a developer back then than it is today? Or did we perhaps miss some more subtle and insightful message in that reading? The editors’ notes in the CMU tech report help with this a little, but without someone more well-versed in the area guiding us through, it’s hard to get more than a shallow reading of some papers.
  • Similarly, it’s sometimes hard to see why a paper might be important to us as working software engineers, rather than to researchers in software engineering. And maybe sometimes it’s not relevant at all: after all, this list was originally intended for introducing new researchers to the field, so maybe sometimes it’s not! Tony Hoare’s Proofs of correctness of data representations was one paper where we had this difficulty. While you can generalize some of the principles there to reasoning about correctness in general, none of us in the group use formal methods day-to-day (aside from automated tools that hide the details), and we never have to do something this rigorous. Without a clear value proposition to working developers, it was hard to muster the motivation to wade through all of the formalisms in that paper
  • Our group-chosen paper, Margaret Hamilton’s Universal Systems Language: Lessons Learned From Apollo turned out to be something of a dud for us. Partially this was because of the two previous points, but it also just seemed like a poorly written paper that couldn’t even explain what kind of thing the Universal Systems Language was (a programming language? A specification language? Something else?). While we want to add some variety outside of the CMU-curated list of papers, it’s also tricky for us to select what might be a good paper ahead of time without knowing the space better.
  • Less significantly, some of the readings are hard to find from a legitimate source. For “Proofs of correctness of data representations” we eventually found it in a compilation of Hoare’s papers, and we ended up skipping a couple of book chapters since it didn’t make sense to have us each buy multiple textbooks just to read a chapter or two from each. I don’t think this will have a very negative effect in the end, since we can still find most of the readings, but it’s unfortunate that we might miss out on a few of them.

Conclusion

All in all, I’m happy that running this group is forcing me to read more research papers, and I’m glad that it’s exposing us to some new ideas. But at the same time, I’d like to figure out a way to extract more value and find deeper insights from these papers than we are right now.

I wonder whether tips for running Great Books programs could help here. I’ve never been through a Great Books program myself, but this feels similar in that we’re studying the original sources for big ideas and trying to wrap our heads around them. Advice for people who go through a Great Books program without a more experienced guide might be relevant.

If you have suggestions, I’m happy to hear them!

P.S. to Googlers: I’m planning to open this group up to people outside the US-CAM office. If you’re interested, check out go/cam-se-research-reading-group.

Highlights from Curry On 2019

I had the privilege to attend Curry On in London earlier this week, and it was a great conference. The idea of the conference is to get people from both academia and industry together to meet and talk about programming languages and related technologies.

For those who weren’t able to attend, I thought I’d summarize my favorite talks. You can also watch recordings. I link to the videos of each of my highlighted talks below.

Simon Marlow: Glean: Facts About Code

(video)

Simon Marlow (of Haskell fame) talked about the system Facebook is building to host a repository of information about their code, called Glean. The idea is to put all information about the code into one place: the code itself, type/static analysis information, test results, bugs, and more.

The central organizing idea of Glean is that it stores “facts” about the code. Whenever the codebase changes (e.g. someone commits a change), new facts are computed, and those new facts are stored as “diffs” against the old facts (this reminds me a lot of how I understand git works). Thus, Glean is an incremental database.

They’re currently using Glean at Facebook for dead-code detection, code navigation, and other miscellaneous IDE features. It sounds, however, like they want to push this idea to the limits and see how much value they can get out of making a centralized knowledge base for their code. I personally think our current tools don’t do nearly enough for us in terms of helping us understand our code, so I’m curious to see what comes out of this in the future.

Frank Luan and Celeste Barnaby: Using ML for Code Discovery at Facebook

(video)

Frank and Celeste presented on some of the ways Facebook is using machine learning to give developers more information about their code (and once again I’m happy to see more people working on this). My major takeaway was about their “neural search” work. The idea is that a process crawls over the entire Facebook codebase and records all “words” found in the code (where a “word” can either be a word found in comments, or a part of a variable or function name). Then after passing that information through some sort of neural network, they have a system that can determine what snippets of code are likely relevant to what words (even words that might not appear around that snippet, just based on related words found elsewhere in the codebase).

In the end, they have an automated system that can respond to Stack-Overflow-like queries like “how do I open a TCP socket?” with a relevant snippet of code. From what I recall, they said the code snippet is relevant about half the time. So that sounds like something you wouldn’t want to rely on exclusively, but I could see such a system complementing the human-sourced answers in Stack Overflow with automated answers of its own.

Edwin Brady: Idris 2: Type-Driven Development of Idris

(video)

I’ve heard Edwin’s talks are great, but this was my first chance to see one. He didn’t disappoint. This is a guy who clearly just really enjoys working on this stuff, and that energy and sense of fun really comes across in his talks.

The talk at Curry On was about his Idris 2, the successor to the dependently-typed Idris. The big feature he focused on was type-driven code synthesis. So if you have a function that takes as input some argument of an algebraic data type (say, a List type with the usual Cons/Nil definition), the underlying Idris 2 system can generate the case-split for you, then search for an expression that fits the expected return type. In certain cases, depending on the type, that resulting code might be exactly what you want.

According to Edwin, this pattern of case-split-then-search-for-code is quite common. He’d like to see if he can automate much of this process into a kind of “programmable tactics” for writing code. Hard to say if that would go anywhere or not at this point, but it’s an intriguing idea that sounds valuable if it works out.

He admits that the programs he’s demoing are completely toy programs, so who knows if this approach would work for programming larger programs, or even moderately sized ones. It was a really fun talk, though, and one that gave me yet another good reason to learn about dependently typed programming.

Adelbert Chang: Microservice Architecture: A Programming Languages Perspective

(video)

Adelbert started off by talking about the kinds of problems one must handle when deploying and managing services in a microservice architecture. For example, how do you make sure all of the dependencies for your service are ready to go when you deploy your service? How do you handle service maintenance, or network partitions? How do you update a service’s message format without breaking the services that rely on the previous format?

The solution he’s been using is a project called Nelson, which is a kind of deployment management layer/runtime for your whole microservice system. Whenever you bring up your system, Nelson knows about the dependencies between different services and can bring them up in the right order. Similarly, when you want to upgrade a service, it can bring the service dependencies up/down in the proper order without the programmer having to think about it. If a service dependency is no longer being used, the management layer notices and shuts it down automatically.

Adelbert pointed out that much of this system is similar to what you get from a language runtime like the JVM. For instance, the part about removing unused services is just like garbage collection. The philosophy here as I understand it (and which I agree with) is that we can get a lot of leverage by trying to treat the entire “system” as one big program and using analogues techniques from the language-implementation world like garbage collection, packages, and type-checking.

Adelbert is currently looking into interface description languages, or “IDLs”, to add better notions of type-checking between services to these systems, including subtyping (which is crucial for upgrading services). He also mentioned a paper Verifying Interfaces Between Container-Based Components based on some of these ideas, and he’s working with some of the authors of that paper to take the ideas further.

I’m really excited about this space. Reasoning about distributed systems using techniques from programming languages was the general theme of my dissertation, so I’m really glad to see other people working on these problems. I’m excited to see what they’ll come out with next.

Lin Clark & Till Schneidereit: Bringing WebAssembly Outside the Web with WASI

(video)

Lin and Till capped off the first day with a keynote on the WebAssembly System Interface, or WASI. The thesis here is that two of the biggest benefits of WebAssembly are portability and the sandboxed security model. Those benefits aren’t restricted to the web alone, though: desktop applications can benefit, too. This is the idea of WASI: it’s a simple interface for running WebAssembly programs outside the context of a web browser and allowing supervised access to operating-system components like the file system or network connections.

I’m especially glad to see the object capability model taking hold in this space. For those not familiar, the idea of object capabilities is that to access something like, say, the file system, you must have some capability token that says gives you access. Typically, the user running the system grants the program certain capabilities (much like the permissions to run an app on an Android phone), and then the program has to hand off those tokens to any dependencies that need to access the relevant systems. This avoids the “ambient authority” problem where a malicious component can gain access to systems it shouldn’t have access to, only because it’s running as a part of a program running in the context of some particular user.

One slogan I found useful was that we should be able to “read the label” on a piece of software in order to know what it does. Today typically the only way of truly knowing what a program might try to do is to read the code, which doesn’t scale. This nutrition-label analogy sounds like a good approach and gets the idea across.

Aside from the technical content of the talk, I continue to be astounded by Lin’s skill at conveying complex technical information. Having spent much of the last two years writing about my research and having put together several different research/technical talks during grad school, I have a deep understanding of how difficult it can be to find a way to communicate the ideas you might have in your head in a simple way. Lin’s slides are beautifully simple and tell the story using a narrative and “characters” that just about anyone can connect to. She’ll be a role model for me the next time I have to give a talk, and I recommend everyone check out more of her cartoons on web technology at Mozilla Hacks.

Cynthia Solomon: Logo, A Computer Language to Grow With

(video)

Cynthia started off day 2 with a fascinating keynote on the language Logo and some of the works it went on to inspire. A large part of the idea of designing Logo (and the curriculum around it) was to focus on the general underlying ideas of computing and ways to teach them rather than surface issues of a particular language: things like procedural thinking, debugging, project-based learning, talking about what you are doing, having objects to play with, and anthropomorphic thinking. Logo was not just a programming language, but a culture: a way of thinking about computers and learning how to talk about what you’re thinking about.

If you haven’t heard of Logo before, the general idea is that you control a little robotic “turtle” and tell it to walk forward, turn, walk backwards, etc., while also giving commands to raise or lower a pen so that the turtle is drawing images as it goes. What I had never realized, but I suppose should have been obvious in retrospect, was that the turtle was an actual, physical object! Cynthia showed videos of the turtle, which looked like a big upside-down bucket on wheels, drawing a picture on a piece of paper while the students who wrote the program watched their results come to life. I had always assumed the turtle was just a metaphor and/or something you saw on-screen in a simulator, so it was literally a revelation to see videos of the actual turtle running around.

Cynthia went through a lot of the history around Logo, both before and after. I don’t have time to recap it all, but I recommend watching the video if you’re interested. Some of the projects that Logo went on to inspire were a “Lego Logo” robotics system from Lego (this must have either been Mindstorms or some precursor), ButtonBox which gave 3-year-olds a board of buttons to use to program a turtle robot, and the popular learn-to-program language Scratch.

Tomas Petricek: The Search for Fundamental Software Engineering Principles

(video)

Spoiler alert: this was my favorite talk of all of Curry On. Tomas asked the question “what should we be teaching students about software engineering that will still be relevant in 100 years?”. It’s a hard question. In computer science, we at least have some fundamental ideas of computing like Turing machines, the halting problem, and Big-O analysis that underpin much of the field, but when it comes to actually doing something with that knowledge (i.e. software engineering), we lack the same kind of fundamental ideas. In fact, it’s hard to say anything concrete about a piece of software at all: how long will it take to build? How much will it cost? Will it produce the right answers? If not, how often will it be wrong? Can it crash? Most of these are not “formal” questions in the sense that questions about science or math are formal, so they’re much harder to answer definitively.

As a result, to the extent that we teach students anything in school about software engineering, it tends to be based in trends that may or may not be around in 20-30 years (e.g. design patterns, tools like git, the agile methodology, language peculiarities, etc.). I would quibble a little with this and say that many schools at least try to teach some more fundamental principles of ideas, but since none of us really know what those are, about the best we can do is teach what we have found from experience to be “best practices” in industry.

Tomas’ idea is that, to start, we should start by studying the context in which many of these best practices developed. In many cases, the decisions made sense only given that context, so we should try to understand that context before adapting them to our own situation.

Tomas then got into the complexities of software, and where they come from. An insightful question he presented was “which is more complex: building a chess program that can defeat a grandmaster, or writing an accounting system that can calculate VAT in the UK and France?”. Research often focuses on the former, because although it’s a hard problem it’s easier to specify, while the latter is more common in our day-to-day work. Much of the work that goes into software engineering is about managing this complexity, and finding ways to mitigate the risk it entails. My overall sense was that Tomas sees the way forward as finding ways to tame this complexity and risk to a manageable point, rather than completely eliminating it. He seems to believe (and I’m generally convinced) that software engineering will likely never be a problem that can be cleanly defined like the principles of computer science mentioned above, but we can study what kinds of issues happen in the aggregate (e.g. broken deadlines, security breaches) and find ways to deal with them.

Overall, I’d say his thesis was that we should be studying software engineering more like a social science than a physical one, and using the techniques from those fields. In particular, he emphasizes understanding the history and trying to find useful metaphors from other fields like urban planning.

On reflecting on this later (and talking to Tomas after the talk), I wondered if we do yet have any software engineering principles that will always be relevant. My thought was that abstraction is such a principle. Time and again, over several generations of programming languages, we see abstraction as the key mechanism for reasoning about software without understanding every tiny detail. Abstraction is what allows software to scale without collapsing in on itself, so I believe we’ll still be teaching about abstraction 100 years from today.

All in all, this talk was near and dear to my heart. I’ve long felt that teaching people to be software engineers by having them major in computer science is like asking mechanical engineers to major in physics. Yes, you need to understand the underlying science to do your work, but the work requires a much better knowledge of how to apply the ideas of that science in practice, and what can go wrong in the real world. I hope more researchers follow Tomas’ lead and come up with better answers for us.

Tudor Girba: Moldable Development

(video)

Tudor gave a description and demo of the Glamorous Toolkit that he and his team use for what he calls “moldable development”. The idea is that as software engineers, much of our time is spent not on the writing of code per se, but trying to understand what has already been written. The only method most development tools provide to do that is by reading the code, line-by-line. According to Tudor, this doesn’t scale. Instead, we need a moldable development environment: one that allows us to write the tools we need to understand our particular programs in a quick and easy way.

Tudor showed several examples of this, such as a color picker for visualizing different colors, a tree diagram, and a graph + arrows diagram for visualizing the flow of messages. Key to all of this is that it’s easy and lightweight to create all of these tools. The environment is built on top of a Smalltalk environment, which is built with this kind of idea in mind. The video of Tudor’s talk gives a much better idea of the kinds of tools and development methodology he’s proposing than I can describe in words alone.

I like the idea, but I’m skeptical that this approach can really make the understanding of code that much faster. Most of his demos seemed to be about visualizing data, but when I’m trying to understand a piece of code, I find that it’s usually the algorithm that’s hard to understand. Algorithms don’t usually have an obvious visual representation in the way data does, so I’m not convinced the moldable approach works there. That said, (a) I’d be happy to be proven wrong, and (b) this idea would still be great for visualizing lots of different kinds of data.

Alastair Donaldson: GraphicsFuzz: Metamorphic Testing for Graphics Shader Compilers

(video)

Alastair gave a talk on metamorphic testing, which was a term I had heard before but never knew what it was. It starts with the problem that it’s often difficult to come up with an extensive list of test cases for something like a compiler, especially if you’re really trying to eliminate every bug. Metamorphic testing offers a complementary approach. Instead of trying to come up with test programs and expected results manually, start with some known program, then modify it so that it should produce an “equivalent” result. This might be something like wrapping a block of code with an “if” test that’s always true, or expanding a number to some expression that computes that number. If you compile and run both the original program and the mutated one, and they produce different results, then you have a bug in your compiler.

The benefit of this approach is that it’s easy to generate lots of such “equivalent” programs randomly, so a computer can easily come up with a large number of test cases to stress test your compiler. Alastair and his team used this approach to test shader compilers for Android from a number of different vendors, and they found many bugs using this approach.

I liked that this approach offers an easy way to explore many corner cases of programs that manual generation of test cases would otherwise overlook. The approach isn’t necessarily applicable to all scenarios, and I don’t know that I have a way to use it in my work today, but it’s a tool worth keeping in my testing toolbox.

Nate Foster: Hyperproperties

This was actually a talk at the associated Papers We Love event the day before the conference itself started. Nate gave a great talk on a relatively recent (2010) idea called “hyperproperties”. The idea is generalization of the notion of “properties” from the world of model checking. If you’ve heard of safety properties and liveness properties before, then you’ve got the right idea.

Suppose we can model the execution of some process as a (possibly infinite) set of states. Think of each state like the state your program is in when you run it in a debugger and click the “next step” button. I’ll call this state-sequence a “trace”. We might want to specify what it means for a trace to be “good”: e.g., whether it always avoids deadlock between two threads, or whether every incoming request is eventually processed. For every propery like that we might come up with, there is a corresponding set of traces that obey that have that property. Thus, a trace is “good” if it’s in the set described by that property, and so the set of traces effectively is the property.

Researchers have studied this framework for describing properties for the last several decades, and it has proved useful for verifying certain kinds of program behavior. In particular, the most commonly used kinds of properties are safety properties that say that some particular bad thing never happens (e.g. “this program will never crash”), and liveness properties that say that some particular good thing always happens (e.g. “Function foo always terminates”).

Some things we’d like to say about our programs are not expressible in liveness properties, though. For example, maybe you have a latency requirement that 99% of
all requests are handled in 100 ms. We can’t say whether a given trace satisfies the requirement. Instead, this kind of behavior is defined in terms of a set of traces. This is the idea of hyperproperties: a hyperproperty is defined as the set of all sets of traces that have some kind of behavior.

This work is still in a relatively early stage. Most of the existing work, as I understand it, is around formalizing this idea, coming up with theories around hyperproperties, and thinking about how they might be used. For example, there’s work in the world of properties that says that every property is the intersection of a safety and a liveness property, and it turns out that a similar result holds for hyperproperties.

I’m curious to explore hyperproperties further and see whether there might be interesting applications, though. I’m working at Google now, and a big concern in the distributed system I work on is the SLOs (“service level objectives”) that have a similar flavor to the 99% latency requirement above. Perhaps there could be some value in thinking of these SLOs as hyperproperties, although it could equally be the case that the work is still too young to be applicable in such a scenario.

Conclusion

I had a great time at Curry On, as well as the colocated Papers We Love event. The conference had the perfect mix of academic and industry talks and participants, and I saw and heard a lot about how to make software engineering better. We may not have solved every issue, but I hope there are many more events like this in the future so those of us who care deeply about these issues can get together, talk, and try to improve the situation a little bit at a time.

What is Type Soundness?

UPDATE (26 March 2017): In class, most people said the below example is not an example of unsoundness because the program will throw a well-known exception rather than allow undefined behavior. Whether this is sound or not may depend on your point of view, as Cornell professor Ross Tate explains, although the short version is that the Java designers generally consider this sound. I may discuss the definition of soundness we used in class and explore this apparent gap between “the type system guarantees what you expect from the annotations” and “no undefined behavior occurs” in a future post, but for now I recommend waiting for a summary of our class discussion on the PRL blog.

As part of this semester’s History of Programming Languages course at Northeastern, Matthias tasked the students (and people like me who just sit in from time to time) to figure out how they would explain type soundness to the typical software developer who’s not familiar with programming languages research or formal semantics. The in-class discussion is tomorrow, so watch the PRL blog for a summary of the full class discussion. In the meantime, here’s what I came up with:

Type soundness is a formal means of proving that your language’s type system won’t “lie” to you. For example of what I mean by lying, take the following example Java snippet:

This snippet type-checks, but it appears to do something weird: line 3 inserts an integer into an array, but when we retrieve that item from the array, somehow we can magically treat it as a string! That shouldn’t happen: the whole point of the type system should ensure that if we have a variable of type String, then it should definitely be a string.

It turns out that at run-time, when an item is inserted into an array, Java will check that the type of the item matches the actual type of the array and throw an ArrayStoreException if they don’t match.

This is an example of how Java’s type system is unsound: the type-checker confirms that “objects” is an array of Objects (into which we expect to be able to store any kind of object), but in reality it can only store strings.

A sound type system is one that guarantees a type-related error will not occur if the program type-checks. Exactly what a “type-related error” is depends on the particular language, but generally it means applying an operation to the wrong kind of arguments, such as inserting an integer into an array of strings, or concatenating a boolean onto a string.

Sound type systems reduce the amount of reasoning programmers have to do by ensuring that certain kinds of executions will never happen. An unsound type system, on the other hand, helps find bugs, but programmer can’t rely on the type-checker to prove that no type-related bugs exist in their program.

Getting Started in Programming Languages

A student in Northeastern’s Fundamentals of Computing course, in which I’m a TA, recently asked me how to learn what the “state of the art” is in the study of programming languages. It’s a long road from Fundamentals to reading and understanding today’s research, but this post is my answer to how to start down that path.

First, a distinction: to study programming languages, many people will point you to a book on compilers or interpreters. While those comprise a large fraction of programming languages, they are only a piece of the puzzle. The heart of (the study of) programming languages is formal semantics: the idea that one can mathematically specify the meaning of expressions and constructs in a programming language to avoid the ambiguity that otherwise arises from plain-English descriptions. Equipped with a formal semantics, one can develop multiple techniques to run programs (compilers, interpreters, hybrid approaches like JITs, etc.), new kinds of optimizations, and manual and automatic techniques to find bugs or prove properties of programs, all while remaining faithful to the intended definition of the programming language. I recommend first learning how to build and run your own language, then moving on to learning formal semantics to get a solid foundation for understanding current research.

With that in mind, here are some resources for the new (or not so new) computer scientist wishing to learn more about programming languages:

Essentials of Programming Languages: EOPL, as it’s better known, introduces readers to the internal workings of programming languages by describing small programming languages and creating an interpreter for each one. The book is very hands-on, with lots of exercises for the reader to modify the interpreters with new features. It touches on the ideas of reasoning about languages and formal semantics, but mostly sticks to the interpreter-as-semantics approach.

Jonathan Turner’s Reading List: Turner is an engineer on Mozilla’s Rust team and recently posted his reading list for getting up-to-speed on programming languages. The list starts with some resources on how to build interpreters and compilers, but also points to more academic material later.

Types and Programming Languages: TAPL (rhymes with “apple”), as it’s better known, has a solid introduction to formal semantics in the first few chapters and would be my pick for a starting point on formal semantics. The remainder of the book deals with type systems, which form only one part of programming languages, but it’s the canonical reference if you’re looking to learn about types.

Semantics Engineering with PLT Redex: The PhD-level programming languages course here at Northeastern uses the Redex book, and I found it to be a good introduction. The tool itself (Redex) is a great way to experiment with semantics, including reduction relations (roughly, the part of the semantics that says how the program runs) and type systems. You could use this book as a substitute for TAPL (at least for learning the basics of formal semantics), or you could use Redex to experiment with the languages described in TAPL.

10PL: This is a list compiled by Northeastern’s PL faculty of (roughly) ten academic papers that they think every student in PL should know. Not all of them are PL papers themselves, and they don’t form a full foundation on their own, but they form a kind of “great books” list for PL. Benjamin Pierce, the author of TAPL, also has a similar list (although with a slightly more type-heavy and theoretical bent).

That list is more than enough to get you started. I omitted resources for learning about formal methods and software engineering, two fields that overlap heavily with PL, but you may be interested in learning about them, too. For more information, I recommend talking to students or faculty in PL at your school, joining (or starting) a PL reading group, or eventually even applying to grad school if you’re so inclined.

Good luck!

Lifetime Parameters in Rust

Not long ago, when I first started working with Rust, lifetime parameters were one of the trickiest aspects to wrap my head around. What was this strange <'a> thing I saw at the beginning of some function and struct definitions, and what was it for?

I’m writing this post to solidify my own understanding of this unfamiliar feature, and hopefully it helps you, too. In it, I’ll cover:

  • the problem lifetime parameters solve
  • the places in Rust where lifetime parameters can be used, and what each location represents
  • the use of lifetimes as bounds on both generic type parameters and other lifetime parameters
  • the 'static lifetime
  • lifetime elision (a set of rules that allow you to omit explicit lifetime parameters in all but the most ambiguous cases)

I assume the reader is familiar with the basics of ownership, borrowing, and references in Rust, as well as generic types; if not, the book is an excellent reference.

Let’s get started!

The Problem

Imagine that we have a hypothetical version of Rust without lifetimes on reference types, and we try to run this program:

At line 15, x would be assigned a reference to memory owned by the coordinate c (through the call to get_x). But at line 16, c would go out of scope, so its memory would be freed. That means that at line 18, x would point to invalid memory—a classic use-after-free error.

So why didn’t the compiler catch this? The problem is that without any extra information on the type signature for get_x, the compiler doesn’t know that the returned reference’s lifetime should be the same as the input reference’s. We could have, say, written a version of get_x that returns a reference with a completely different lifetime, like a reference to a constant.

A similar issue can occur with references stored inside structs. Consider if a similar struct that instead stored references to its x and y fields and we tried the following:

Again, running this program would cause a use-after-free error. In this case, example_x and example_y are freed when the block ends at line 13, so c.x at line 15 would reference a freed location in memory. For the compiler to catch this error before we run the program, we need to establish some sort of relationship between the lifetime of a RefCoordinate and of the references it contains.

As you’ve probably already guessed, lifetime parameters solve both problems.

The Basics of Explicit Lifetimes and Lifetime Parameters

Although they are not always made explicit, every reference type in Rust has an associated lifetime describing the scope in which the reference can be used.

In cases like the above, we do want to make the lifetimes explicit so that we can express, for example, that the reference returned by get_x has the same lifetime as the input argument, c . That would give the compiler enough information to know that the use of x at line 18 is invalid.

Rust does not provide a way to write down concrete lifetimes (e.g. there’s no way to express “the lifetime starting at line 13 and ending at line 16”), but typically that’s not what we want, anyway. Instead, we want to abstract over all possible lifetimes; this is what lifetime parameters provide.

Lifetime parameters are special generic parameters on items like functions and structs, much like generic type parameters. The parameters provide names for our lifetimes that can be used as explicit lifetimes in our reference types. To make this concrete, here is the program after adding the proper lifetime parameters to get_x:

Let’s break this down one step at a time. In general, 'a means “the lifetime a”, and &'a T is the type for a reference of type T with lifetime 'a (or &'a mut T for a mutable reference).

get_x has a single lifetime parameter, 'a, declared immediately after the name. c’s declared type says that it is a reference whose lifetime is 'a. This declaration does not change c’s lifetime; it only gives it a name so we can refer to it later. Finally, the return type &'a i32 says that get_x returns a reference to an i32, whose lifetime is the same as the input argument’s lifetime. In other words, from the caller’s perspective the returned reference is only usable as long as the input c is still in scope. With this information (and replacing our hypothetical, lifetime-ignoring Rust with the real thing), the compiler is able to report that our use of get_x is invalid, because c does not live long enough.

We could write the code like this instead to get a working program:

Structs work similarly, as seen in this lifetime-ified example of RefCoordinate :

The struct Coordinate is now parameterized over a lifetime 'a, and its references are each declared to have that same lifetime 'a. In effect, this expresses the constraint that any reference to the struct may not outlive its contained references.

Lifetimes on Other Items

Traits can also have lifetime parameters, with the same syntax and meaning as structs:

Finally, when implementing methods in an impl block, the block itself can declare lifetime parameters to be used in the named struct or trait:

In general, lifetime parameters are declared as generic parameters on items like functions, structs, traits, and impls, and they are used both in reference types and as arguments to items that take generic parameters (as with GetXRef and RefCoordinate in the above example).

Bounded Lifetimes, Lifetimes as Bounds

If you’re familiar with advanced uses of generic types, you know that they can have bounds, as in fn foo<T: S>(x: T). Here the provided T must be a type that implements S.

Similarly, lifetimes can act as bounds to other lifetimes. A parameter like <'a: 'b> means “I expect some lifetime 'a that is at least as long as 'b”.

Addtionally, lifetimes can act as bounds on types. A type parameter such as <T: 'a> means “I expect some type T such that all references in T have a lifetime at least as long as 'a”.

Bounds in both cases can be combined using the + operator just as with normal type bounds.

The ‘static Lifetime

There is one particular lifetime in Rust that can be named without abstracting over it: the static lifetime, 'static. This is the lifetime for references that are alive throughout the entire execution of a program, such as references to string literals and global constants.

Lifetime Elision

Lifetime parameters provide the compiler with the extra information it requires to prevent use-after-free issues, but writing explicit lifetimes for every single reference type can be cumbersome, especially for obvious cases like this:

We would rather write the following and have the compiler figure out where to insert the lifetime annotations:

Fortunately, Rust has a simple set of rules, called the elision rules, that allow the programmer to elide (leave out) lifetime parameters in functions in the obvious cases (structs, however, must always use explicit lifetimes for their fields). The rules refer to input and output lifetimes, which simply refer to the lifetimes used in input types or return types, respectively. The rules are (as quoted from the book):

  • Each elided lifetime in a function’s arguments becomes a distinct lifetime parameter.
  • If there is exactly one input lifetime, elided or not, that lifetime is assigned to all elided lifetimes in the return values of that function.
  • If there are multiple input lifetimes, but one of them is &self or &mut self, the lifetime of self is assigned to all elided output lifetimes.

Eliding any other output lifetimes is a compile-time error. The book has many more examples to help you understand the elision rules.

Summary

  • Lifetime parameters abstract over the lifetimes used in type annotations and allow the programmer to express relationships between lifetimes.
  • Lifetime parameters are declared as generic type parameters to functions, structs, traits, and impl blocks and can be used in reference types, struct types, and trait uses. They can also act as bounds on type and lifetime parameters.
  • The 'static lifetime extends over the whole program and is used for constants and string literals.
  • A simple set of lifetime elision rules allows programmers to omit explicit lifetimes in most cases.

Hopefully this has helped clear up one of the more confusing aspects of Rust’s type system. If you have questions I didn’t answer, please sound off in the comments. Happy hacking!