Sometimes in Scala I have code where a series of functions is applied to a variable a very limited number of times, say 3 or 4 times, like this:
def myTransformation(s: State): State = {
val s1 = f(s)
val s2 = g(s1)
h(s2)
}
What would be a good naming convention for the s1/s2 variables? Counting works. In e.g. haskell single quotes are appended. This can be mimicked with underscores, or by using backticks to allow for an actual single quote to be used. Another alternative is by appending letters (stateA, stateB). If it’s only a single intermediate variable, you can prepend new (for “newState” or “stateNew”). Preferably I’d like to use the “idiomatic” scala (3) way, but I haven’t been able to find it.
I’m asking because in my current codebase however this pattern occurs often but is not straightforward to generalize, as there might be bits of code inbetween the val s1/s2’s, and multiple values might be returned. In this specific example, if it occurred often in my code, I would probably generalize it with a helper that uses varargs to get nice and short syntax for this kind of construct. But that’s not always possible nor ideal if it doesn’t occur often.
I’m interested in advice for all versions of scala, but in particular scala 3.