Separation of Concerns

Separation of Concerns (SoC) is a principle of design used in computer programming. Separation of concerns results in modular programs. The idea of separation of concerns is that each part of a computer program should deal with one concern, and that each of those sections of code should be broken down into more targeted concerns, preventing the same module of code from addressing more than one concern at a time.

Concerns have different granularities, so that the 10,000 foot view of a solution will have separations that may be addressed by different company divisions, and more focused concerns would be addressed by teams of programmers, and so on down to the most granular of concerns being addressed by individual programmers or projects.

From a Java point of view, concerns may be separated by process, package, class, object, and method as the concerns move from the broad concepts down to very granular individual needs of the application.

For example from a Java point of view, you might have a web application that is broken up into sections that individual teams create. A typical Java web application might follow the Model View Controller (MVC) pattern, but individual teams handle the database, user interface, and business logic separately. However, in the user interface team, each JSP page is tasked with one or a limited selection of user interfaces that are all grouped by theme, such as logging into the application, or setting up a payment plan for the web application. And continuing to use separation of concern the login User Interface (UI) may split out a user’s Plain Old Java Object (POJO) from calandar object used to set the user’s birthdate.

When the computer code has been written using the principle of separation of concerns, the program becomes very modular. Changes to one part of the program do not have to impact changes to other portions of the program. Refactoring source code within limited scopes becomes possible.

Back to our web application example, if the database team changes MySQL’s schema to make the data access time quicker, the UI team should not care or even notice the changes have occurred except that now the web app is more responsive. This is the beauty of SoC design, and the reason it has become standard across the software industry.

Separation of Concerns is the basis for modern Object Oriented Programming (OOP) used in Java, C++ and C#. Aspect-Oriented Programming (AOP) also uses SoC to separated concerns into objects and aspects. Even Functional Programming (FP) uses SoC to separate concerns into namespaces and functions. Separation of concerns is standard across every computer programming paradigm in current use.

Older programs written before the 1970’s often did not follow the SoC design principles. The dreaded “goto” is from that era. Spaghetti code that one-and-only-one programmer understood often made its way onto the computers of the day. The concept of separating out concerns so that pieces of code could be written independently was not as standard as we find it today.

Edsger W. Dijkstra is credited with coining Separation of Concerns in his 1974 paper titled, “On the role of scientific thought.” As quoted by Wikipedia:

Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. It is, that one is willing to study in depth an aspect of one’s subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. We know that a program must be correct and we can study it from that viewpoint only; we also know that it should be efficient and we can study its efficiency on another day, so to speak. In another mood we may ask ourselves whether, and if so: why, the program is desirable. But nothing is gained–on the contrary!–by tackling these various aspects simultaneously. It is what I sometimes have called “the separation of concerns”, which, even if not perfectly possible, is yet the only available technique for effective ordering of one’s thoughts, that I know of. This is what I mean by “focusing one’s attention upon some aspect”: it does not mean ignoring the other aspects, it is just doing justice to the fact that from this aspect’s point of view, the other is irrelevant. It is being one- and multiple-track minded simultaneously.

By the late 1980’s, SoC had become widely accepted in the computer programming world.