Bugs in Commonly-Used Classes Have Wide Effects

Learn about the impacts stemming from bugs in commonly utilized Rails classes.

Let’s talk about the interdependence of pieces of code. Some methods are called in only one place in the application, while others are called in multiple places.

How does the controller method work?

Consider a controller method. In most Rails apps, there is only one way a controller method gets called: when an HTTP request is issued to a specific resource with a specific method. For example, we might issue an HTTP GET to the URL /widgets. That will invoke the index method of the WidgetsController.

How does the find method work?

Now, consider the find method on User. This method gets called in many more places. In applications that have authentication, it’s possible that User.find is called on almost every request. Thus, if there’s a problem with User.find, most of the app could be affected. On the other hand, a problem in the index method of WidgetsController will only affect a small part of the app.

We can also look at this concept at the class level. Suppose User instances are part of most pieces of code, but we have another model called WidgetFaxOrder that is used in only a few places. Again, it stands to reason that bugs in User will have wider effects compared to bugs in WidgetFaxOrder.

While there are certain other confounding factors (perhaps that WidgetFaxOrder is responsible for most of our revenue), this lens of class dependencies is a useful one. The concepts here are called as:

  • fan-out
  • fan-in

Fan-out vs. fan-in

Fan-out is the degree to which one method or class calls into other methods or classes. Fan-in is the inverse of fan-out, the degree to which a class or method is called by others.

What this means is that bugs in classes or methods with high fan-in classes used widely throughout the system can have a much broader impact on the overall system than bugs in classes with a low fan-in.

Explanation

Consider the system diagrammed in the figure below. We can see that WidgetFaxOrder has a low fan-in, while Widget has a high one. And WidgetFaxOrder has only one incoming “uses” arrow pointing to it, whereas Widget has two incoming “uses” arrows but is also related via Active Record to two other classes.

Get hands-on with 1200+ tech skills courses.