<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Laconicism is underrated</title>
    <description></description>
    <link>http://markabramov.me/</link>
    <atom:link href="http://markabramov.me/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 04 Jan 2026 14:26:50 +0000</pubDate>
    <lastBuildDate>Sun, 04 Jan 2026 14:26:50 +0000</lastBuildDate>
    <generator>Jekyll v4.4.1</generator>
    
      <item>
        <title>Storing Temporal Data with Tree-Like Structures in an RDBMS</title>
        <description>&lt;h1 id=&quot;storing-temporal-data-with-tree-like-structures-in-an-rdbms&quot;&gt;Storing Temporal Data with Tree-Like Structures in an RDBMS&lt;/h1&gt;

&lt;p&gt;Persisting a tree to an RDBMS is, in general, a solved problem: there are at least four effective data models that allow you to keep your hierarchical data stored safely and with various degree of referential integrity.&lt;/p&gt;

&lt;p&gt;However, let’s say you want to store a company hierarchy and be able to answer questions like “who was CEO on 2017-01-03?” and “who was on John’s team on 2013-09-12?” This task requires not just storing the tree, but also being able to “time travel” back to any point of time and restore that tree at that time.&lt;/p&gt;

&lt;p&gt;When a task like that came up at work, it turned out that knowledge on this particular subject is a bit scarce, but the general consensus is that you use whatever method you prefer and store two timestamps (valid_since / valid_until) alongside your nodes or edges and scope all your requests correspondingly.&lt;/p&gt;

&lt;p&gt;I have used different methods of tree storage before, but it still wasn’t immediately clear which method would be better adapted to that task since you would have to make more requests, store more data, and sometimes have to rebuild large parts of the trees when something changes. Thus, I decided to run some benchmarks.&lt;/p&gt;

&lt;h2 id=&quot;test-setup-and-methodology&quot;&gt;Test setup and methodology&lt;/h2&gt;

&lt;p&gt;Before we go further, I must mention some particulars of the task I had at work: we have a medium-sized (~20000 nodes) tree with &lt;em&gt;very infrequent&lt;/em&gt; updates (&amp;lt;20 / day) and &lt;em&gt;frequent&lt;/em&gt; reads (easily tens of thousands of these per day). This is important because we can largely ignore destructive operation costs as long as our queries are fast. If your data has different read/write characteristics, you will need to run your own analysis.&lt;/p&gt;

&lt;p&gt;For these benchmarks, I generated several datasets containing a mix of queries and destructive operations. It’s easier to explain using an example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;events:
- timestamp: 1
  type: add_node
  key: 1
  parent: null
- timestamp: 5
  type: add_node
  key: 2
  parent: 1
&amp;lt;...&amp;gt;

tests:
- timestamp: 3
  type: descendants
  key: 1
  result: []
- timestamp: 6
  type: descendants
  key: 1
  result: [2]
&amp;lt;...&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this simple set we have two nodes: (1), the root, and (2), a child of (1). At timestamp 0 there are no nodes in the tree. At timestamp 2 there is only one, (1). At timestamp 6 there are both of them. As such, the test at timestamp 3 has no descendants for (1) and the test at timestamp 6 has one.&lt;/p&gt;

&lt;p&gt;There are three basic types of modifying operations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_node&lt;/code&gt; (add a node C containing parent key P as the child of its parent P);&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;change_parent&lt;/code&gt; (switch parent of a node from key X to key Y; transfer it to that new parent with all the children);&lt;/li&gt;
  &lt;li&gt;and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;implode_node&lt;/code&gt; (remove a node with a certain key from the tree, transferring all its children to the current parent of the imploded node)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also two types of queries:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ancestors&lt;/code&gt; (select all ancestors of the node ordered from child to root)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;descendants&lt;/code&gt; (select all descendants of the node, unordered)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These events are not equally frequent in our practice, so the following distribution was used:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;for updates: ~85% add_node / ~10% change_parent / ~5% implode_node&lt;/li&gt;
  &lt;li&gt;for queries: ~80% ancestors / ~20% descendants&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, three differently-sized datasets were used:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;small_set&lt;/code&gt;: 100 initial nodes, 50 updates, 200 queries per update&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;medium_set&lt;/code&gt;: 500 initial nodes, 500 updates, 200 queries per update&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;large_set&lt;/code&gt;: 1000 initial nodes, 5000 updates, 200 queries per update&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see that each set is about an order of magnitude larger than the previous one.&lt;/p&gt;

&lt;p&gt;Software-wise, I’m running Ubuntu 17.10, PostgreSQL 9.6.6 and MySQL 5.7.21.&lt;/p&gt;

&lt;h2 id=&quot;tested-methods-overview&quot;&gt;Tested methods overview&lt;/h2&gt;

&lt;h3 id=&quot;parent_id--timestamps&quot;&gt;parent_id + timestamps&lt;/h3&gt;

&lt;p&gt;This is probably the simplest and the most familiar way to store a tree. Every node has a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent_id&lt;/code&gt; key that refers to its parent and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_since&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_until&lt;/code&gt; columns. When you change a parent, you only need to update the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_until&lt;/code&gt; column with the current timestamp and insert a new node with the new parent and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_since&lt;/code&gt; set to the current timestamp. When you implode a node, you need to create new nodes for each direct children and that’s about it.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;very simple to implement&lt;/li&gt;
  &lt;li&gt;very light on writes, you only store the bare minimum&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;you can’t get all the ancestors or all the descendants in one query&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;parent_id--timestamps--snapshots&quot;&gt;parent_id + timestamps + snapshots&lt;/h3&gt;

&lt;p&gt;Let’s try making the previous method a bit more effective. What if we made a periodic snapshot of the whole tree? We would only need to run our queries on the subset of the table that is the current snapshot + maybe some delta changes between snapshot time and current state.&lt;/p&gt;

&lt;p&gt;Technically that looks like this: we create another table &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;versions&lt;/code&gt; that has just two fields: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timestamp&lt;/code&gt;. Every node has also a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;version&lt;/code&gt; field that refers to the id for that version. Every several ticks (hours/days/whatever) we make a snapshot, meaning that we copy all the active nodes from the previous snapshot with the new version and make the new snapshot current. All new writes are directed to the new current version.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Supposedly faster reads (more on that later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Much higher space requirements for the snapshots&lt;/li&gt;
  &lt;li&gt;You have to be really careful when building snapshots, and either “stop the world” for the duration of the snapshot, or duplicate writes to the “current” and the “previous” snapshots. If you don’t, you open yourself up to data races&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: this method was only tested on MySQL.&lt;/p&gt;

&lt;h3 id=&quot;ltree--timestamps&quot;&gt;ltree + timestamps&lt;/h3&gt;

&lt;p&gt;PostgreSQL has a standard &lt;a href=&quot;https://www.postgresql.org/docs/9.1/static/ltree.html&quot;&gt;ltree&lt;/a&gt; extension that implements the so-called “materialized path” pattern. You store a string-like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; for every node that contains that node ancestry. For example, for node (3) that is a child of (2) that is a child of (1) the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.2.3&lt;/code&gt;. This Postgres extension is interesting because it supports &lt;a href=&quot;https://www.postgresql.org/docs/9.4/static/gist.html&quot;&gt;gist indexing&lt;/a&gt; of the path column, supporting queries like “select all ancestors of path” or “select all descendants of path”. However, for ancestors we can do &lt;strong&gt;even better&lt;/strong&gt;: simply split the path string on dots and reverse and there you have it.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Supported path queries out of the box&lt;/li&gt;
  &lt;li&gt;Fast lookups for descendants on path via gist index&lt;/li&gt;
  &lt;li&gt;Zero-query lookups for ancestors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A bit trickier to implement&lt;/li&gt;
  &lt;li&gt;Manipulating strings &lt;em&gt;feels&lt;/em&gt; like it should be slow (but actually isn’t, see below)&lt;/li&gt;
  &lt;li&gt;Changing ancestry means you need to create new nodes for every descendant that is updated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: this method was only tested on PostgreSQL.&lt;/p&gt;

&lt;h3 id=&quot;closure-tables--timestamps&quot;&gt;closure tables + timestamps&lt;/h3&gt;

&lt;p&gt;This is a relatively new method for me, one I have never used before. There is a good intro &lt;a href=&quot;https://coderwall.com/p/lixing/closure-tables-for-browsing-trees-in-sql&quot;&gt;here&lt;/a&gt;. The gist of it that you store both ancestor and descendant info in a separate “closure” table.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Very fast lookups for both ancestors and descendants&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Every tree update results in a lot of closure updates both up and down the tree&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;initial-hypotheses&quot;&gt;Initial hypotheses&lt;/h2&gt;

&lt;p&gt;My intuition suggested that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent_id + timestamps&lt;/code&gt; method would probably be the slowest, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent_id + snapshots&lt;/code&gt; be the fastest, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ltree&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closure tables&lt;/code&gt; somewhere in-between. My rationale was that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snapshot&lt;/code&gt; method would have the least data to reason about for any point of time and as such have the best selectivity for its indices, whereas the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ltree&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closure tables&lt;/code&gt; methods generate a lot of extra stuff for any update that would really trip up most queries.&lt;/p&gt;

&lt;h2 id=&quot;actual-results&quot;&gt;Actual results&lt;/h2&gt;

&lt;p&gt;Well, I was wrong on all counts. In actuality, the parameters of my dataset generation made any extra garbage generated by the modifying ops negligible. If the node changes 20 times over the lifetime, that’s nothing, nothing at all. Any index by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;key&lt;/code&gt; would have fantastic selectivity. Besides, for such smallish datasets the storage requirements are also really, really small.&lt;/p&gt;

&lt;h3 id=&quot;some-charts&quot;&gt;Some charts&lt;/h3&gt;

&lt;h4 id=&quot;mysql&quot;&gt;MySQL&lt;/h4&gt;

&lt;h5 id=&quot;total-time-taken-by-queries&quot;&gt;Total time taken by queries&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/mysql_tests_total.png&quot; alt=&quot;total test time&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;total-time-taken-by-destructive-operations&quot;&gt;Total time taken by destructive operations&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/mysql_setup_total.png&quot; alt=&quot;total test time&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;total-db-size-as-reported-by-sumdata_length--index_length&quot;&gt;Total db size (as reported by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUM(data_length + index_length)&lt;/code&gt;)&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/mysql_db_size.png&quot; alt=&quot;total db size&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;postgresql&quot;&gt;PostgreSQL&lt;/h4&gt;

&lt;h5 id=&quot;total-time-taken-by-queries-1&quot;&gt;Total time taken by queries&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/postgresql_tests_total.png&quot; alt=&quot;total test time&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;total-time-taken-by-destructive-operations-1&quot;&gt;Total time taken by destructive operations&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/postgresql_setup_total.png&quot; alt=&quot;total test time&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;total-db-size-as-reported-by-pg_database_size&quot;&gt;Total db size (as reported by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pg_database_size&lt;/code&gt;)&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/img/historic-trees/postgresql_db_size.png&quot; alt=&quot;total db size&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;I don’t put PostgreSQL and MySQL results on the same scale, because a lot depends on server configuration tuning and I’m not sure my server configuration is the best. Having said that, results for the same method are close enough to not warrant a switch of db, so just go with whichever you prefer&lt;/li&gt;
  &lt;li&gt;It deserves repeating that my tests were read-heavy, not write-heavy. Changing the read to write ratio WILL affect your results&lt;/li&gt;
  &lt;li&gt;All the tests were run in a single thread, cpu load for db floated around 30% (of a single core), and for the ruby process around 70% (of a single core)&lt;/li&gt;
  &lt;li&gt;Code could definitely be made faster and slicker, but that is left as an exercise to the reader&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;NEVER&lt;/strong&gt; do time-travelling updates, as tempting as those might be. However well-meaning you are, you will break data integrity, cause time paradoxes and probably even destroy the Universe. Just don’t.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;In my testing, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closure&lt;/code&gt; method ended up being the fastest, closely followed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ltrees&lt;/code&gt;, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent_id&lt;/code&gt; being way slower and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent_id + snapshots&lt;/code&gt; being the slowest of the bunch.&lt;/p&gt;

&lt;p&gt;The latter result was surprising to say the least, and some of the slowness could probably be attributed to the fact that infrequent updates + snapshots means you have a lot of rows in your table from snapshotting. You also don’t win as much from the snapshots as you would if updates had been more frequent. Also, any ancestor/descendant query would also require getting the corresponding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;version&lt;/code&gt;, which meant an extra query to the versions table (admittedly that could be optimized by having some in-memory cache for versions and carefully managing that cache). Either way, other methods were so much more effective that I didn’t feel the need to play around with snapshot thresholds or other params.&lt;/p&gt;

&lt;p&gt;In conclusion, my first recommendation for this kind of trees with temporal data sewn in would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closure tables&lt;/code&gt; as the fastest and the most universal of all four. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ltree&lt;/code&gt; would be a close second, having the better support for many kinds of queries and easier ranking for descendants (that would come handy if you wanted, for example, to display nested comment threads or something like that).&lt;/p&gt;

&lt;h2 id=&quot;some-code-and-further-reading&quot;&gt;Some code and further reading&lt;/h2&gt;

&lt;p&gt;Test setup, implementation and test generation code for this post can be found &lt;a href=&quot;https://github.com/markiz/historical-trees-matchup&quot;&gt;here&lt;/a&gt;. All the usual caveats apply: code is rough, ineffective and written by a dangerous idiot, please be careful when running other people’s code&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://link.springer.com/chapter/10.1007/978-3-319-19024-2_28&quot;&gt;Version Management of Hierarchical Data in Relational Database&lt;/a&gt; — paper suggesting the design we ended up using&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://db.in.tum.de/~finis/papers/DeltaNI.pdf&quot;&gt;DeltaNI: An Efficient Labeling Scheme for Versioned Hierarchical Data&lt;/a&gt; — an interesting paper that I ended up not using but is probably worth mentioning anyway&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Huge props to &lt;a href=&quot;https://github.com/jimm&quot;&gt;Jim Menard&lt;/a&gt; for proof reading this article.&lt;/p&gt;
</description>
        <pubDate>Fri, 02 Feb 2018 22:00:00 +0000</pubDate>
        <link>http://markabramov.me/programming/general/2018/02/02/storing-temporal-data-with-tree-like-structures-in-rdbms.html</link>
        <guid isPermaLink="true">http://markabramov.me/programming/general/2018/02/02/storing-temporal-data-with-tree-like-structures-in-rdbms.html</guid>
        
        
        <category>programming</category>
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>Thoughts on complexity</title>
        <description>&lt;p&gt;When I posted a link to the old “&lt;a href=&quot;http://www.teale.de/images/java.jpg&quot;&gt;Friends don’t let friends do Java&lt;/a&gt;” picture, I never expected it to receive a lot of comments. I never expected being called a neo-luddite, either. I guess, I should have prepared for this, since it was a fairly cheap jab at Java. It got me thinking about all sorts of complexity though.&lt;/p&gt;

&lt;p&gt;Before I proceed any further, I must disclaim that I am as guilty as any other programmer: under pressure of budget and time constraints I have cranked out code that I am really ashamed of. When required to choose between sabotaging business plans and dying a little inside, I will always choose the latter without second thoughts. However, I feel it’s important for us as craftsmen, philosophers and artisans to do some occasional introspection, reflection and reality check.&lt;/p&gt;

&lt;h2 id=&quot;classifying-complexity&quot;&gt;Classifying complexity&lt;/h2&gt;

&lt;p&gt;If the code works and the business task is solved, is it really important how that was achieved? Yes it is. It’s been fairly established that technical debt (however vaguely defined) is something that should be avoided at all costs. Or at least paid as immediately as possible, before it starts charging you the interest. However, when it comes to code complexity, it is often overlooked. Sometimes people even use tools and metrics that force you to increase complexity for no apparent reason at all. I’m going to focus on two kinds of complexity today: patterns explosion and dependency hell.&lt;/p&gt;

&lt;h3 id=&quot;explosion-of-patterns-and-fake-extensibility&quot;&gt;Explosion of patterns and fake extensibility&lt;/h3&gt;

&lt;p&gt;Now, Java has been ridiculed a lot for the abuse of patterns by its users. Lots of it occurs naturally as a result of language being extremely verbose and at the same time insufficiently powerful. You couldn’t make something like &lt;a href=&quot;http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/orm/jdo/TransactionAwarePersistenceManagerFactoryProxy.html&quot;&gt;TransactionAwarePersistenceManagerFactoryProxy&lt;/a&gt; up even if you wanted to. But it’s not exclusive to Java at all. I see people building extreme class hierarchies in Ruby too. Even if they don’t use crazy inheritance chains, the result is using all the patterns prematurely.&lt;/p&gt;

&lt;p&gt;Maybe it’s better illustrated by an example: http://madewithenvy.com/ecosystem/articles/2015/active-record-and-the-srp/&lt;/p&gt;

&lt;p&gt;What he wanted to do: create a record with a provided email, send some notification that’s about two lines of code in any sane codebase. What he actually did: build two classes with dependency injection, abstract out the repository and notifier interface. That’s extra two classes and two interface contracts for what amounts to be a chance at some future extensibility. I mean, fine, if you really need this stuff, you need it. If your application requires twenty different notifiers for this particular case, by all means go for it. You don’t reach for a sledgehammer to crack a nut. What I find especially sad, that this kind of abstraction is often considered okay by all kinds of OO best practitioners. It looks fine, its metrics are fine, method length is fine, class length is fine, number of arguments per method is fine. Codeclimate or a similar tool will probably rate this code A. I disagree.&lt;/p&gt;

&lt;p&gt;DHH has been fighting this kind of architectural astronautics for ages. He didn’t succeed. Now I know how easily a rails application turns into a giant ball of mud. However, this is the case when preemptive taking of medicine is much worse than any illness it would cure. You don’t use the nuclear option unless you’ve exhausted all the other ways.&lt;/p&gt;

&lt;h3 id=&quot;dependency-hell&quot;&gt;Dependency hell&lt;/h3&gt;

&lt;p&gt;Dependencies are another huge source of accidental complexity that you might willingly add to your app without thinking too much.&lt;/p&gt;

&lt;p&gt;Consider a simple task: let’s say you see a feed of posts and comments and would like to especially filter out those that are in Orcish. Let’s also say that you don’t mind some false negatives (i.e. Orcish comments that get through the cracks), you’re fine with 80% successful result (however you do mind false positives, i.e. Elven comments being classified as Orcish). What’s the best way to deal with this situation?&lt;/p&gt;

&lt;p&gt;Now, language detection is a known problem. Common solutions include training a neural network, using bayesian analysis on trigrams, maybe something else. However these are fairly costly solutions. For example, javascript &lt;a href=&quot;https://github.com/wooorm/franc&quot;&gt;franc&lt;/a&gt; library includes 250kb of raw json with a bunch of trigrams. That’s 250kb of json you have to load before you even do something meaningful. After that the target text is split into trigrams, checked against that huge dict, and so on and so forth. Now, does this solve the problem? Yes. Is it the best way to do it? It depends.&lt;/p&gt;

&lt;p&gt;Our generic solution didn’t take into account that we’re okay with 80% success rate and only one blocked language. Do we really need this dependency? I’m saying we could get rid of it and make a much better ad-hoc solution that will be two orders of magnitude faster. Just make a list of top-100 most popular Orcish words that are unique to Orcish, and build them into one big regex like this: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/word1|word2|word3|word4|...|word100/&lt;/code&gt;. The way most sane languages implement regular expressions, it will probably be blazingly fast, require miniscule memory, and be perfectly fit for embedding into a browser, even a mobile one.&lt;/p&gt;

&lt;p&gt;Obviously, this approach doesn’t scale too well and has some very noticeable limitations (relatively high false negatives rate), but it’s dead simple and doesn’t bring in a huge dependency. We never shoot assault rifle at mosquitoes, even if it killed them, because that would be simply wasteful (and sometimes dangerous). However, when it comes to dependencies, developers often pile them on without so much as twitch of an eye.&lt;/p&gt;

&lt;p&gt;Oftentimes, performance hit you get by abusing dependencies is not really an issue. 1 second load time or 5 seconds load time, who cares if it only needs to happen once per session. However, another thing to consider before you make your application more complex for no reason is abstraction leakage. Joel Spolsky formulated his brilliant &lt;a href=&quot;http://www.joelonsoftware.com/articles/LeakyAbstractions.html&quot;&gt;law of leaky abstractions&lt;/a&gt; back in 2002. What this means to us, practically, is that either you understand the inner mechanics of the abstractions you use, or one day you’re going to have to deal with an abstraction that fell apart right on your hands. In my practice, I’ve encountered all kinds of breakage on all levels of stack: hardware fails, os fails spectacularly, networking fails, infrastructure, like rdbms, fails so often it’s not even funny, your libraries fail, your framework fails, your client browser fails and obviously your application fails all the time. Now you can’t prevent stuff from failing, it’s a fool errand, however what’s not there cannot fail. The less moving parts there are, the more robust the mechanism generally is. Think about this before adding another 35mb “&lt;a href=&quot;https://rubygems.org/gems/cld2/versions/1.0.3&quot;&gt;compact language detector&lt;/a&gt;” gem to your project.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Like I mentioned in the beginning of this essay, we are often faced with the problem of external constraints passed to us directly from The Powers That Be. Short on budget, short on time, we build some quick and dirty hacks that solve our immediate problems, but lead to more problems in the future. It’s very similar in nature to the technical debt: you add some dependency or pattern today, you have to pay the cognitive price for the rest of your project’s life. Since it’s not immediately apparent, measured, quantified and turned into KPIs and metrics, crawling complexity doesn’t get much attention. And even if it did, there is often no easy solution for this.&lt;/p&gt;

&lt;p&gt;However, I’m looking into the future with optimism. Dependency overuse starts getting frowned upon, which is a good thing in my book. Also, a lot of work is being done in the language development field, where new generations of languages are almost always superior to their predecessors: think swift, clojure, elixir, scala, rust. These new tools empower us to do more in less code with less cognitive cost and more safety guarantees. And nothing makes lazy me happier than having more for less.&lt;/p&gt;

&lt;p&gt;A journey of a thousand miles starts with one little step.&lt;/p&gt;
</description>
        <pubDate>Mon, 04 Jul 2016 22:00:00 +0000</pubDate>
        <link>http://markabramov.me/programming/general/2016/07/04/thoughts-on-complexity.html</link>
        <guid isPermaLink="true">http://markabramov.me/programming/general/2016/07/04/thoughts-on-complexity.html</guid>
        
        
        <category>programming</category>
        
        <category>general</category>
        
      </item>
    
  </channel>
</rss>
