%% date:: [[2021-08-20]], [[2021-02-25]], [[2024-01-11]] parent:: %% # [[Respect Levels of Abstraction]] Abstraction means to put a name on a concept to make it more digestible. [^stackoverflow] In [[Software Development]], this entails programming at a level that is [[Coding and writing are converging|closer to natural speech than code]]. The higher the level of abstraction, the lower the level of specificity and accuracy, as there is a trade-off between the two. Abstraction is necessary for the creation of a [[Mental models|Mental model]] that omits some details to describe something that is more generally true. Application code can be thought of as a stack of tasks that range from the most concrete to the most abstract. Each level describes a broader concept than the one before it. ![[respect_levels_of_abstraction1.png]] - [Source](https://simpleprogrammer.com/respecting-abstraction/) [^01] ![[Respecting Levels of Abstraction - Simple Programmer#^abfb8b]] Thinking of code in terms of these levels is useful as a reminder to avoid code that spans multiple levels. Respecting levels of abstraction is a central point in programming. ## Features of code that respects levels of abstraction ### [[Polymorphism]] Polymorphism in programming means decoupling the _what_ code (code that describes an object) from the _how_ code (code that describes how a particular object is interacted with or instantiated). ### Good naming and encapsulation We can respect levels of abstraction by naming objects or functions in a way that is appropriate for their level of abstraction and by making sure they're consistent with parent and child functions. ### Concision and expressiveness We need to strike a balance between [[Clean Code]] and code that is readable. Instead of: ``` void saveIndex(Index const& index) { if (index.hasID() && index.isQuoted() && index.isLiquid()) { ``` [^01] we should consider abstracting out the boolean checks into another function so that we can rewrite it like this: ``` void saveIndex(const Index& index) { if (isValid(index)) { ``` ## References [^01]: [[Respecting Levels of Abstraction - Simple Programmer]] [^stackoverflow]: [[What Does Abstraction Mean in Programming]]