Abstraction is not vague, it is a specific behaviour to expose

The dictionary meaning of abstraction points out to something that is not concrete yet or a theoretical one. In programming abstraction means specific. My definition may be confusing, but if we use abstractions right then we care only about the specific behaviour of something we are dealing with.

Photo by Cassi Josh on Unsplash

Throughout my college days I have always understood abstraction as information hiding. It is that mindset that contributes to think how to hide information instead of how to express specific behaviours. My understanding improved when one of my mentors gave me the SICP book. I did not read all the chapters but on reading this book I thought I should have started to program only after reading this book. There is no assignment operator example for several pages when you start reading this, which emphasises on how to structure a program than give instructions to the computer.

Most of us are using procedural abstractions in one form or the other. People using languages like Java use interfaces and abstract classes to provide specific behaviour, dynamic languages rely on duck typing (as long as that signature is available the behaviour is available) and then my favourite pure functions.

Let us look at these two functions sum and product. They both will take a list and give back a number

sum (1,1,1,1,1) → 5

product (1,1,1,1,1) → 1

Both are doing similar transformations, They take a list and give a number

(List) → Number

Now let us bring another function join which takes a list and gives a string.

join(“hello”,”world”) → “helloworld”

Now what are the specific similarities, both take a list and give something as an output. Can we be more specific here? Yes, they take a list with a specific type and return that specific type.

(List<Integer>) → Integer

(List<String>) → String

All 3 can be said to provide a specific function

(List<Type>) → Type

How can we combine these sum, product & join to provide a single method then?

If we combine them then we need to see it as a function which takes a list, a function of how to combine the list that produces the result. In the functional parlance this is called a reduce operation.

(List<Type>, function()) → Type

The new function that should be passed as an argument will be small and will just tell what to do with the result and next item in the iteration of the list. It will be

function(result, nextValue) → nextResult, which in abstract terms

(Type, Type) → Type

So sum, product and join becomes so small that they have to deal only with what to do in an iteration, the iteration and collation is done by another abstract function.

function(List<Type>, (function(Type, Type) →Type)) → Type

Anyone in the early stages of their programming career it is worth while to go through SICP, there is a site called interactive SICP to help do the exercises online. Remember, abstraction means specific, not vague. I have kept the examples simple to give an idea, the reduce function is called in different names in different languages like inject, fold, reduce etc.

Abstraction to start an engine — Photo by Ashutosh Dave on Unsplash

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store