The Invasion of Iraq, As Expressed in Code

Presumably, the invasion plan for Iraq ran something like the following:

InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

```
InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

// Back home for tea and medals
``` // Back home for tea and medals

Now, any semi-competent programmer could tell you that this plan is woefully inadequate. Firstly, there is no checking of assumptions. A start at improving the code could go like this:

Assert(PrettyGoodEvidenceForWMD);
Assert(NotIncrediblyVolitileCountryHeldTogetherByIronFist)
Assert(NotLikelyToIncreaseDangerOfTerrorism) ``` Assert(PrettyGoodEvidenceForWMD);
Assert(NotIncrediblyVolitileCountryHeldTogetherByIronFist)
Assert(NotLikelyToIncreaseDangerOfTerrorism)

InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

// Back home for tea and medals
``` // Back home for tea and medals

In production code, however, there would need to be more pre-condition checks, as I’m sure we’ve missed some.

Next, of course, we note there is no exception handling—meaning any problems will bring the whole edifice crashing down. We should catch exceptions, let’s have a first stab at this:

Assert(PrettyGoodEvidenceForWMD);
Assert(NotIncrediblyVolitileCountryHeldTogetherByIronFist)
Assert(NotLikelyToIncreaseDangerOfTerrorism) ``` Assert(PrettyGoodEvidenceForWMD);
Assert(NotIncrediblyVolitileCountryHeldTogetherByIronFist)
Assert(NotLikelyToIncreaseDangerOfTerrorism)

try
{ InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

// Back home for tea and medals
}
catch (Exception)
{ // Catch any other problems --- not sure what to do StayTheCourse();
}
``` // Back home for tea and medals
}
catch (Exception)
{ // Catch any other problems --- not sure what to do StayTheCourse();
}

This is looking better — though the catch all Exception block looks like a problem. Standard advice is never to catch exception in the general case, but I don’t see what we can do here. I don’t think it should drop through but who knows what it would be better to do. I’ve stubbed out the StayTheCourse() method, again, in production code there should be something far more robust:

public void StayTheCourse()
{ president.speech("We must stay the course"); ``` public void StayTheCourse()
{ president.speech("We must stay the course");

// TODO: work out what to do next
}

To counter this problem, we should add some specific exception handling for things it's foreseeable may happen. We can then call appropriate code to do something sane under each of these circumstances---even if it's just to clean up and pull out cleanly.

<p><pre><code>Assert(PrettyGoodEvidenceForWMD);<br />

Assert(NotIncrediblyVolitileCountryHeldTogetherByIronFist)
Assert(NotLikelyToIncreaseDangerOfTerrorism)

<p>try<br />

{ InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

<p>	// Back home for tea and medals<br />

}
catch (PeopleNotWelcomingUsWithFlowersException)
{ ExecutePlanForUnwelcomeInCountry();
}
catch (CivilWarException)
{ ExecutePlanForCollapseIntoCivilWar();
}
catch (InsurgencyException)
{ ExecutePlanForInsurgency();
}
catch (Exception)
{ // Catch any other problems — not sure what to do StayTheCourse();
}


To counter this problem, we should add some specific exception handling for things it's foreseeable may happen. We can then call appropriate code to do something sane under each of these circumstances---even if it's just to clean up and pull out cleanly.

try<br />
{
	InvadeCountry(Country.Iraq, Tactics.ShockAndAwe);

	// Back home for tea and medals<br />
}<br />
catch (PeopleNotWelcomingUsWithFlowersException)<br />
{
	ExecutePlanForUnwelcomeInCountry();<br />
}<br />
catch (CivilWarException)<br />
{
	ExecutePlanForCollapseIntoCivilWar();<br />
}<br />
catch (InsurgencyException)<br />
{
	ExecutePlanForInsurgency();<br />
}<br />
catch (Exception)<br />
{
	// Catch any other problems --- not sure what to do
	StayTheCourse();<br />
}<br />
</code></pre>

As I'm not skilled in the arts of international affairs, I'll have to leave the TODO in StayTheCourse() and the implementations of ExecutePlanForX() to more able minds than myself. I think this is a better starting point, however, even if we have left some methods as stubs --- it shows we've thought through the problems that may occur in our code, a mark of a good programmer.
← Older
Nine Circles of Hell and Web Services
→ Newer
Universal's Big Mistake, Thumbing Their Nose to Apple