Prelude to AOP - Requirements, Patterns, SRP and DRY

Featured image

AOP has graduated for quite sometime now from the esoteric reaches of design to become a mainstream pattern. Hence it sounds almost superfluous to describe AOP in an explicit article since there have been multiple attempts to document it already. But I believe that in talking about AOP, I get to talk about good architecture in general. Hence this series of posts attempt to talk about some of my views on architecture in general and how good architecture almost inevitably leads to AOP. In this article we will talk about requirements, principles of good design and the need for AOP.

Why AOP?

To answer this, let us take a small segue into two important principles of Object Oriented Design(OOD)

OOD - Introduction to SRP and DRY

To me there is no other object oriented principle that is more fundamental than the Single Responsibility Principle (SRP). The SRP states a simple OOD axiom, namely - One class implements exactly one responsibility. In short the class should change because of one factor not due to multiple factors. There is a reciprocal statement of the SRP called DRY (Do not Repeat Yourself) DRY states that we should not repeat the same logic in multiple places. In essence, DRY states that one single responsibility should be implemented by one class or a series of classes only. IMO the difference between a good and a bad design lies in the emphasis that is laid in following these two principles. A bad design is inextensible and brittle i.e. a bad design cannot accommodate future changes in requirements without spiraling into a quagmire of destructive code changes. Good design mandates that the changes are kept localized i.e. a change in requirement should not lead to pervasive changes across the code base. Hence good design pays heed to SRP and DRY.

Requirements - An introduction to Horizontal Requirements

In the above discussion, we had talked about requirements in passing. Now let us talk about them in more detail. There are typically two types of requirements for any application namely functional and non functional requirements.

Horizontal Requirements & AOP

Implementing horizontal requirements in functional code, even if it is restricted to availing the features of the application framework used, is in violation of the Single Responsibility Principle (SRP). It is impossible to change the implementation of these horizontal requirements or handle changes in the requirement itself without having to make code changes across the entire application. This is potentially an extremely brittle situation. Yet implementation of these horizontal requirements would call for code that is well integrated with the functional code. To resolve these seemingly paradoxical needs, many patterns have been thought about by application designers. Some of these are listed below and would be elaborated in later blog entries.