SOLID CODE: Single Responsibility Principle

Photo by Tim Mossholder on Unsplash
  1. Single Responsibility Principle
  2. Open / Closed Principle
  3. Liskov Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

Introduction

In the first article, I will talk about the Single Responsibility Principle.

Explain the principle

Photo by Michael Heng on Unsplash
SRP

Example

The code below is an example of the SRP violation. This error was often made when I first learned to code. The Student class has too many functions: storing student information, formatting information, and storing information.

public class Student {
public string Name {get; set;}
public int Age {get; set;}

// Format this class as text, html, json to print
public string GetStudentInfoText () {
return "Name:" + Name + ". Age:" + Age;
}
public string GetStudentInfoHTML () {
return "<span>" + Name + "" + Age + "</span>";
}
public string GetStudentInfoJson () {
return Json.Serialize (this);
}
// Store to database, down to file
public void SaveToDatabase () {
dbContext.Save (this);
}
public void SaveToFile () {
Files.Save (this, "fileName.txt");
}
}
// Student now contains only information
public class Student {
public string Name {get; set;}
public int Age {get; set;}
}

// This class only formats information to display student
public class Formatter {
public string FormatStudentText (Student std) {
return "Name:" + std.Name + ". Age:" + std.Age;
}
public string FormatStudentHtml (Student std) {
return "<span>" + std.Name + "" + std.Age + "</span>";
}
public string FormatStudentJson (Student std) {
return Json.Serialize (std);
}
}

// This class only takes care of storage
public class Store {
public void SaveToDatabase (Student std) {
dbContext.Save (std);
}
public void SaveToFile (Student std) {
Files.Save (std, "fileName.txt");
}
}
// Class Helper violates SRP
// But because it's small, we can ignore it
public class Helper {
public string GetUser ();
public DateTime GetTime ();
public string GetCurrentLocation ();
public DbConnection GetDatabaseConnection ();
}
// The helper is big, we need a split
public class Helper {
public string GetUser ();
// ... ..
public DateTime GetTime ();
// ... ..
public string GetCurrentLocation ();
// ... ..
public DbConnection GetDatabaseConnection ();
}

// Split helper into smaller helper
public class UserHelper {
}
public class TimeLocationHelper {
}
public class DatabaseHelper {
}

Conclusion

In essence, a principle is a principle; it is only a guide, not an absolutely unchanging rule. If you investigate carefully, you will find that there are still a few programmers who dissect, object, and point out these principles' flaws. However, understanding them still makes the code we write easier to read, understand, and manage.

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beribey

Beribey

40 Followers

Always be nice to anybody who has access to my toothbrush.