Closures and Higher-Order Functions to Reduce Code Reuse
I’m using the same solution to reduce code reuse that I did on the previous course project, since they’re both RESTful APIs. Since it’s all being done in a functional paradigm, I started using higher-order functions. The endpoint functions that are very copy-pasted can be generalized into higher-order functions that return closures.
Each higher-order function handles a different type of endpoint, accepting attribute names, column names, and class objects. It returns a closure that’s been filled in like Mad-Libs. This largely eliminates code reuse.
The downside is that the higher-order functions are not so much written as assembled, and are fairly unreadable. (I’d hate to have to maintain that code, which is never a good sign.)
13 January 2023