Explicit or implicit binding of choice arguments: the .. Record Wildcards operator

When should we use the .. Record Wildcards operator to implicitly bind choice arguments, and we should we explicitly enumerate them? In other words, choose between:

  • Implicit:
    let splitAmount = 80.0
    exerciseCmd iouCid Iou_Split with ..
    
  • Explicit:
    let splitAmount = 80.0
    exerciseCmd iouCid Iou_Split with splitAmount
    

Note: Let’s suppose the arguments (splitAmount) are already bound for some reason (contract field, choice argument, code cleanness etc), and let’s not consider here the otherwise nice solution: exerciseCmd iouCid Iou_Split with splitAmount = 80.0.

Some arguments are as follows:

Implicit (using ..)

  • Convenient (you need to type less, think about arguments less)
  • Easier to refactor (less editing needed when arg list changes)
  • Shorter code: better signal/noise ratio
  • Clean code should limit scope where variables can come from - e.g. a long function is a code smell

Explicit

  • Explicit is cleaner than implicit according to some guidelines (see also Zen of Python, C++ Core Guidelines)
  • Easier to track in the IDE/editor where a variable is used
  • Easier to find out what the choice arguments are (no need to open the choice)
  • Passing many arguments could already be a code smell, so the code might already need refactoring (giving more structure)
  • Slightly safer: harder to unintentionally pass a variable

Note: I only found a bit of documentation describing .. on docs.daml.com.

2 Likes

Alas this is a “grass is greener” situation: the signal/noise ratio of the “Explicit” form can be lower, greatly so in some situations, making it harder to quickly understand what the code is doing and therefore to see whether there’s a problem, semantically speaking.

For that reason I don’t think there’s a good hard-and-fast rule about when to choose one form over the other. DAML developers should use their best judgment in each situation.

For my part, I am loath to bind local variables needlessly. So I would have simply written the form above as exerciseCmd iouCid Iou_Split with splitAmount = 80.0. You can put with record arguments on separate lines just like with let, so it seems no more clear to bind the choice arguments as local variables first.

2 Likes

Thanks Stephen, I’m adding the argument “signal/noise” ratio to the question.
I didn’t intend to bind vars needlessly, I supposed they had already been bound. I’m adding a note on that.