What is Abstraction?
As per https://www.collinsdictionary.com/dictionary/english/abstraction I think this definition suits best match for programmers:
"the act of considering something as a general quality or characteristic, apart from concrete realities, specific objects, or actual instances."
Synonyms of abstraction given are: concept, thought, idea, view.
Above information is about the meaning and synonyms about the word: abstraction given in the Collins dictionary.
Abstractions are used regularly in everyday language, we use abstraction when talking about lots of things, such as:
- Chair
- Table
- Computer
- Mobile Phone
These are abstract ideas which everyone understands but people don’t have a mobile phone, they have an exact make of mobile phone but most people don’t talk about the exact type by the abstract type of mobile phone.
Understanding Abstraction & Why is understanding Abstraction important
Abstractions help hide concrete code which can change with abstractions such as interfaces and abstract classes which are less likely to change. Using abstraction limits dependencies in code, reduced dependency means reducing the effect of change in your code.
Two of the key long term goals for developers when writing code
- Increase the readability of the code
- Minimize dependencies in code, reducing the effects of change
Junior developers seem reluctant to use abstractions in code because of a mixture of not understanding how abstractions work and because they are focused on getting the code working and bypass the creation of abstractions, which can lead to big complex methods, few classes and no abstractions.
Junior developers are focused on the short term goal of getting the code working with the intent of leaving the design and creation of abstractions until later (later often doesn’t come).
Abstraction is one of the key principles behind many of the OO design principles such as:
- Inheritance
- Polymorphism
- Composition
Benefits of abstraction:
- Code is easy to understand
- Manages change and the effect of change
- Creates cohesive code – it finds common fields
- Create loose coupling
Abstraction refines concepts to their core values, stripping away ideas to the fundamentals of the abstract idea. It leaves the common details of an idea.
Abstractions make it easier to understand code because it concentrates on core features/actions and not on the small details.
- Encapsulate what varies
- Favor composition over inheritance
- Program to interfaces, not to implementations
- Strive for loosely coupled design between objects that interact
- Depend upon abstractions. Do not depend upon concrete classes
When abstraction proceeds into the operations defined, enabling objects of different types to be substituted, it is called polymorphism. When it proceeds in the opposite direction, inside the types or classes, structuring them to simplify a complex set of relationships, it is called delegation or inheritance.
How to find abstractions?
Common abstractions are
- Nouns – things, people, systems
- Verbs
- Types
- Behaviors
- Is A – Inheritance
- Has A – Behaviors
Actions/Verbs are often abstractions e.g. Drawing, validating, processing, routing.
When I mention types I am referring to Abstractions are useful in life. People use abstractions all the time, you often mention
- Computers
- Chairs
- People
- Cars
You don’t talk about the distinctive type of car but say car in general and people will understand you what you are talking about. The same idea can be used in your code design. You can find a common action like validating, drawing, Updating, exceptions. The common action will be the interface and the concrete classes can hide behind it.
Exceptions are a good example you have a core exception (the ones lazy programmers throw when they don’t want to specify the exact error) and then distinctive exceptions extend it. Exception is the abstraction and InvalidPluginExecutionException , NullReferenceException etc are the implementing concrete classes.
Why Abstractions are so important?
Change can ripple through poorly designed code because poorly designed code is fragile, you change one part of it and an unrelated part of the code suddenly breaks. The reason for this sudden breaking is dependency.
Abstractions (interfaces/Abstract classes) reduces dependencies and the abstraction acts as a barrier to dependency and change.
Abstractions help create well designed code.
The code is easy to understand because if you have captured the right abstractions is obvious what the code is doing.
Abstractions help create loosely coupled code. The reason is the abstraction acts like a type and encapsulates the potential changes behind the abstract class/interface.
To understand why this is import you need to understand the statement
code to an interface, not a concrete class.
A classic abstraction is creating a database layer, an abstract class or interface which parts of the code users to interact with the database. The abstract minimizes the effects of change because the code which interacts with the interface will not be effected by the implementation/concrete class changing. It also allows the code to change in the future and more types being added.
Adding more types will not effect any code which interacts only with the interface.
This brings me nicely to the design principle
Encapsulate what varies
Business rules or algorithms are a good example. When you have a behavior or type you have an Abstraction. Using an abstract class will manage the effects of change and create a loosely coupled system which will manage the effect of change.
Understanding abstractions can make you look at your existing code base from a different perspective because the potential abstractions highlight where the code might change in the future and areas of potential refactoring.
0 comments:
Post a Comment