11 ms·
Comments are perfectly fine on their own. It's common that a block of code can be given a higher level explanation in a comment, but wouldn't be a good standalo
by Kinrany 1y ago
Comments are perfectly fine on their own. It's common that a block of code can be given a higher level explanation in a comment, but wouldn't be a good standalone function because it depends on the context too much and so the function has absolutely no chance of ever being used a second time.
- stirfish 1y agoIt'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 1y agoTests are code. If the function is easy to test then it's more likely to really be worth having.