Curing primitive obsession with better types 馃獎
By Google for Developers
Key Concepts
- Primitive Obsession
- Custom Abstraction
- Refactoring
- Readability
- Maintainability
- Reusability
Primitive Obsession: The Pitfalls of Overusing Basic Types
The transcript highlights a common anti-pattern in software development known as "primitive obsession." This occurs when developers overuse low-level, built-in data types such as strings, integers (ints), and pairs to represent complex information. While basic types are fundamental, their overuse can lead to code that is difficult to understand, maintain, and reason about.
Example of Primitive Obsession:
The transcript uses the example of a "bounding box" being represented by a "minus bounding box." This implies a situation where a complex concept like a bounding box (likely requiring coordinates, dimensions, etc.) is being shoehorned into a primitive type, making its meaning unclear and its usage prone to errors.
Consequences of Primitive Obsession:
- Reduced Readability: Code becomes harder to decipher because the meaning of primitive types is ambiguous. Developers have to guess what the first or second element of a pair represents, or what a specific integer value signifies.
- Increased Maintenance Burden: Modifying or extending code riddled with primitive obsession becomes a complex task, as the underlying structure is not clearly defined.
- Potential for Errors: Without clear abstractions, it's easier to misuse primitive types, leading to bugs. For instance, using raw numbers for timeouts without a clear unit or context can be problematic.
The Solution: Custom Abstractions and Refactoring
The recommended solution to primitive obsession is to refactor the code by introducing custom abstractions. This involves creating specific data types or classes that clearly represent the intended concepts.
Benefits of Custom Abstractions:
- Improved Readability: Custom types provide self-documenting code. For example, instead of a pair of integers representing coordinates, a
Pointobject withxandyproperties is much clearer. - Enhanced Maintainability: Well-defined abstractions make it easier to modify and extend functionality without introducing unintended side effects.
- Increased Reusability: Clearly defined concepts can be reused across different parts of the codebase or even in other projects.
The Refactoring Process:
The transcript suggests a straightforward approach:
- Recognize the Pattern: Identify instances where primitive types are being used to represent structured or complex information.
- Introduce Custom Types: Create new classes or data structures that encapsulate the relevant data and behavior.
- Replace Primitives: Substitute the overused primitive types with these new, more descriptive custom types.
Analogy:
The transcript uses an analogy: "If your code feels like it's speaking in mono salelabic grunts instead of full sentences, it's time for better types." This vividly illustrates how primitive obsession leads to terse, uninformative code, whereas custom abstractions allow for more expressive and understandable code.
Real-World Applications and Examples
The transcript alludes to common scenarios where primitive obsession manifests:
- Packing structured info into strings: For example, storing a date and time as a single string like "2023-10-27 10:30:00" instead of using dedicated date and time objects.
- Using raw numbers for timeouts: Representing a timeout duration as a raw integer (e.g.,
5000) without specifying units (milliseconds, seconds) or context.
Conclusion and Key Takeaway
The core message is to refactor primitives into clear concepts. By embracing custom abstractions, developers can significantly improve the readability, reasonability, and reusability of their code. The advice concludes with a call to action: "keep it classy and follow for more tips." This emphasizes the importance of writing elegant and well-structured code.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

Why Does This Guy Appear In Kids Videos?
sphynx

TIC en las Organizaciones - Electiva Complementaria II Unisimon
Julieth G眉ell S

How to Tame Your Advice Monster | Michael Bungay Stanier | TED
TED

Margaret Heffernan: Why it's time to forget the pecking order at work
TED

The importance of psychological safety: Amy Edmondson
The King's Fund

What Is Psychological Safety?
Harvard Business Review

13-Conflict Management: Listening in Conflict
Deliberate Development