Skip to content

RxJS Anti-Pattern Collection

RxJS is a powerful reactive programming library, but when used incorrectly, it can be a breeding ground for bugs and reduced maintainability. In this section, we will introduce some common mistakes when using RxJS with TypeScript and best practices to avoid them.

Purpose of this section

  • Prevent bugs: avoid implementation problems by understanding common mistakes before they happen
  • Improve Maintainability: Learn code patterns that are easy to read and test
  • Optimize performance: learn techniques to avoid memory leaks and unnecessary processing

List of anti-patterns

This section covers the following 17 anti-patterns.

🔴 Critical Issues

These patterns can have a serious impact on your application.

PatternProblemImpact
External publication of SubjectPublish the Subject as it is, and make it possible to call next() from the outsideUnpredictability of state management, difficult to debug
Nested subscribeCalling more subscribe in subscribeCallback hell, complication of error handling
Rampant state management flagsManaging state with 17 boolean flags, imperative thinking still remainsLow readability, difficult to maintain, hotbed of bugs
if statement nesting in subscribeComplex conditional branching (nesting of 3 or more) in subscribeLow readability, difficult to test, violates declarative philosophy
unsubscribe forgettingNot unsubscribing to infinite streamsMemory leak, resource waste
Misuse of shareReplayUse without understanding how shareReplay worksOld data references, memory leaks

🟡 Issues requiring attention

These can be problems in certain situations.

PatternProblemImpact
Side-effects in mapChanging state in map operatorUnpredictable behavior, difficult to test
Ignoring Cold/HotNot considering the nature of ObservableDuplicate execution, unexpected behavior
Mixing with PromiseNot converting Promise and Observable properlyUncancelable, poor error handling
Ignoring backpressureFail to control high frequency eventsPerformance degradation, UI freeze

🔵 Code quality issues

These are not direct bugs, but they are factors that reduce code quality.

PatternProblemImpact
Error suppressionNot handling errors properlyDebugging difficulties, poor user experience
DOM event leaksNot freeing DOM event listenersMemory leaks, performance degradation
Lack of type safetyExcessive use of anyRuntime errors, refactoring difficulties
Improper operator selectionUse of operators that do not serve the purposeInefficiency, unexpected behavior
OvercomplicationComplicating a process that can be written simplyLow readability, difficult to maintain
One-liner hellMixed stream definitions, conversions, and subscriptionsDifficult to debug, difficult to test, low readability
State changes in subscribeChange of state directly in subscribeDifficult to test, cause bugs
Lack of testsNo tests written for RxJS codeRegression, refactoring difficulties

How to proceed with the study

  1. Learn 15 anti-patterns in detail in Common Mistakes and How to Fix Them
  2. Each anti-pattern has a "bad example" and a "good example" code
  3. Review your code with Anti-pattern Avoidance Checklist
  4. Practice best practices and share them with your team

After learning about anti-patterns, please also refer to the following sections.

Next Steps

  1. Start with Common Mistakes and How to Fix Them to learn practical anti-patterns and their solutions.
  2. After learning, review your actual code with the Anti-pattern Avoidance Checklist.

IMPORTANT: These anti-patterns are frequently found in real projects. Understanding them early on will help you write quality RxJS code.

Released under the CC-BY-4.0 license.