Good abstractions are often used.
Good abstractions refer to 2 or more places in your software.
Examples:
- Functions with +2 + sites.
- An abstract class with 2+ concrete classes.
- Interfaces with 2+ implementations.
- Common with 2+ instances
- Libraries with 2+ users.
- and etc.
An abstraction that refers to 2 or more places helps reduce code size by decomposing common things, which is good.
But if you have many abstractions that are referenced only once, then this is a good chance that the abstraction is not needed.
Some examples where unnecessary abstractions occur:
- Writing object code (interfaces and abstract classes having only 1 implementation or nodule). These interfaces and abstract classes are not needed (at this moment of development). The principle of YAGNI.
- Someone creates a "brilliant new" interface on the old one, where new functions, after some conversion, call only the old interface. You can imagine what a big mess is the result if you repeat this several times. In this case, the old functions will have one call site, so they are not needed. You need to move the code from the old functions to the new one or not to write a new one and change the old one.
Some examples of really good abstractions:
- Equipment abstraction level: provides a single interface for applications, so they do not need to develop code for each type of equipment.
- File System: It doesn't matter if you use FAT, NTFS, EXT3. It allows you to use files and directories, the file system driver does the rest.
- C language: you do not need to port your program for each CPU architecture. Just compile it.
So, to answer your question pragmatically: Abstractions are bad if they are referenced in less than 2 places .
As for modulation, its subjectivity: you can organize your code the way you want. If the source code of the library is in the same source file, this does not make it worse than if you put several hundred files into it.
When you evaluate your abstractions as good or bad, always consider the big picture: the whole project, the entire product line, etc.
Calmarius
source share