What is abstraction

What is abstraction

Headshot Bronislav Klučka on May 24, 2025

Developers use the word "abstraction" regularly, yet there is limited understanding of what it means. In a broader sense, abstraction has been overshadowed by its use in some languages to denote a method or class that will be overwritten later, as well as the phrase "use abstraction." is understood to mean "write an abstract class.".

Abstraction is one of the most important techniques for achieving decoupling and stabilizing your public interfaces at any level.

Abstraction: A way of presenting an entity's important attributes while hiding its implementation details. Abstraction manages complexity.

Okay, that's nice, but what does it mean? Consider the word "car." It represents a significant abstraction. You know what it's supposed to do. It allows for variability without losing focus on its purpose and hides all implementation details.

What about a bad abstraction? The term "gas pedal" is one example. The terminology is confusing because it mixes up "gas matter" and "gasoline as a liquid." It is also incorrect for electric cars because it shows the implementation details.

Examples

/**
* Simple example
* --------------
* data can be saved to database, file, over network
*/
repository.saveToDatabase(item); // ❌ leaking
repository.save(item); // ✅ Nice


/**
* Auth example
* ------------
* user is authenticated using token
*/
authModule.tokenExists(token); // ❌ leaking
/*
* There may be more to the user being logged in than just the token.
* - The token may exist during registration.
* - The token may exist during 2FA/MFA.
* In those cases, however, the user is still not logged in.
*/
authModule.isLoggedIn(token); // ✅ Nice

/**
* UI example
* ------------
* check what tab has been clicked
*/
// ❌ leaking
const activeTab = tabs.find(tab => {
	return (tab.left < X) && (tab.right > X) &&
		(tab.top < Y) && (tab.bottom > Y)
}) ?? null;
// You assume that the tabs are rectangular.

// ✅ Nice
const activeTab = tabs.getTabFromPoint({x: X, y: Y});
		

Some rules

So, how can you can decide, what the correct abstraction is? Answer the following questions:

  • Is the name "what it does" or is it "how it's done"?
  • Can you change the implementation without changing the calling code?
  • When using a foreign object, are you calling its methods or are you processing its data?

If you answered "yes" to the first part of those questions, then you are probably on the right track.

We use cookies and similar technologies, such as Google Analytics, to analyze website traffic. This allows us to understand how visitors interact with our site.

More info

This website uses Google Analytics, a web analytics service offered by Google. Google Analytics uses cookies to help us analyze how users interact with our site. The information generated by the cookie about your use of the website (including your IP address) will be transmitted to and stored by Google. We use this information to compile reports on website activity and to provide other services related to website and internet activity.

Analytics cookies help us improve our services. We do not use them for marketing or advertising purposes. We do not sell this data.