Single Responsibility Principle in JavaScript
In object-oriented programming, the single responsibility principle states that every context (class, function, variable, etc.) should have a single responsibility, and that responsibility should be entirely encapsulated by the context. All its services should be narrowly aligned with that responsibility.
JavaScript is not a true object oriented language but that doesn't mean we can’t leverage some of the benefits of thinking in terms of the Single Responsibility Principle. If constructs within your application adhere to SRP they naturally become easier to understand, easier to maintain and most importantly adaptable to change.
The responsibility is defined as a charge assigned to a unique actor to signify its account abilities concerning a unique business task.
Adaptability to change or more specifically understanding what makes an object or function mutable is key in understanding the Single Responsibility Principle. To understand an objects responsibility we must first understand our actors and what business objective these actors are trying to achieve.
We know that our logic is breaking the Single Responsibility Principle when an object, or function in our application has more than one reason to change. Objects in a high state of mutability create unintended cohesion between business logically separated concerns. This cohesion or coupling mean changes to any subsystem have far reaching affects across your entire applications.
Email me when Eric Clifford publishes or recommends stories