Design Your Codebase with Low Fan-out, High Fan-in Classes
Tags: programming, bestpractices, codesmells
Published: 2022-08-31 15:41:19

---
title: Design Your Codebase with Low Fan-out, High Fan-in Classes
published: true
date: 2022-08-29 00:00:00 UTC
tags: programming,bestpractices,codesmells
canonical_url: http://calebhearth.com/fan-out-vs-fan-in
---

Smaller, simpler classes are easier to reason about. The size of a class is easy to measure with line count or method count, but complexity is more difficult to quantify. One such measure is [cyclomatic complexity](https://linearb.io/blog/cyclomatic-complexity/), a count of the different paths through code.

Another way of measuring complexity of code is with **fan-out** —how many classes (or modules) a class interacts with—and **fan-in** —how many other classes collaborate with the class.

Take for example, these two designs for `DownloadAndCombineEbookChapters`, a class that that scrapes a user page for purchased ebooks, downloads .zips of multiple files, and combines them into a single file with chapter markers.

```
class DownloadAndCombineEbookChapters
  def call
    html = GetUserPageFromLibrofm.()
    page = Capybara.string(page)
    ebooks = ExtractEbooks.(page).map { Ebook.new(...) }
    ebooks.map do |ebook|
      chapter_files = ebook.download_urls.map do |url|
        Tempfile.create { |f| f.write(LibrofmAPI.connection.get(url)) }
      end
      CombineEbookChapters.(chapter_files)
    end
  end
end

```

[![A dependency graph showing this implementation of the class DownloadAndCombineEbookChapters connecting directly to five classes that it interacts with: GetUserPageFromLibrofm, Ebook, ExtractEbooks, LibrofmAPI, and CombineEbookChapters.](http://calebhearth.com/assets/threadsewn-high-fan-out-light-694a70905b9d653d25064f4084898db60b8baac872b29b9c7daf241158ef1bfb.svg "A dependency graph showing this implementation of the class DownloadAndCombineEbookChapters connecting directly to five classes that it interacts with: GetUserPageFromLibrofm, Ebook, ExtractEbooks, LibrofmAPI, and CombineEbookChapters.")](/threadsewn-high-fan-out.dot)
_The dependency graph in this method is flat and broad._

This high fan-out design class depends on `GetUserPageFromLibrofm`, `ExtractEbooks`, `Ebook`, `Librofm`, and `CombineEbookChapters`. This is 5 domain classes, plus `Tempfile` and `Capybara` if you chose to count these toward the collaboration count. There is a mix of imperative calls to service objects and declarative work being done inline.

```
class DownloadAndCombineEbookChapters
  def call
    GetEbooksFromLibrofm.().map do |ebook|
      ebook_chapters = DownloadEbook.(ebook)
      CombineFiles.(ebook_chapters)
    end
  end
end

```

[![Another dependency graph showing the second, lower fan-out implementation of the class DownloadAndCombineEbookChapters. This one shows DownloadAndCombineEbookChapters connected directly to only three collaborator classes: GetEbooksFromLibrofm, DownloadEbook, and CombineFiles. GetEbooksFromLibrofm also has connections to GetUserPageFromLibrofm and to Ebook. GetUserPageFromLibrofm connects to LibrofmAPI. DownloadEbook connects to LibrofmAPI and to Ebook as well, giving these two classes a higher fan-in.](http://calebhearth.com/assets/threadsewn-light-41763d62054390d039cd4ecc354e5789d303c7501ca2e0cfaa54836058464616.svg "Another dependency graph showing the second, lower fan-out implementation of the class DownloadAndCombineEbookChapters. This one shows DownloadAndCombineEbookChapters connected directly to only three collaborator classes: GetEbooksFromLibrofm, DownloadEbook, and CombineFiles. GetEbooksFromLibrofm also has connections to GetUserPageFromLibrofm and to Ebook. GetUserPageFromLibrofm connects to LibrofmAPI. DownloadEbook connects to LibrofmAPI and to Ebook as well, giving these two classes a higher fan-in.")](/threadsewn.dot)
_The refactored class has a narrower, deeper dependency graph that indicates lower fan-out in classes doing work as well as higher fan-in for utility classes Ebook and LibrofmAPI._

Refactored to push dependencies into its collaborators, this low-to-medium fan-out class interacts only with three other classes, down from five. It’s also much easier to understand what is happening in this class than to follow the logic in the original implementation. You can imagine that references to `Ebook` and `LibrofmAPI` are now shared between `GetEbooksFromLibrofm` and DownloadEbook. This increases the fan-in and reuse of these two utility classes: a data object and an API adapter.

### Where do these names come from?

The terms fan-out and fan-in have been used in reference to software design at least since 1994 and were also used in _[Code Complete](https://app.thestorygraph.com/books/946c8a4d-39e9-4a55-b879-898643ab7c75)_, which notes that high fan-in implies “good use of utility classes” and that high fan-out indicates that a class may be “overly complex”. The terms likely originate from logic circuit design, where fan-out is the number of outputs connected to a logic gate and fan-in is the number of inputs to the gate.

Quick note: throughout this post, we’ll use “class” to refer to classes, modules, types, etc.

## Fan-out

A class’ fan-out is the number of classes that it directly collaborates with. More collaborators probably means that your object is responsible for too many things. Fewer outside collaborators means less work being done by a class, making it safer to modify, easier to understand both in isolation and as a part of the system, and more likely to be reusable.

As fan-out increases in a class, consider it a code smell that is getting stronger. As with any smell, high fan-out does not necessarily mean that a class needs to be changed, but it should attract attention and the decision to ignore the smell or make a change should be intentional.

It’s difficult to suggest a specific number of collaborators to recommend, but certainly single digits. I’ve seen 7 recommended, but that feels very high and I would be suspicious of any code depending on more than 3-5 classes by name.

## Fan-in

The number of classes that depend on a class is its fan-in, and having classes in your system with a high fan-in is very desirable.

Being relied upon by many parts of your system indicates that the class provides a generic, useful logic may have been extracted from one or more other classes which independently needed and implemented the functionality. It is an indication of succinctness in code.

Code that has high fan-in tends to be useful even outside of your own codebase, which makes it a prime candidate for extraction to a shared library and for open sourcing.

## Some Fan-out is a good thing

This advice could be misconstrued to mean that rather than using a collaborator a class should implement work itself. This is not intended. Imagine that instead of a mix of imperative calls and declarative work, `DownloadAndCombineEbookChapters` above inlined all work. It would be a large class, and prone to becoming even larger and harder to maintain as complexity increased, such as if a new source of ebooks besides `LibrofmAPI` were introduced.

Rather, it is desirable to break this work into smaller objects and to push those utility classes further down in the dependency graph as high fan-in classes which can do this work in a generic manner useful in multiple places in your system. It is good to have a little bit of fan-out when it means that your class remains easy to understand, change, and test.

## Designing Your System

Strive to create a system with very few high fan-out objects and many high fan-in objects.

One way to limit the number of collaborators is to introduce intermediate classes that encapsulate the behaviors you need in your class, remembering to consider the [Law of Demeter](/law-of-demeter). Doing so reduces the number of direct collaborators—and therefore, fan-out—while potentially helping to identify shared behaviors, increasing fan-in.

[![A directed graph showing a Blog App node connected to 9 submodules: Post, Author, Comment, Highlight, Like, Repost, User, Tag, and Category](http://calebhearth.com/assets/high-fan-out-light-290516488724f0b5fef48de9ede7f714f5a1811c8b9d07ed3b7d4d27b605ddce.svg "A directed graph showing a Blog App node connected to 9 submodules: Post, Author, Comment, Highlight, Like, Repost, User, Tag, and Category")](/high-fan-out.dot)
_The Blog App module has too many concerns - it fans out_

[![Another graph with Blog App's collaborators collected into sub-modules: Social System connects to Highlight, Like, Repost, and User; Author remains directly connected from Blog App but now also depends on User; Blog App also continues to rely on Post but Post now depends on Comment, Tag, and Category.](http://calebhearth.com/assets/lower-fan-out-light-d3634d6b7c1379fa6b01b50b3235f3e1c9788d9bd4c2d9c8b1d2ae5930e7c897.svg "Another graph with Blog App's collaborators collected into sub-modules: Social System connects to Highlight, Like, Repost, and User; Author remains directly connected from Blog App but now also depends on User; Blog App also continues to rely on Post but Post now depends on Comment, Tag, and Category.")](/lower-fan-out.dot)
_The problem is alleviated by organizing concerns into sub-modules_