The idea that language is a homogeneous code is a massive simplification. In actual fact, we constantly use a wide array of codes, be they other languages, dialects, registers, or special purpose codes (for bank account numbers, book numbers, shoe sizes, and so on). In this paper we provide a formal analysis of code switching.