Posts /

Abstraction

Twitter Facebook Google+
19 Apr 2016

Abstraction

Everything should be made as simple as possible, not simpler.

– Einstein

Abstraction is an evolving concept in my mind. From the obscure notion of a library as a hello world newbie to the concepts of encapsulations, inheritance and polymorphism as I become more fluent with the ideas of object oriented programming. These days I am concerned with how to intelligently wire up abstractions, how to refactor the code I've written into more beautiful patterns and gradually hone my ability as an aspiring programming craftsman.


With the succinct overview of abstraction below I hope that others will be able to refer to this and reinforce their understanding or gain a new perspective - basically the unwritten hope of all blog posts ever! Please take my words with a seasoning of caution as I cannot boast any expertise in this domain, and please, please, contact me when you see a conceptual error - criticism is my fertiliser!


Simplification

To my mind, this is the crux of abstraction. Make your source code **visually** simpler, though architecturally more complex.

Less lines of code

Data hiding

As we simplify, complexity is moved into independent parts of the system. If we maintain good variable/function/class names our source code becomes even more intelligible.

Simplifying the interface you manipulate

DontRepeatYourself

Hide multi-use code

ObjectOrientation

The core principles behind Object Orientated Programming boil down to Encapsulation, inheritance and polymorphism. If you can master these, you are well on your way!

Techniques for creating dry, structured code

Encapsulation: Package, and hide, reusable/shared code into an extendable and callable ‘capsule’ (usually a class).

def capsule
end
class ManyCapsules
end

Inheritance: If many objects share functionality, they should obtain it from a common parent object.

class CoreCapsule
end

class SpecificCapsule < CoreCapsule
end

Polymorphism: If that common functionality differs, even a little, the child can override or extend parent functionality.

class CoreHabitat
  def initialize
  end

  def getPrimaryAtmosphericGas
    "Oxygen"
  end
end

class HumanHabitat < CoreHabitat
end

class AlienHabitat < CoreHabitat
  def initialize
  end

  def getPrimaryAtmosphericGas
    "Methane"
  end
end

puts HumanHabitat.new.getPrimaryAtmosphericGas
puts AlienHabitat.new.getPrimaryAtmosphericGas

Oxygen
Methane

OUTPUT

Oxygen
Methane

Notice that, despite both `HumanHabitat` and `AlienHabitat` both representing the same type of thing (CoreHabitat), they output different results. Therefore, we can describe two things that are alike, but give them unique behaviour via polymorphism - cool.

Libraries

Think of individual gems/packages/modules that you use to enhance a project.

A collection of abstracted code

Frameworks

Now contrast the idea of a library to that of something like **Ruby on Rails** of **Django**. Both of these frameworks come bundled with numerous library dependencies - and thus achieve a lot more!

A collection of libraries

In closing

Abstraction provides us a wealth of power enabling us to move away from single file scripting to better translate our thoughts and program complexity to manageable and flexible systems. Learning these concepts can transform your ability as a programmer and enable you to breeze through interviews the average coding interview, since you can almost always anticipate a question related to Object Oriented Programming. Now go read something related to your programming language of choice, or perhaps contrast the types of abstraction presented here to those found in Javascript or functional programming languages.


Perfecting oneself is as much unlearning as it is learning.

– Dijkstra


Twitter Facebook Google+