10

So I just found out about the term single source of truth while watching video series about programming code interviews.

From what I understood it meant that the single source of truth is an encapsulation of any type of operation/logic that might be used multiple times.

It reminded me very strongly of the SRP which is pretty much the same thing in a sense that it encapsulates certain operations and/or logic that might be used for multiple times.

Is there something very subtle that I am missing here?

Thank you all for your time.

2 Answers 2

16

They're two different principles. Pretty much the only thing they have in common is the word "single."

Single Source of Truth is the end result of the process of normalizing a database. Every piece of entity information is stored once, and only once.

Single Source of Truth explains why we put Customers in one table, and Products in another. By associating customers with products, using a CustomerProducts table, we avoid storing either customers or products in two different places, and instead put pointers in the CustomerProducts table that point to each customer and product. This also allows us to associate multiple customers with a product, and multiple products with a customer, without duplicating information such as the product name or its price.

Having a Single Source of Truth (each datum is stored in only one place) means that, when you change that datum, the entire system sees the same change at the same instant. Contrast that with multiple sources of truth, where you have to change the datum in all places where it is stored. Different parts of the system might see two different values for the same datum, at least temporarily.

Single Responsibility Principle (SRP) means that a class should only have one responsibility or one reason to change. The example that Fowler gives is that of a Modem class:

interface Modem
{
    public void dial(String phoneNumber);
    public void hangup();
    public void send(char c);
    public char recv();
}

This class violates SRP, because it has two major responsibilities: establishing a connection, and sending data. To correct the problem, you would split the interface into two different interfaces: a connection interface, and a communication interface. The first interface would contain the dial and hangup methods, and the second interface would contain the send and receive methods.

SRP is not a law, but merely a principle. Sometimes SRP is violated for convenience or other reasons. The same is true of database normalization; sometimes data is kept in a denormalized form (includes some duplication) for performance or other reasons.

Persistence Ignorance is another form of SRP: a class should have no knowledge of how to save itself to a data store. That's not its responsibility; it's the responsibility of some other class. Were this not the case, you would have to change every class that uses the data store if you wanted to change the data store to some other type of data store.

7

Umm, no? You seem to be missing fairly significant distinctions!

The single responsibility principle doesn't encapsulate anything. It does not de-duplicate any behaviors/code/logic. All it says is that a class should do one thing. You can still have two classes that do the same thing. The principle is totally fine with that.

The single source of truth doesn't encapsulate anything. It is a guideline that helps you clarify your design. Very often, you have two or more versions of the same data. One in a database, one in a cache, one in memory, one in the clientside, one in sqllite sitting on a PC somewhere... Single source of truth is something you designate. You say that one is the one that wins when your sources disagree. That version is truth.

In short, these are decidedly unrelated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.