Monday, October 24, 2016

Abstraction Layers

An important skill that I think every developer should have if they want to write cleaner code is to understand what abstraction layers are and when abstraction layers are being mixed in code. 

What do I mean by abstraction layers?
"In computing, an abstraction layer or abstraction level is a way of hiding the implementation details of a particular set of functionality, allowing the separation of concerns to facilitate interoperability and platform independence." (Wikipedia)
Computers don't define abstraction layers for you. Programmers do. When you're dealing with an interface of some sort - whether it's a function or some web API, whatever details that exist behind that interface is essentially a different layer or level of abstraction because those details are hidden from you. One layer (in this case, the layer that your program resides in) is concerned with the what and the other layer (the one that's behind an API) is concerned with the how.

Programmers defines those interfaces that separate abstraction layers. They decide what the public sees. Not only that, but internally they must also decide what every part of the system sees - how the individual units interact with each other - and carve out the layers of abstraction that exist within the system. Those decisions are really important. Good abstractions are easy to understand. Bad abstractions are confusing. This is why using clear and descriptive names are important for writing a maintainable program.

This post isn't about how to pick good abstractions using techniques like writing descriptive names, however. It's about why it's so important to be able to recognize when abstraction layers are being mixed. Why? Because in any program, even if the names are sound, it's still a mess if the names of things that belong in different abstraction layers are mixed!

What do I mean by mixed abstraction layers?

Abstraction layers separate the what from the how. When the what and the how are mixed, abstraction layers are mixed. You see this most often in "god" functions. They do everything. Everything exists on the same plane.

Why is this a problem?

As someone reading the program, you're forced to wrangle with details that you may not care about. Here's a good real world example: say you're looking for a recipe for a grill cheese sandwich. You go on the site, and it's about 200 pages long. It doesn't just tell you the ingredients for a grill cheese sandwich, it tells you how those ingredients can be obtained, the history behind each ingredient, the health effects of consuming each ingredient. Shit you don't really care about if all you're trying to do is to make a damn sandwich!

I just need to know that I need sliced cheddar cheese. Not how it's made or how it's sliced (if I care about either of those things I'll dig deeper myself) what the history is (which is completely irrelevant to my concern). Point is, I don't need to see all of that information at once. It's overwhelming.


No comments:

Post a Comment