Fundamentals 24 min read

Why Mastering Abstraction Is the Secret to Cleaner, More Maintainable Code

This article explains how abstract thinking underpins software design, illustrates abstraction with visual and code examples, shows the pitfalls of duplicated code and forced casts, and offers practical habits—reading, summarizing, naming, and domain modeling—to strengthen a developer's abstraction skills.

21CTO
21CTO
21CTO
Why Mastering Abstraction Is the Secret to Cleaner, More Maintainable Code

What Is Abstraction

Abstract thinking is the most important cognitive ability for engineers because software technology is essentially an abstract art; we spend most of our time modeling problem domains rather than writing code.

Abstraction is the process of extracting common, essential features from many things while discarding non‑essential ones.

Wikipedia describes abstraction as filtering information to keep only what is relevant to a purpose, e.g., turning a concrete football into the general concept of a ball.

Abstraction and Language Are One

Language and abstraction are inseparable: naming a concept like "cow" expresses the abstract idea that all cows share. Good naming reflects clear abstract thinking and improves code readability.

Abstract thinking uses words (concepts) to judge, reason, and draw conclusions, distinguishing human thought from animal cognition.

The Hierarchy of Abstraction

Using a Picasso abstract cow as an analogy, an abstract class represents the common features of all cows, while concrete subclasses (water buffalo, dairy cow) add specifics. Higher abstraction ignores more details, increasing generalization but reducing expressive power.

Three characteristics of abstraction:

Ignores details (e.g., abstract class or interface).

Represents common properties.

Has a hierarchical level: higher levels have smaller intension and larger extension.

Layered Abstraction in Software

Complex problems require layered abstraction, as seen in the OSI seven‑layer network model where each layer hides details from the one above.

Programming languages evolved through layered abstraction: machine code → assembly → C → Java, each step raising the level of abstraction and productivity.

Repeated Code Is Missing Abstraction

Duplicated code signals a lack of abstraction. Example: a search‑condition assembly routine appears in dozens of places.

//取默认搜索条件</code><code>List<String> defaultConditions = searchConditionCacheTunnel.getJsonQueryByLabelKey(labelKey);</code><code>for(String jsonQuery : defaultConditions){</code><code>  jsonQuery = jsonQuery.replaceAll(SearchConstants.SEARCH_DEFAULT_PUBLICSEA_ENABLE_TIME, String.valueOf(System.currentTimeMillis() / 1000));</code><code>  jsonQueryList.add(jsonQuery);</code><code>}</code><code>//取主搜索框的搜索条件</code><code>if(StringUtils.isNotEmpty(cmd.getContent())){</code><code>    List<String> jsonValues = searchConditionCacheTunnel.getJsonQueryByLabelKey(SearchConstants.ICBU_SALES_MAIN_SEARCH);</code><code>    for (String value : jsonValues) {</code><code>        String content = StringUtil.transferQuotation(cmd.getContent());</code><code>        value = StringUtil.replaceAll(value, SearchConstants.SEARCH_DEFAULT_MAIN, content);</code><code>        jsonQueryList.add(value);</code><code>    }</code><code>}

Simply moving this code to a util class is only half the refactor; we must also introduce meaningful concepts such as SearchCondition and SearchConditionAssembler with proper names.

public class SearchConditionAssembler {</code><code>    public static SearchCondition assemble(String labelKey){</code><code>        String jsonSearchCondition = getJsonSearchConditionFromCache(labelKey);</code><code>        SearchCondition sc = assembleSearchCondition(jsonSearchCondition);</code><code>        return sc;</code><code>    }</code><code>}

Forced Type Casting Indicates Bad Abstraction Level

Violating the Liskov Substitution Principle (LSP) forces instanceof checks and casts, e.g., selecting tasty fruit:

public class FruitPicker {</code><code>    public List<Fruit> pickGood(List<Fruit> fruits){</code><code>        return fruits.stream().filter(e -> check(e)).collect(Collectors.toList());</code><code>    }</code><code>    private boolean check(Fruit e) {</code><code>        if(e instanceof Apple){</code><code>            if(((Apple) e).isSweet()) return true;</code><code>        }</code><code>        if(e instanceof Watermelon){</code><code>            if(((Watermelon) e).isJuicy()) return true;</code><code>        }</code><code>        return false;</code><code>    }</code><code>}

Raising the abstraction level by adding isTasty() to Fruit eliminates casts and satisfies LSP.

public class FruitPicker {</code><code>    public List<Fruit> pickGood(List<Fruit> fruits){</code><code>        return fruits.stream().filter(e -> e.isTasty()).collect(Collectors.toList());</code><code>    }</code><code>}

How to Improve Abstract Thinking

Read More

Reading books forces you to form mental images and practice abstraction, unlike passive video consumption.

Summarize and Consolidate

Write summaries in your own words to deepen understanding and sharpen abstraction.

Naming Practice

Every variable, method, or class name is an opportunity to exercise abstraction; clear names reflect clear thinking.

Domain Modeling Practice

Analyzing and modeling problem domains, or studying well‑designed open‑source code (e.g., Spring’s Bean abstractions), trains abstraction and modeling skills.

Summary

Abstraction is the core cognitive skill for programmers, involving finding commonality, generalizing, and modeling concepts.

Language and abstraction are inseparable; good naming reveals clear abstract thinking.

Abstraction has hierarchical levels: higher levels increase generality but reduce expressive detail.

Choosing the right abstraction level is a key design skill.

Duplicate code and forced casts signal missing or misplaced abstraction; refactoring should introduce proper concepts and names.

Deliberate practice—reading, summarizing, naming, and domain modeling—significantly improves abstraction ability.

[1] https://baike.baidu.com/item/抽象/9021828
[2] https://zh.wikipedia.org/wiki/抽象化
[3] https://baike.baidu.com/item/抽象思维
[4] https://www.sohu.com/a/359915387_260616
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Code Refactoringsoftware designobject‑oriented programmingDomain Modelingnamingabstraction
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.