Design Principles – Difference Between Single Source of Truth and Single Responsibility Principle

design-principlessingle-responsibility

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.

Best Answer

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.

Related Topic