Console or log output performed inside map, flatMap or filter

ID

scala.correctness.side_effects_in_monadic

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

low

Resource

Api Misuse

Language

Scala

Description

map, flatMap and filter are expected to be pure transformations. A side effect placed in one of them runs an unspecified number of times - lazy views, parallel collections and re-evaluated streams all change how often the function is applied. Move the effect into a foreach, or keep the transformation pure and log around it.

Rationale

map, flatMap and filter are expected to be pure transformations. A side effect placed in one of them runs an unspecified number of times - lazy views, parallel collections and re-evaluated streams all change how often the function is applied. Move the effect into a foreach, or keep the transformation pure and log around it.

The following code illustrates the pattern detected by this rule:

case class Order(id: String, total: Double)
class SideEffectsInMonadic {
  private val logger = LoggerFactory.getLogger(classOf[SideEffectsInMonadic])

  def ids(orders: List[Order]): List[String] =
    orders.map { o =>
      // FLAGGED: Console or log output performed inside map, flatMap or filter
      println(s"mapping ${o.id}")
      o.id
    }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved