One of the first things a software engineer learns is ādonāt reinvent the wheelā. If something is already made, use that instead of writing your own. āStand on the shoulders of giants, they know what theyāre doing better than youā. Writing your own tools and libraries, even when one already exists, is labelled āNIH syndromeā Ā and is considered quite bad. āBut what if my version is better?ā. Surely, reinventing the wheel canāt be bad when your new wheel improves existing wheel designs, right? Well, not if the software is open source, which is usually the case in our industry. āJust contribute to itā youāll be told. However, contributing to an open source project is basically teamwork. The success of any team depends on how well its members work together, which is not a given. Sometimes, your vision about the tool might be vastly different from that of the core members and it might be wiser to create your own prototype than to try and change the minds of all these people.
However, Open Source politics is not what I wanted to discuss today. Itās not the biggest potential benefit of reinventing the wheel. Minimizing overhead is. You hardly ever need 100% of a project. Given enough time to study its inner workings, you could always delete quite a large chunk of it and it would still fit your needs perfectly. However, the effort needed to do that or to rewrite the percentage you actually need is big enough that you are willing to add redundant code to your codebase.
Redundant code is bad. It still needs to get parsed and usually at least parts of it still need to be executed. Redundant code hinders performance. The more code, the slower your app. Especially when we are dealing with backend code, when every line might end up being executed hundreds or even thousands of times per second. The slower your app becomes, the bigger the need to seriously address performance. The result of that is even more code (e.g. caching stuff) that could have been saved in the first place, by just running what you need. This is the reason software like Joomla, Drupal or vBulletin is so extremely bloated and brings servers to their knees if a site becomes mildly successful. Itās the cost of code that tries to match everyoneās needs.
Performance is not the only drawback involved in redundant code. A big oneĀ is maintainability. This code wonāt only need to be parsed by the machine, it will also be parsed by humans, that donāt know whatās actually needed and what isnāt until they understand what every part does. Therefore, even the simplest of changes become hard.
Iām not saying that using existing software or libraries is bad. Iām saying that itās always a tradeoff between minimizing effort on one side and minimizing redundant code on the other side. Iām saying that you should consider writing your own code when the percentage of features you need from existing libraries is tiny (lets say less than Ā 20%). It might not be worth carrying the extra 80% forever.
For example, in a project Iām currently working on, I needed to make a simple localization system so that the site can be multilingual. I chose to use JSON files to contain the phrases. I didnāt want the phrases to include HTML, since I didnāt want to have to escape certain symbols. However, they had to include simple formatting like bold and links, otherwise the number of phrases would have to be huge. The obvious solution is Markdown.
My first thought was to use an existing library, which for PHP is PHP Markdown. By digging a bit deeper I found that itās actually considered pretty good and it seems to be well maintained (last update in January 2012) and mature (exists for over 2 years). I should happily use it then, right?
Thatās what I was planning to do. And then it struck me: Iām the only person writing these phrases. Even if more people write translations in the future, they will still go through me. So far, the only need for such formatting is links and bold. Everything else (e.g. lists) is handled by the HTML templates. Thatās literally two lines of PHP! So, I wrote my own function. Itās a bit bigger, since I also added emphasis, just in case:
function markdown($text) {
// Links
$text = preg\_replace('@\\\\\[(.+?)\\\\\]\\\\((#.+?)\\\\)@', '<a href="$2">$1</a>', $text);
// Bold
$text = preg_replace(ā@(?<!\\\\)\\*(?<!\\\\)\\*(.+?)(?<!\\\\)\\*(?<!\\\\)\\*@ā, ā<strong>$1</strong>ā, $text);
// Emphasis
$text = preg_replace(ā@(?<!\\\\)\\*(.+?)(?<!\\\\)\\*@ā, ā<em>$1</em>ā, $text);
return $text;
}
Since PHP regular expressions also support negative lookbehind, I can even avoid escaped characters, in the same line. Unfortunately, since PHP lacks regular expression literals, backslashes have to be doubled (\\
instead of \
so \\\\
instead of \\
, which is pretty horrible).
For comparison, PHP Markdown is about 1.7K lines of code. Itās great, if you need the full power of Markdown (e.g. for a comment system) and Iām glad Michel Fortin wrote it. However, for super simple, controlled use cases, is it really worth the extra code? I say no.
Rachel Andrew recently wrote about something tangentially similar, in her blog post titled āStop solving problems you donāt yet haveā. Itās a great read and Iād advise you to read that too.