The single responsibility principle

The single responsibility principle

The single responsibility principle (SRP) instructs developers to write code that has one and only one reason to change. If a class has more than one reason to change, it has more than one responsibility.

The single responsibility principle (SRP) instructs developers to write code that has one and only one reason to change. If a class has more than one reason to change, it has more than one responsibility. Classes with more than a single responsibility should be broken down into smaller classes, each of which should have only one responsibility and reason to change.

This article explains that process and shows you how to create classes that have only a single responsibility but are still useful. Through a process of delegation and abstraction, a class that contains too many reasons to change should delegate one or more responsibilities to other classes.

It is difficult to overstate the importance of delegating to abstractions. It is the lynchpin of adaptive code and, without it, developers would struggle to adapt to changing requirements in the way that Scrum, Kanban, and other Agile frameworks demand.

Problem statement

To better explain the problem with having classes that hold too many responsibilities, this section explores an example. Listing 1 shows the TradeProcessor class. This class reads records from a file and updates a database.

Despite its small size, this sort of code is common and often needs to cope with new features and changes to requirements

class TradeProcessor:
  lots_size: float = 100000.0

  def process_trades(self, stream: Sequence[str]) -> None:
    lines: List[str] = []
    line: str
    for line in stream:
      lines.append(line)
    trades: List[TradeRecord] = []
    line_count: int = 1
    for line in lines:
      fields: List[str] = line.split(',')
      if len(fields) != 3:
        print(f'WARN: Line {line_count} malformed. Only {len(fields)} fields(s) found.')
        continue
      if len(fields[0]) !=6:
        print(f"WARN: Trade currencies on line {line_count} malformed: '{len(fields)}'")
        continue
      trade_amount: Optional[int] = int_try_parse(fields[1])
      if trade_amount is None:
        print(f"WARN: Trade amount on line {line_count} not a valid integer: '{fields[1]}'")
      trade_price: Optional[float] = float_try_parse(fields[2])
      if trade_price is None:
        print(f"WARN: Trade price on line {line_count} not a valid decimal: '{fields[2]}'")
      source_currency_code: str = fields[0][:3]
      destination_currency_code: str = fields[0][3:6]

      trade = TradeRecord(source_currency_code,
                          destination_currency_code,
                          trade_amount/self.lots_size,
                          trade_price)
            trades.append(trade)
      line_count += 1

    engine = create_engine('sqlite:///trades.db', echo=False)
    metadata = MetaData(engine)
    tbl = Table('trade_table',
                metadata,
                autoload=True,
                autoload_with=engine)
    for trade in trades:
      ins = tbl.insert(None).values(
                    source_currency=trade.source_currency,
                    destination_currency=trade.destination_currency,
                    lots=trade.lots,
                    price=trade.price)
            conn = engine.connect()
            conn.execute(ins)
print(f'INFO: {len(trades)} trades processed')

refactoring interface-design agile-methodology refactor python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Identifying Non-Functional Requirements (NFR) As Part of Your Agile Project Inception

Full workshop breakdown on how to identify non-functional requirements (NFR) as a part of an agile project inception, including preparation and execution tips.

Kick-Off Your Agile Team With A Working Agreement Workshop

In this article, I will discuss how I adapted Avi’s original canvas to the needs of the teams I was coaching, elaborate on the different elements of a working agreement, and share with you a step-by-step guide to facilitating collaborative working agreement development workshops.

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.