← home

What is combinatorial explosion?

Combinatorial explosion is the rapid increase in the number of options as parameters increase. In Ruby, this becomes a problem when designing classes like Notification, where hundreds of combinations complicate the co...

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
A combinatorial explosion is a phenomenon where the number of possible options rapidly increases with the addition of elements. Everything seems innocent until you start counting.
For example:
  • We have 3 types of pizza and want to choose 2 — that's just 3 options.
  • But what if we have 20 toppings and want to choose any combination? Already over a million options!
In mathematics, this is related to combinatorics - a branch that studies the ways of selecting and arranging objects.
In programming, artificial intelligence, or game theory, a combinatorial explosion is a real enemy. For example, in chess, the number of possible positions after 5 moves is over 69 billion. Going through all the options is simply impossible — optimizations, heuristics, and workaround strategies are needed.
Combinatorial explosion is a situation where "counting everything" becomes impossible because there are too many options.
The term sounds a bit dramatic - and rightly so. It illustrates well how quickly a simple task can grow into a real mathematical storm.

Combinatorial Explosion in Creating a New Class in Ruby

In Ruby (as in other OOP languages), combinatorial explosion can occur when you try to anticipate all possible combinations of object behavior or dependencies between class parameters.
Example of poorly designed Notification class:
class Notification
  def initialize(user:, type:, channel:, urgency:)
    @user = user
    @type = type         # :comment, :like, :mention, :follow
    @channel = channel   # :email, :sms, :push
    @urgency = urgency   # :low, :medium, :high
  end

  def deliver
    # logic based on all combinations
  end
end
Now we have:
  • 4 types of events (:comment, :like, :mention, :follow)
  • 3 channels of sending (:email, :sms, :push)
  • 3 levels of urgency (:low, :medium, :high)
This already gives 4 × 3 × 3 = 36 combinations, each of which potentially requires separate delivery logic (deliver). Add 2 more parameters — and now we have hundreds of options that are difficult to test and maintain.

How to Avoid Combinatorial Explosion?

1. Strategy Objects:
class EmailNotificationStrategy; def deliver; ...; end; end
class PushNotificationStrategy; def deliver; ...; end; end
2. Separate Responsibilities (SOLID):
  • Notification should not know everything about all channels.
  • Each channel implements its own behavior.
3. Metaprogramming (should be done wisely):
rubyCopyEditdefine_method("deliver_#{channel}_#{type}_#{urgency}") do
  # ...
end
But if there are 100+ methods, it will only worsen the situation. A combinatorial explosion in a class occurs when you try to "sew" too many logic options related to the combination of parameters into the class. Over time, this leads to unreadable code, bugs, and pain.

🔥 More posts

All posts
What is vibe coding?
Jul 25, '25 21:51

What is vibe coding?

Vibe coding is intuitive programming in tandem with AI: without strict rules, with music, inspira...

What is a brain stack?
Jul 28, '25 19:37

What is a brain stack?

Brain stack is a model that describes how the brain works in layers: from neurons to self-awarene...

What is integer overflow?
Programming (Програмування)Aug 15, '25 08:28

What is integer overflow?

Integer overflow is the overflow of an integer when the value exceeds the limit of the variable t...

What is a HAR file (HTTP Archive)?
Programming (Програмування)Aug 25, '25 18:23

What is a HAR file (HTTP Archive)?

HAR file (HTTP Archive) is a .har format that stores a log of the browser's interaction with the ...

What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is a rapid increase in magnitude, where each subsequent step multiplies the re...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is the rapid increase in the number of options, where for n elements the pos...