4 ms·
It's okay if a function is only used once. It's worth it to separate it from the context it doesn't need. Well, twice, including the test. If a block is compl
by stirfish 1y ago
It's okay if a function is only used once. It's worth it to separate it from the context it doesn't need.
Well, twice, including the test. If a block is complex enough to warrant a high level explanation, you might as well capture that intent in a test too.
Edit: in my example, `applyCouponCode` takes in a `shoppingCart` and a `couponCode`, so you know that's all you need to apply a coupon code. You'd change it to something like `applyCouponCode(shoppingCart, couponCode, user.memberStatus)` so you can tell at a glance that `memberStatus` has something to do with how coupons work. You wouldn't want to pass in the whole `user`, because giving the function more than it needs makes it hard to infer what context the function needs and, therefore, what it does.
- Kinrany 11mo agoTests are code. If the function is easy to test then it's more likely to really be worth having.