logoalt Hacker News

josephgyesterday at 10:12 PM0 repliesview on HN

I think it’s very easy to over apply that rule, and break a large function into a lot of small functions that you need to scroll up and down to reason about.

Here’s a 150 line long function I wrote which I think is quite beautiful:

https://github.com/josephg/diamond-types/blob/e143890a596aaf...

This function traverses a DAG given 2 points in the dag, A and B. It breaks the dag into 4 regions - the nodes which are (transitively) only in the parent subgraph of A, B, in both or - implicitly - in neither. It runs in O(n log n) time.

How would you improve this function? It could use a better doc comment. But do you honestly think it would be better if it were broken into a lot of small functions, each called once? How would you do it?