3 ms·
When there are a few they can be really great. But if you need to accurately name every single intermediate thing they can become visual noise that hides what h
by scotty79 4y ago
When there are a few they can be really great. But if you need to accurately name every single intermediate thing they can become visual noise that hides what happens.
- shadowgovt 4y agoIn general, I think that when one does that, the code smell one is smelling isn't "This language isn't expressive enough; I need a third way to describe calling a function." It's "What I'm doing is actually complicated and I need to switch to describing it with a DSL, not adding more layers of frosting on this three-layer cake."
- notdonspaulding 4y agoI struggle to think of real-world examples where I've just needed to chain and chain and chain values of different types more than a handful of times. The claimed need for the pipe operator is this construction: function bakeCake() { return separateFromPan(coolOff(bake(pour(mix(gatherIngredients(), bowl), pan), 350, 45), 30)); } The piped code looks like: function bakeCake() { return gatherIngredients() |> mix(%, bowl) |> pour(%, pan) |> bake(%, 350, 45) |> coolOff(%) |> separateFromPan(%) ; Which is... fine? It certainly looks better than the mess we started with, but adding names here only helps clarify each step. function bakeCake() { const ingredients = gatherIngredients(); const batter = mix(ingredients); const batterInPan = pour(batter, pan); const bakedCake = bake(batterInPan, 350, 45); const cooledCake = coolOff(bakedInPan); return separateFromPan(cooledCake); } Even if you consider the `const` to be visual noise, the names are useful. At any point you can understand the goal of the code on the right-hand side by looking at the name of the variable on the left-hand side. You can also visually scan the right-hand side and see the processing steps. You can also introduce new steps to the control flow at any point and understand what the data should look like both before and after your new step. I agree that the the control flow is more clearly elucidated in the pipe operator example, but it tosses away useful information about the state that the named variables contain. It also introduces two new syntactical concepts for your brain to interpret (the pipe operator and the value placeholder). I contend the cognitive load is no greater in the example with names, and the maintainability is greatly improved. If you have an example where there are dozens of steps to the control flow with no break, I'd be really curious to see it.
- dgb23 4y agoAlso the second example is easier to manipulate. You can hack in branches, logging etc. during development. I'm also not sure how the proposal tries to solve the problem that we can't easily pluck out members from an object in the first example. Will people just write something like `get(obj, "member")`? Or maybe they thought about this?
- drowsspa 4y agoWhat this reminds me is of those hierarchies Cat extends Animal... In these simple "real-world-inspired" examples it seems to make sense, but in programming I'd say a lot of times there's simply no good name for the intermediate steps.
- postalrat 4y agoHow about function bakeCake() { return do( () => gatherIngredients(), ingredients => mix(ingredients), batter => pour(batter, pan), batterInPan => bake(batterInPan, 350, 45), () => coolOff(bakedInPan), cooledCake => separateFromPan(cooledCake) ); }
- ubertaco 4y ago...which is just function bakeCake() { const ingredients = gatherIngredients(); const batter = mix(ingredients); const batterInPan = pour(batter, pan); bake(batternInPan, 350, 45); // this is an in-place modifying function, I guess const cooledCake = coolOff(bakedInPan); return separateFromPan(cooledCake); } ...but with an extra `do(...)` wrapper? It could at least be function bakeCake() { return do( gatherIngredients, mix, batter => pour(batter, pan), batterInPan => bake(batterInPan, 350, 45), coolOff, separateFromPan ); } Although if we had function currying, the convention in ML languages is to put the most-commonly-piped-in param last for these functions: function bakeCake() { return do( gatherIngredients, mix, pour(pan), // assuming that pour(pan) returns a function that pours something into that pan bake(350, 45), // assuming that bake(temp, minutes) returns a function that bakes something at that temperature for that time coolOff, separateFromPan ); }