You have to pass third before going home.

All forces occur in pairs, and these two forces are equal in magnitude and opposite in direction.

This is Newton's third law of motion.

This is Patrick's first law of programming.

The thing I've been bitten with more than any in my "life" at, and before, Microsoft are bugs related to the releasing or freeing or generally UNDOing something I did earlier in code.  Acquire a CriticalSection.  Forget to free it in some weird error path you never expect to hit, until you hit actually hit it.  Allocate a hunk of memory, forget to free it somewhere you never expect to hit, until you hit it.  Take a ref count, forget to release it somewhere (you know the rest).  So over the years, I have adapted my code monkey style to follow some rules in other disciplines of science that tend to lend themselves to computer science (and some of these are really axioms for just about everything you do in daily life).

So in this particular exmaple, it is Newton's Third Law.  For every action, there is an equal and opposite reaction.  For every allocate, there is a free, for every InitializeCriticalSection, there is a DeleteCriticalSection and so on...

I've tried to explain to my wife that this follows daily life as well, for every upward flip of the light switch, there is a downward flip, for every rightward twist of the stove nob, there is an equal leftward twist, etc.  Needless to say that doesn't go over well, but the small upside here is our couch is very comforatble. 

So I tend to keep my little crazy "laws" to my work.  I almost always try to take some additional steps to ensure that every time I do something in code that has an "action" in the API, there is an undo action later in the code.  And then, I also try to make sure that undo action is reachable by every exit path.  Save that one time I wrote a test that I forgot to actually allocate memory for an object before using it....which I just ran in to yesterday.  That my friends is a very loose variant of De Morgan's law being applied to Newton's Thrid Law which was adapted to be Patrick's First Law of Programming. Got that? 🙂

Skip to main content