3 ms·
I've become a big fan of using something like a `main()` function to act as the one conventional place where pure functions are piped together at the top of a m
by mypetocean 3y ago
I've become a big fan of using something like a `main()` function to act as the one conventional place where pure functions are piped together at the top of a module.
You get this very nice linear execution birds' eye view which tends to be readable. Combine that with hoisting and you start with this big picture at the top of a file, and then can dig deeper into the smaller functions as needed, written in lexical order lower in the file.
Here is a very trivial JS example from a kata:
```js
function main (numbers) {
return Array
.from(numbers)
.sort(byGreatest)
.slice(0, 2)
.reduce(toSum)
}
```
This is much more regularly written in languages with a pipeline operator, like Elixir, because you can pipe to arbitrary functions and operators (instead of being restricted to a method chain).
(JS/TS will get there eventually, if the TC39 committee can ever finally commit to the proposal.)
- noduerme 3y agoI like the method chain and find it very readable (and a good guarantor of typed output in the absence of strict types, if you use something like Typescript to lint it). But I don't see how the choice between that or a pipe operator has any impact on whether function main() should be your program's entry point. Unless your program is going to be an endless loop, e.g. a Nodejs server or else a game, I don't know why you'd want to saddle yourself with a main() every time it runs.