logoalt Hacker News

Prefer do notation over Applicative operators when assembling records (2024)

48 pointsby wazHFsRylast Tuesday at 7:57 PM6 commentsview on HN

Comments

nanolithyesterday at 11:45 PM

There is plenty of room for debate over this advice. Prefer do notation if the effects of the flow are more important than the data structure, and prefer using Applicative style if the data structure layout is more important to understand than the effects to build it. In the example in the article, the software is easier to understand from the perspective of how data was acquired to populate the record. However, this isn't true in a general sense; often records are purpose built in higher level languages to reflect effects and constrain them.

Example: when using Parsec or similar, Applicative style is far easier to understand when examining how records are parsed. In this case, the records will likely reflect the AST, which in turn will reflect the grammar. The two reinforce each other: additional constraints on syntax will naturally flock to data constructors.

show 1 reply
throwaway17_17today at 2:32 AM

In the ‘Caveats’ section the author discusses that the advice only applies to types constructed via Record Syntax and that datatypes with positional arguments are exempt from the rule/preference proposed.

I wonder if the argument for the do notation’s usage being better along several metrics can stand generalization. There are sometimes discussions about the merits of named argument to functions and data constructors versus the much more common positional syntax, the arguments made in favor of more ‘broad spectrum’ named arguments reflect the more specific pro’s being argued in TFA.

It would be interesting to see what types of syntax additions, and the accompanying weight of those additions throughout an entire code base, would prove an acceptable trade off for moving away from positional arguments being the default. I almost assume the extra line noise and extra name selection would be the most negative effects, but the most positive effects are harder for me to guess at. But overall, the reaction of devs to more ‘book keeping’ in there code is hardly ever positive.

Garleftoday at 6:50 AM

From an outsiders perspective, this kind of discussion seems... immature?

(As in: Kids discussing something "important" to them while the parents chuckle)

I like the do notation better because it expresses the intent at the point of usage and I don't have to look up the definition when coming back after a few weeks.

kccqzytoday at 2:19 AM

I personally think of the distinction as whether the name is important. In Applicative style you apply the arguments to the constructor in order. In do notation you give names to the intermediate pieces. If the names are important enough to be stated (which is usually the case for records) use do notation. Otherwise don’t. This really reminds me of languages like Swift that have external names (aka argument label); whether a function parameter deserves an external name should be decided on a case-by-case basis and similarly here.

wazHFsRytoday at 5:50 AM

In the authors enter first and last name example, isn’t the do notation also much clearer on order of effects? Eg being asked first for first name?