tomjaguarpaw / haskell-opaleye

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

stevemao opened this issue · comments

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?

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

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

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.

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!