All developers have been told to write reusable code. I remember having that drilled into my head in college by my professors daily. The main reason we have to write reusable code: so we don’t have to write the same code twice. That is a perfectly valid reason and I can’t argue with that. But, what if there’s a bigger reason.
Optionality and Reusable Code
Optionality, a concept that Nassim Taleb has brought to us in his books Antifragile, is the ability to have many options at every layer of the system that lets us prepare for volatility, giving yourself the chance to not only withstand it but to succeed and prosper in a big way.
Reusable coding should increase the number of options on how to use the code.
Within software, one way of reaching optionality is to write your code, at every layer, to be reusable. Reusable coding should increase the number of options on how to use the code without breaking the Single Responsibility Principle. Let me show you with an example.
Reusable Refactoring Example
Couple developers on my team submitted a pull request for a rather large feature. In the code was a class who’s purpose was to convert identifiers from one value from the next using a array of old and new values.
var ids = [
{ old: "162735", new: "8493273" },
{ old: "736482", new: "7427364" }
];
class DeckIdConverter {
convertDeckIds(ids, data){
var jdata = JSON.stringify(data);
ids.forEach( id => {
jdata = data.replace(id.old, id.new);
});
return JSON.parse(jdata);
}
}
Yes, this is reusable code. We can instantiate it and reuse it many places and not have to copy/paste code. However, within the context of Optionality, this isn’t very reusable: it doesn’t give us very many options on how to use it.
When I saw this code, I asked the developers how we could make this more reusable. It’d be nice to use this code to swap out identifiers with other entities.
They began to brainstorm on a more generic EntityIdConverter. However, one of them had a brilliant idea: It’s really just a simple multiple string replace, lets create a String extension called “replaceMany”.

Ten minutes later they changed the code to this:
String.prototype.replaceMany = function(stringMaps) {
var s = this;
stringMaps.forEach((map) => {
s = s.replace(new RegExp(map.source, 'g'), map.destination);
});
return s;
}
var ids = [
{ source: "162735", destination: "8493273" },
{ source: "736482", destination: "7427364" }
];
var jdata = JSON.stringify(data);
jdata.replaceMany(ids);
This code is way more reusable and can be used with any entity or within any domain. This will allow for code that is more readable and maintainable.
Overview
To avoid volatility in a system, one should apply optionality at every level to give oneself as many options as possible to avoid or withstand large catastrophes. To do this in software, developers should write reusable code that will provide the highest number of options for reuse.