Leaky Abstractions: The initial sign of tightly coupled systems



Imagine a scenario where you are creating an application for an operating system. while building this app you get an exception saying, “Unable to write memory address 0x08048faf”. That’s amazing, now what are you supposed to do? Find that address and check for the issue? It will be a daunting task to figure out what exactly went wrong. But what about the issue at hand, the problem that your app is supposed to solve. Because the operating system failed to abstract its implementation details, you must investigate the issue which is not your problem.

Abstractions are amazing, they are here for hiding the irrelevant to focus on relevant. while building complex systems, developers heavily rely on abstraction. Each level of abstraction tries to hide the underlying level of complexity of the implementation, Letting the developers focus on the actual problem at hand. Memory management in the managed programming environment is a great example of properly implemented abstraction. Developers who write code for the managed environments like, .Net or Java do not have to care about the memory management, it just works. Developers are free from all the overhead from writing the code which can clean the memory whenever there are objects which are not in use, checking the memory for memory leaks and knowing how the objects are allocated in the memory (which is a huge thing). Another great thing about this abstraction is, the developers doesn’t have to configure their classes in a certain way so that the underlaying framework (Garbage Collector) will clear the memory occupied by the instances of that class.

Now let’s look at how ORM (Object-relational mapping) like entity framework works. ORM’s are great, they are there so that the developers from the OO (Object Oriented) world, where data is interconnected graph of objects, shouldn’t have to worry about Relational databases where data is represented in tabular format. ORM framework tries to solve this mismatch between OO world and Relational world. In other words, it tries to abstract details of relational world from OO. But the problem with ORM tools is that, before using them, it requires a lot of configuration, this couples your application to the ORM framework. For complex or Custom queries developers are forced to write ad-hoc class structures or writing custom code to get the data they need or in some cases to write stored procedures. Which means developers can’t really work without knowing how the relational databases work. The ORM framework here is exposing plenty of details to the consumers, the abstraction is leaked!

If abstraction were perfect, we use them, and we don’t have to worry about the layers it is hiding and enjoy the simplicity. Since abstractions leak we must also learn the lower layers. If we keep building this stack of layers, the amount of knowledge required grows with it. Leaky abstractions also make your system tightly coupled, If the abstraction requires you to do a lot of configuration before using, it is an initial sign that your code is going to be coupled with that component, also it requires you to have some knowledge about the abstraction being used. All abstractions to some degree are leaky, try to minimize the details being leaked, make sure the users of your abstraction are not required to configure a lot of things before using it. If you spot any abstraction are exposing too many details to the other layers, you can minimize the damage by adding a layer and hide those details.

Comments

Popular posts from this blog

Threading in .Net – Part I – The Basics.

Writing High Quality Functions