My Intek
My Intek

Reputation: 95

Respecting Single Responsibility Principle on method level

In the context of email sending, is it ok if to query for external information inside the method or should the value be passed like the second example.

1.

void sendEmail(int companyId) {  
    String companyName = this.selectCompanyName(companyId);
    String body = companyName;
    send(body);
}

2.

void sendEmail(String companyName) {
    String body = companyName; 
    send(body);
}

Upvotes: 1

Views: 196

Answers (1)

Yes, querying from the method is still single responsibility (the method is still doing only one thing: send the email). That of itself doesn't contradict the principle.

What does however, is that the method should be passed the company name directly, and the getCompanyName method should not belong to the same type.

If you want to follow SRP, you have to actually do it on types too: should the class really be responsible for sending and fetching stuff from (presumably) the database?

  • Sending mail to a company name
  • Finding a company name from a company id

Are two separate jobs that each deserve their type.

Upvotes: 3

Related Questions