What is the Interface Segregation Principle?

ISP states that no client should be forced to depend on methods it doesn’t use. In other words, it’s better to have multiple small, focused interfaces than one large, general-purpose one. Keeping interfaces small and focused helps ensure that changes to one part of the system don’t ripple through unrelated parts, making code more maintainable and reducing unintended side effects.

Benefits of ISP

  • Reduced Complexity: By keeping protocols and namespaces focused, you avoid bloated structures that are hard to maintain.
  • Ease of Change: When interfaces are small, changes to one part of the code don’t force changes in unrelated parts.
  • Improved Testability: Focused functions and protocols are easier to test in isolation, leading to more reliable code.

ISP in Clojure

Clojure doesn’t have interfaces in the traditional object-oriented sense, but it does have protocols, multimethods, and namespaces. ISP is about thinking carefully about what functions or methods a client should have access to and not overloading them with unnecessary functionality. With Clojure, the goal is to create protocols or functions that do exactly what they need—no more, no less.

Keeping your namespaces small and focused can help avoid creating large modules where consumers end up depending on unnecessary functions.

Suppose you have a namespace shapes.core that includes functions for both 2D and 3D shapes:

(ns shapes.core)

(defn area-circle [radius] (* Math/PI radius radius))
(defn volume-sphere [radius] (* 4/3 Math/PI (Math/pow radius 3)))

If you were to refactor these into more focused namespaces, clients working with 2-d shapes only won’t have to depend on 3-d shapes.

(ns shapes.two-d)

(defn area-circle [radius] (* Math/PI radius radius))

(ns shapes.three-d)

(defn volume-sphere [radius] (* 4/3 Math/PI (Math/pow radius 3)))

The Interface Segregation Principle is all about avoiding the burden of unnecessary dependencies. In Clojure, this means keeping protocols, functions, and namespaces small and focused. By following ISP, you make your code easier to extend, easier to change, and easier to understand. So, as you design your next protocol or function, think about how to keep it lean and aligned with ISP!