Recently I was listening to a podcast and there was this really smart guy Piwai talking about something that instantly captivated by attention. That was the coining of the term Offensive Programming.
What is offensive programming?
Well, you can find the literature on Wikipedia and also I am not the best person to explain that. So check that out please. But fundamentally, offensive programming refers to a style of programming that is exact opposite of the more famous counter-part the defensive programming.
Defensive programming refers to coding style which adheres to dealing gracefully with conditions that should not happen.
Offensive programming on the other hand, well just tells you to let the app crash. Don’t try to recover, don’t try to handle the exception, just log the stack trace and crash.
The reason behind this is that in reality the problem can be much bigger and somewhere else in the code, as a side effect of you are getting this error in first place. This forces you to fix the problem at the source and will possibly result in a healthier code base.
When it makes sense to be offensive?
This was my exact concern while I was listening to this podcast. Thankfully, Piwai answered that himself. I also, talked about it with a really smart guy at the office and he also made the same remarks.
So at Square (the company who do payments and author libraries) what they do is, they stick to a defensive style of programming for interfaces and parts of code that deals with external interfaces and/or user interactions. Basically, something that is not in your control.
But, for the internal interfaces, where the classes you wrote are going to interact with each other, you don’t have to be that paranoid about that. This is where he (Piwai) said you should switch to the offensive approach. You have full control over the classes you wrote, and the expected behaviour is in your control. If it fails to do so, it’s better to just crash and let the problem to be fixed at the source.
That is the exact reason he said at Square, they make very liberal use of assertions in the code. Assertions are not forgiving at all.
I would attempt to point to examples here, one that the Piwai himself talked in very brief and the one that I’ve encountered myself where I thought it made sense.
In this example, say we are handling credit card objects. There is no point to internally validate the credit card object every time you deal with it.
As soon as we get a credit card, we decorate it with a validated credit card. That’s all the defensiveness we had to offer.
Now internally, we go offensive and throw exceptions or assertions every time we encounter an invalidate credit card object.
The code below is not perfect, but can give you an idea.
Another example I can think of is a much simpler one and more relatable.
Suppose, we have a utility function that uploads a file to s3.
It would make sense to follow offensive programming style and just throw an exception if somehow they file or the key reaching the function is None.
Few more tips from the podcast
1. How to start with offensive programming?
Best way is to start putting assertions in the code, where you think is suitable. Yeah, we’ll experience more crashes and that’s awesome!
Because now we know that we have a problem.
2. We feel more confident about the code base:
We just know that, this method doesn’t try to handle nulls, thus I can confidently say that it was not null or it would’ve crashed.
3. Do incremental roll outs.
When you ship a code, roll it out like for 1% of users. We’ll have a ton of crash reports, and that’s good! I mean not for the 1% users but they are taking one for the team!
4. Crash at preventable errors and recover from expect-able errors :
Preventable errors are invalid arguments, NPEs etc. Go offensive on these.
Expectable errors are like resource depletion, invalid user inputs etc.
Try to recover from these.
Overall, it was nice to listen to a guy who works at a company like Square talking about how they use offensive programming for a healthier code base. And if Square is doing something, we all can learn something from that!