CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

What is Air Code, and What's its Purpose?

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



Comments

Geoff Appleby said:

Without knowing the actual terminology, I've been using aircode for a long time :) Hell, aircode _is_ my pseudo code!

That's not a bad idea however, marking stuff with an aircode attribute. in fact, why not take it one step further - write yourself an article (what a goo dexcuse to start :) - that is, put it in the articles section - with the code listing describing the aircodeattribute class. then whenever you use the aircode attribute, hyperlink it to that page so that people can find out what the hell it means.

Hell, i'm bored, here you go *laughs* (Deliberately written in VB to confuse people, since you'll likely write C# examples :P

''' -----------------------------------------------------------------------------
''' <summary>
''' Signifies that the following code is for example purposes, and likely doesn't compile
''' </summary>
''' <remarks>
''' Copyright CodeBetter.Com via DNR
''' </remarks>
''' -----------------------------------------------------------------------------
<AttributeUsage(AttributeTargets.All, AllowMultiple:=False)> _
Public Class AirCodeAttribute
Inherits System.Attribute

Public Overrides Function ToString() As String
Dim oBuilder As New System.Text.StringBuilder
oBuilder.AppendFormat("This is the AirCodeAttribute.{0}", vbCrLf)
oBuilder.AppendFormat("Use of this attribute indicates that the code it is applied to has not been tested and likely won't compile. {0}", vbCrLf)
oBuilder.Append("It marks a section of code that is for example only.")
Return oBuilder.ToString
End Function
End Class
# February 27, 2005 7:44 PM

Geoff Appleby said:

Ooh, throw a couple of code errors into it and make sure that the code listing for the aircodeattribute is itself marked with aircode attribute!!!!

Attribute recursion! W00t!
# February 27, 2005 7:46 PM

Sahil Malik said:

LOL, you just had to blog about that code :).

SnippetCompiler works for .NET 2.0 now, so one neat thing about such quick write'n'forget code is - it's much easier to do now.

BTW, I find partial classes incredibly useful in writing aircode.

Man, why can't the whole world move to .NET 2.0.
# February 27, 2005 9:36 PM

Raymond Lewallen said:

Because some of us work for the gov't and they are slow to move, and certainly won't authorize moving platforms or frameworks until its been released for awhile. That's they way its always worked for me in various areas of the public sector.
# February 28, 2005 5:19 AM

Brendan Tompkins said:

"LOL, you just had to blog about that code :). "

Sahil, you are 100% right on. And that snippet isn't even Air Code! It took me all weekend to figure out how to get it into a post!
# February 28, 2005 7:09 AM

Brendan Tompkins said:

Geoff this is absolutely fantastic! I'm going to port it to C# and COBAL.
# February 28, 2005 7:10 AM

Darrell said:

The problem with air code is then every cut-n-paste programmer (term used LOOSELY) will:
1. Ask why it doesn't compile, or
2. Prove you wrong by saying it doesn't compile so you don't know what you are doing.

:)
# February 28, 2005 7:39 AM

Brendan Tompkins said:

Yep. But when you have the [AirCode] att at the top of your code, you can just point them there. Hey, and if you're just thinking out loud, then it's probably better that your code doesn't compile in the first place! Less chance that smelly code will get inadvertently introduced into someone's project.
# February 28, 2005 7:45 AM

Scott Isaacs said:

Of course us VB developers will be using angle brackets:

<AirCode> _
Private Sub DoNothing()
Exit Sub
End Sub
# March 1, 2005 6:19 PM

Brendan Tompkins said:

Scott! Yes, the AirCode attribute so far has only been coded for VB, see Geoff's reply above. Your use is exactly correct (which is to say that it's completely incorrect).
# March 2, 2005 7:24 AM

Geoff Appleby said:

Yes Scott, to have used it in the correct manner you needed broken code.

This would work:
<AirCode> _
Private Sub DoNothing()
Exit Function
End Sub

So would this:
<AirCode> _
Private Sub DoNothing()
string foo = "Doh!";
Exit Sub
End Sub

And probably my favourite:
[AirCode] _
Private Sub DoNothing()
Exit Sub
End Sub

It's only broken in the simplest way, and it only if you include the attribute in the first place :)
# March 4, 2005 2:15 AM

Peter's Gekko said:

If SQL had known attributes I should have decorated my Like query with [AirCode]. Several comments questioned...
# April 8, 2005 1:57 PM

Raymond Lewallen said:

SqlServerCentral address a recent issue they had with an author guilty of plagarism.
# July 27, 2005 9:34 AM

Raymond Lewallen said:

SqlServerCentral addresses a recent issue they had with an author guilty of plagarism and issues an apology.
# July 27, 2005 9:44 AM

David Hayden said:

As mentioned in my previous post, the DbConnectionStringBuilder Class mentioned in Pro ADO.NET 2.0 allows...
# October 26, 2005 4:40 PM

gogole said:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
# July 11, 2006 9:33 AM

Adding Action Filters to MonoRail Controllers « Random Code said:

Pingback from  Adding Action Filters to MonoRail Controllers &laquo; Random Code

# March 2, 2008 7:06 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!