Code That Breaking SRP

What is SRP?

Single responsibility principle (SRP) states that a class or module should have one, and only one reasone to change.

Robert Cecil Martin (2008) Clean Code, : .


public class UserValidator {
  private Cryptographer cryptographer;
  public boolean checkPassword(String userName, String password) {
    User user = UserGateway.findByName(userName);
    if (user != User.NULL) {
      String codedPhrase = user.getPhraseEncodedByPassword();
      String phrase = cryptographer.decrypt(codedPhrase, password);
      if ("Valid Password".equals(phrase)) {
        return true;
    return false;

Robert Cecil Martin (2008) Clean Code, : . - Listing 3-6

Doing something else than checking password. Session.initialize()


public Money calculatePay(Employee e) throws InvalidEmployeeType {
  switch (e.type) {
    return calculateCommissionedPay(e);
  case HOURLY:
    return calculateHourlyPay(e);
  case SALARIED:
    return calculateSalariedPay(e);
    throw new InvalidEmployeeType(e.type);

Robert Cecil Martin (2008) Clean Code, : . - Listing 3-4

There is more than one reason for it to change


public class Sql {
  public Sql(String table, Column[] columns)
  public String create()
  public String insert(Object[] fields)
  public String selectAll()
  public String findByKey(String keyColumn, String keyValue)
  public String select(Column column, String pattern)
  public String select(Criteria criteria)
  public String preparedInsert()
  private String columnList(Column[] columns)
  private String valuesList(Object[] fields, final Column[] columns) private String selectWithCriteria(String criteria)
  private String placeholderList(Column[] columns)

Robert Cecil Martin (2008) Clean Code, : . - Listing 10-9

Class must change when we add a new type of statement & when we alter the details of a single statement type


object-oriented clean-code


comments powered by Disqus