← Back to list

Single Source of truth (SSOT) — like DRY, or better?

In my first blog, I discussed the principle of “Don’t Repeat Yourself” (DRY) and how it can help us write code that is easier for others…

David Mostoller · 2024-08-31 19:07 · 4 claps · 3.5 min read
#software-development #ssot #dry #coding
Open on Medium ↗
Wiki topics: 💻 · Programming

Single Source of truth (SSOT) — like DRY, or better?

In my first blog, I discussed the principle of “Don’t Repeat Yourself” (DRY) and how it can help us write code that is easier for others to understand, and code that is more modular, adaptable, and scalable. I used some vanilla JavaScript as an example and explained how React enables JavaScript code that is much DRY-er than its vanilla counterpart.

Today, I want to talk about the Single Source of Truth (SSOT), which in many ways is similar to DRY. Both principles aim to ensure that code or data is not duplicated throughout your application or system. However, SSOT focuses not on repeating code but rather on ensuring that data is centralized. With SSOT, your data is stored in a single location and referenced from there. While you may still write some duplicate code, the consistency and reliability of your data improve significantly.

All joking aside, ensuring our data lives in one place provides numerous advantages to our code. It reduces redundancy, minimizes the risk of inconsistencies, and simplifies the process of updating and maintaining your data.

Understanding SSOT with a Simple Example

Let’s explore SSOT with a simple example involving three classes with a many-to-many relationship. In this example, we have Games and Players connected by Results. The Results class will act as our SSOT. In this simple system, we'll see the difference between DRY and SSOT principles.

First, let’s look at our Result class. It holds references to a player, a game, and a score. The player and game are instances of the Player and Game classes, respectively, and the score is a number.

class Result:
    all = []

    def __init__(self, player, game, score):
        self.player = player
        self.game = game
        self.score = score
        self.__class__.all.append(self)

    @property
    def player(self):
        return self._player

    @player.setter
    def player(self, value):
        from classes.player import Player
        if isinstance(value, Player):
            self._player = value

    @property
    def game(self):
        return self._game 

    @game.setter
    def game(self, value):
        from classes.game import Game
        if isinstance(value, Game):
            self._game = value

    @property
    def score(self):
        return self._score 

    @score.setter
    def score(self, value):
        if isinstance(value, int):
            self._score = value

Applying SSOT in the Game Class

The Game class has a title and a few aggregate methods. If we examine these methods, we can see how applying SSOT fulfills our goal of DRY code, even though we still end up with multiple functions that contain similar or nearly identical code.

class Game:
    def __init__(self, title):
        self.title = title

    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, new_title):
        if isinstance(new_title, str):
            self._title = new_title

    def results(self):
        from classes.result import Result        
        return [result for result in Result.all if result.game == self]

    def players(self):
        return list(set([result.player for result in self.results()]))

    def average_score(self, player):
        player_scores = [result.score for result in self.results() if result.player == player]
        if player_scores:
            return sum(player_scores) / len(player_scores)

By referencing our Single Source of Truth (the Result class), we can easily access and iterate through our data from other components. This allows us to write custom functions that perform specific tasks on data stored elsewhere.

Leveraging SSOT in the Player Class

Continuing to our Player class, we see that in just the same way, we can access the same data from another class entirely. By maintaining a single repository of all the data relevant to all three classes in one source, we ensure consistency, accuracy, and ease of maintenance.

class Player:
    def __init__(self, username):
        self.username = username

    @property
    def username(self):
        return self._username

    @username.setter
    def username(self, new_username):
        if isinstance(new_username, str):
            self._username = new_username

    def results(self):
        from classes.result import Result
        return [result for result in Result.all if result.player == self]    

    def games_played(self):
        return list(set([result.game for result in self.results()]))

    def played_game(self, game):
        return game in self.games_played()    

    def num_times_played(self, game):
       return len([result for result in self.results() if result.game == game])

Why SSOT Matters

The key advantage of SSOT is that it centralizes your data, reducing the risk of discrepancies and simplifying updates. For example, in the Player and Game classes, all references to players, games, and their results are consistent because they all refer back to the Result class. This setup not only keeps your code DRY by eliminating redundant data storage but also ensures that your data is always up-to-date across the entire system.

Conclusion

The Single Source of Truth (SSOT) principle is a powerful concept in software development. It extends the ideas of DRY by focusing on data rather than just code. By ensuring that data is stored and maintained in one place, SSOT helps create more reliable, maintainable, and scalable systems. Whether you’re working with small-scale projects or large applications, embracing SSOT will lead to cleaner, more efficient, and more consistent code.


메타데이터
post_id
f7dc8fba013e
slug
single-source-of-truth-ssot-like-dry-or-better-f7dc8fba013e
url
https://medium.com/@dmostoller/single-source-of-truth-ssot-like-dry-or-better-f7dc8fba013e
canonical_url
https://medium.com/@dmostoller/single-source-of-truth-ssot-like-dry-or-better-f7dc8fba013e
author_url
https://medium.com/@dmostoller
status
ok
fetched_at
2026-07-22 23:41:08