4 ms·
> Why not assign each call to a variable for readability? The problem is that if you use a variable, you either: - use a new variable each time, therefore you
by arnvald 5y ago
> Why not assign each call to a variable for readability?
The problem is that if you use a variable, you either:
- use a new variable each time, therefore you have to think of appropriate name each time (and having a lot of variables does not necessarily make code more readable)
- or you have to come up with a rather generic name of variable, like "result" or "output", otherwise the name will not make sense until the very end where the last value is assigned
Let's say I want to have a list of active products from some category. With option 1 it would be:
const allProducts = Product.all()
const activeProducts = allProducts.filterActive()
const activeHouseProducts = Category.match(activeProducts, "house")
with option 2 you'd do:
var activeHouseProducts = Product.all()
activeHouseProducts = activeHouseProducts.filterActive()
activeHouseProducts= Category.match(activeHouseProducts, "house")
and with pipe operator you can do:
const activeHouseProducts =
Product.all().filterActive() |> Category.match(%, "house")
- magicalist 5y agoFWIW it seems like option 1 should actually be const activeProducts = Product.all().filterActive() const activeHouseProducts = Category.match(activeProducts, "house")