Computer Sciences Dept.

Code Sandwiches

Matt Elder, Steve Jackson, and Ben Liblit

A code sandwich consists of before<\em>, after<\em>, and body<\em> code. Typically, the before<\em> code makes some change, the body<\em> code works in the context of the change, and the after<\em> code undoes the change. A code sandwich must guarantee that its after<\em> code will execute if its before<\em> code has executed, even if exceptions arise. This pattern is common to many programming situations, and most modern languages have some language-level support for expressing it.

We survey support for code sandwiches in several programming languages and proposed language extensions. We explain why such support can improve a program, consider related features, and discuss desirable properties that a language can provide its programmers. We relate these properties to Jyro, our code sandwich extension to Java. We examine two large, mature open-source programs, find numerous sandwich-related bugs and readability issues, and demonstrate how they might be avoided using our Jyro implementation.

Download this report (PDF)

Return to tech report index

Computer Science | UW Home