michaelahlers / scala-guide

Helpful recommendations and practical solutions for the working professional.
MIT License
2 stars 0 forks source link

Explore Judicious Use of Tuples and Preference for Case Classes #3

Open michaelahlers opened 1 year ago

michaelahlers commented 1 year ago

Document and propose alternatives to confusing uses of tuples in control flow with pattern matching.

Quick example:

(i, j, k) match {
  case (true, 1, "value") => // Do one thing.
  case (_, 2, "value") => // Do another.
}

This can start simple but doesn't scale:

(i, j, k, x, y, z) match {
  case (true, 1, "value", _, _, _) =>
    // Do one thing.
  case (_, 2, "value", _, _, _) =>
    // Do another.
  case (true, _, _, true, 3, "value") => 
    // Original arguments are relevant, but not all.
  case _ =>
    // Give up; I can't possibly understand all cases, but "this should never happen" gets utter.
    ???
}

This is another clear case where ADTs help in reducing the state space.