I think I first heard the term “Air Code” on DNR,
although I’m not sure exactly who said it (Mark Miller maybe? Rory
Blyth?) but I think it’s a valuable and often overlooked as
a tool for communicating programming concepts.
What is Air Code?
Air Code n. A bit of software code, written in a particular language, compiled using an imaginary compiler.
This past week, I asked Sahil a question, and he replied with the following snippet of code, to illustrate a point he was making:
partial class Program{
static void Main(string[] args) {
Man Brendan = new Man();
Console.WriteLine(Brendan.MakeSound());
Type t = Brendan.GetType();
Console.WriteLine(t.BaseType.ToString());
}
}
public class Monkey {
public string MakeSound() {
return ("RRR !!! RRR!!!");
}
}
class Man : Monkey {
public new string MakeSound() {
return ("How do I find the base class?");
}
}
The point of Air Code isn’t to write code that you expect will
actually compile – it’s useful when you want to communicate
how to do something in a particular language, but don’t want to worry
too much about syntax. I mean, we all use ReSharper or CodeRush
anyway, right? We’re going to know if the code has errors way
before we hit that compile button, so why waste time compiling
code? Actually, I think Sahil’s code will compile (that’s just in his nature), but even if it didn’t it would still be good Air Code.
How is Air Code different from pseudocode?
Pseudocode: n. a detailed yet readable
description of what a computer program or algorithm must do, expressed
in a formally-styled natural language rather than in a programming
language.
Air Code is often actually written in a
particular language and it’s intentionally not formally
styled. Its main purpose is really to say “Here’s how
to do this, but hey, I only compiled this with my air compiler, so
don’t hold me accountable if there are
errors.” I think it’s a useful concept, because it
allows you to quickly communicate an idea without worrying about
someone actually trying to compile it, correct you in comments, or then
email you, saying, “My compiler is saying that it can’t find the
Sytsem.XML namespace!”
Are there any formal conventions to indicate that code is actually Air Code?
There are now! I’m proposing the [AirCode] attribute to
indicate that you shouldn't actually do anything as silly as try to
compile the code you’re seeing. So if if you see this
attribute on my blog anywhere, it means, that the code probably won’t
compile. So go ahead and email that code! Blog
it! Don’t compile, use Air Code! You’ll feel liberated!
-Brendan