4 ms·
I really disagree with just the toy example. If it's just in one place, fine, but when you need to change the example to also do something else like check the e
by a_t48 7y ago
I really disagree with just the toy example. If it's just in one place, fine, but when you need to change the example to also do something else like check the end of the string `return x.startswith("foo") && !x.endswith("bar");` this style will end up biting you. Yeah, your IDE has tools that can help, but you might not catch all examples, especially in other branches.
- sesteel 7y agoYes! I understand that it feels inconvenient to read, but what is REALLY inconvenient is hunting down all occurrences of a magic string comparison that is poorly defined.
- addicted 7y agoI disagree with the toy example even if it’s just 1 use (although to be honest, you could construct a different example where I would choose to abstract only if there were multiple uses). The big problem I have with the toy example is that both pieces of code convey different ideas to a human reader, and limiting myself to the situations the author finds the indirection cumbersome, such as code reviews, or debugging, both would lead the reader in different directions. The suggested solution would make a reader think the business requirement of that code is to check if the input starts with foo, whereas the indirection code suggests to the reader that the input should be like foo (which in this case happens to be by testing it starts with foo, but that may be incorrect, or may change). I think the author’s example really hurts their case. A better example may be where the intention indeed is to check whether the input starts with the string foo, but you want to trim the sample first. I’ve seen code that would abstract that out with a function trimmedStartsWith, or something, instead of just input.trim().startsWith(foo), where I think the former only makes sense if you’re doing it a bunch of time.