Abstraction: Why hiding implementation details is important ?

Why hiding implementation details is important ?

So when we say hiding implementation details it doesn’t literally mean that you hide your code behind the curtains or to some n layers of protection such that no one can see how you have implemented stuffs.

Hiding implementation details just means to make your code feasible for changes and modification without breaking the dependent code.

When you hide your implementation details you say

a house with doors and windows

and not

a box with four walls, with 3 holes, two of which fit panes of glass surrounded by wood frames, one that fits a large plank of wood with hinges and a metal knob, etc.

A good class interface is the one which allows you to focus on the interface without needing to worry about its internal implementation.

Blah that’s all theoretical and bookish….

So what if i don’t Abstract my code ?

Cool then for an instance let us consider a project without abstraction at all. So under that circumstances you have values placed at different places. If you have some functionality which is to be used more than once then you basically copy and paste your code and if in case different versions of that functionality is required then you basically add ‘if’ constructs and other conditional stuffs and use the same piece of code at different places. Manhhh it works, but few days and months down the line you would be battling with your code:

  1. When you find a bug in your code and you have to fix it. Now since you have copy pasted your code then you have to hunt at all the occurrence of the code all through your project and make changes.
  2. Now in order to make some change in the code the other programmer should understand the existing code but the complexity increases when the size of the code increases. And without the abstraction being in place the size of code base increases constantly with each piece of code getting added. That’s really fine and doable for some small size of code but as the size of the code base increases it becomes really difficult in making these changes.

So it becomes really becomes managing and maintaining code which is not abstracted.

The most important benefit of the abstraction is that it hides irrelevant details. And most importantly all the objects which we see around are abstraction of some kind. When we say house, doors, windows they are abstraction of some kind.

Benefits of Abstraction

  • Hiding complexity so that your brain doesn’t have to deal with unless you’re are concerned about it.
  • Hiding sources of change so that when the source changes the effects are localized.

 

References

Code Complete 2 by Steve McConnell