Concepts / C++ Class Design Best Practices

C++ Class Design Best Practices

A concrete type is the simplest kind of class.

  • Programming

Why Class Design Needs Restraint

When you design a class, you are deciding how a piece of a program will represent an idea and what that piece will be allowed to do. Not every idea needs the same amount of machinery. Some ideas are simple, and some parts of a program must run quickly. The source describes a particular kind of class for these situations, called a concrete type. This article follows one design decision from start to finish: how to tell whether a concrete type is the right choice, and when it is not.

Keep one question in mind as you read: given an idea you need to represent, what is the least machinery that still represents it correctly? The guideline in this article is an answer to that question.

What Makes a Concrete Type Concrete

Definition

A concrete type is the simplest kind of class.

The word simplest is the key to the definition. A concrete type is not defined by a special feature that sets it apart from other classes. Instead, it is defined by its position at the simple end of the range of classes you could write. When you compare it with a more complicated class, the concrete type asks less of the reader and of the program.

Tracing a Design Choice Step by Step

Worked example

Choosing a kind of class for a temperature reading

A program needs a type that holds one temperature value in degrees and offers a few simple operations on it. Decide which kind of class fits, using the source's preference order.

  1. 1
    Step 1: Name the idea

    The idea is a single temperature reading. It holds one number and does a small amount of work with it. The idea itself is simple.

  2. 2
    Step 2: Check whether the concept is simple

    The source says to judge whether a concept is simple before choosing a concrete type, because the guidance applies where it is applicable. A single reading with a few operations passes this check.

  3. 3
    Step 3: Apply the preference

    The source says to prefer a concrete type over more complicated classes and over plain data structures where it is applicable. Since the idea is simple, the concrete type is the option the source asks you to prefer.

  4. 4
    Step 4: Confirm the reason

    The first situation the source names for concrete classes is representing simple concepts. A simple idea deserves a simple representation, and a concrete type keeps the representation from growing beyond what the idea needs.

Result

A concrete type is the right choice. The representation stays as small as the idea it models.

Three Options on the Preference Scale

When you are shaping a piece of a program, the source gives you three options to weigh: a concrete type, a more complicated class, and a plain data structure. The source gives a clear preference order. Where it is applicable, prefer a concrete type over more complicated classes and over plain data structures. The concrete type is the option the source asks you to prefer when it fits.

Concrete typeSimplest kind of classMore complicatedclassAlternativePlain data structureAlternative
How does a concrete type compare with a more complicated class and with a plain data structure? The concrete type sits in the preferred column.

The phrase where applicable matters. The source does not say the concrete type is always right. It says to prefer it when it applies, which means you first have to judge whether the concept is simple enough for a concrete type to fit. The diagram shows the concrete type in the preferred column and the two alternatives beside it, but the choice depends on that judgment.

OptionRole in the sourceWhen it applies
Concrete typeSimplest kind of class, preferred where applicableSimple concepts and performance-critical components
More complicated classAlternative that the concrete type is preferred overOnly when the concept is not simple enough for a concrete type
Plain data structureAlternative that the concrete type is preferred overOnly when the concrete type does not fit

The source's three options and how the preference applies to each

Two Situations Where Concrete Classes Fit

The second guideline in the source names the two main situations for concrete classes. Each situation has its own reason, so each is worth understanding separately.

Deciding Through Simplicity and Performance

Put the source's guidance into an order of questions. Start with the simplicity judgment, since the preference applies only where it is applicable. If the concept is simple, a concrete type is the choice. If it is not simple, check whether the component is performance-critical, since the source also recommends concrete classes there. If neither holds, the source's preference does not settle the matter, and you weigh a more complicated class against a plain data structure.

Is the conceptsimple?Prefer a concretetypeUse a concrete classIs itperformance-critical?Weigh a morecomplicated class ora plain datastructureyesnoyesno
What sequence of questions should you ask to decide between a concrete type and the alternatives?

Mistakes When Applying the Guideline

  • Common mistake

    Choosing a concrete type without judging simplicity first

    What it looks like

    Reaching for a concrete type for an idea that has many variations and behaviors, simply because concrete types are mentioned as preferred.

    Why it fails

    The source says to prefer a concrete type where it is applicable, and it tells you to judge whether the concept is simple before choosing one.

    Do this instead

    Start by judging whether the concept is simple. Only then apply the preference.

  • Common mistake

    Treating the preference as absolute

    What it looks like

    Deciding that every class in the program must be concrete because the source prefers concrete types.

    Why it fails

    The source does not say the concrete type is always right. The phrase where applicable limits the guidance.

    Do this instead

    Use the concrete type when the concept fits, and weigh the alternatives when it does not.

  • Common mistake

    Assuming simple means small in code size

    What it looks like

    Judging a concept simple only because its class has few lines.

    Why it fails

    The source defines a concrete type by its position at the simple end of the range of classes, meaning what it asks of the reader and the program, not a line count.

    Do this instead

    Ask whether the representation asks less of the reader and the program than the alternatives would.

Applying the Guideline to New Cases

MEDIUM

A program needs a type that stores a single pair of coordinates for a point on a screen and supports moving the point by an offset. Using the three-step order from this article, decide whether a concrete type fits. Say which situation it falls under and what you would weigh if it does not fit.

Hints
  • First ask whether the concept is simple.
  • Then check whether the component is performance-critical.
  • If neither holds, compare a more complicated class with a plain data structure.
HARD

A subsystem must represent a family of related behaviors that vary by the kind of item being processed, and the code must work with items through a shared interface. Decide whether a concrete type is the preferred choice here, and explain why the source's phrase where applicable is important in your answer.

Hints
  • Ask whether the concept is simple, in the sense the source uses.
  • Consider whether the idea needs more machinery than a single simple representation.

Key Takeaways

  • A concrete type is the simplest kind of class, defined by its position at the simple end of the range of classes you could write.
  • Where it is applicable, the source asks you to prefer a concrete type over more complicated classes and over plain data structures.
  • Judge whether a concept is simple before choosing a concrete type, because the preference depends on that judgment.
  • Concrete classes are recommended for representing simple concepts and for building performance-critical components.

Related concepts

More concepts starting with C