tomjaguarpaw / haskell-opaleye

Other
602 stars 115 forks source link

What's the best way to improve the readability of large select? #569

Closed stevemao closed 1 year ago

stevemao commented 1 year ago

How can I improve the following code?

orderBy (asc (^. _12)) $ aggregateOrdered (asc (^. _19)) (p31 (groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, arrayAgg, arrayAgg, arrayAgg, arrayAgg, arrayAgg, arrayAgg, arrayAgg, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy))
        $ do
                rowA <- selectA
                rowB <- selectB
                ...

                where_ $ ...

                return ( field1
                        , field2
                        ...
                        , field31 )

I need to select a lot of fields and do aggregations.

The readability becomes low _12 or _19 doesn't work on large size tuples out of the box In the future, p31 can grow to p100 and the library won't have it anymore.

What's your recommendation?

tomjaguarpaw commented 1 year ago

My first suggestion would be to use a record type rather than a tuple of 31 elements. Would that work for you?

stevemao commented 1 year ago

Can you show me an example? I'm not sure how to do that with aggregateOrdered and groupBy, etc

tomjaguarpaw commented 1 year ago

Sure, there's an example in the tutorial. Is that enough for you to go on? If not let me know and I'll write out a fuller example for you.

stevemao commented 1 year ago

Looks like it's working well!

The only annoying thing is

data Widget a b c d e ... = Widget ...

is still quite long and need to construct it manually. But I don't think it can be avoided.

Thank you so much for the help!