3 ms·
'Whenever you do forget, it's immediately obvious by looking at the function in question. Or by grepping things that "open" something to see if there's correspo
by dllthomas 12y ago
'Whenever you do forget, it's immediately obvious by looking at the function in question. Or by grepping things that "open" something to see if there's corresponding defer nearby.'
That's not great. Static guarantees are worlds better than "I can manually look at it, and manually grep for places I need to look".
"But I didn't need to write a ChdirToSomewhereAndBack wrapper class either."
That's not exactly hard, and if you're doing something frequently then writing something to abstract it away is the right approach. It's true that many languages leave defining that wrapper class ugly and push it far away from the site of use in one-offs.
"All potential bugs are in plain sight, not hidden in some wrapper."
That would be one of the upsides, yes. The related downsides are that there is more room for bugs because you have to get it right every time instead of just once, and bugs might be hidden in plain sight when there is too much clutter.
"If you needed to do this chdir thing often, you could simply wrap it in a function:"
That seems isomorphic to the RAII wrapper class, with slightly more syntactic cruft.
- vardump 12y ago"That's not great. Static guarantees are worlds better than "I can manually look at it, and manually grep for places I need to look"." Grepping or looking is pretty much what you need to do also in C++, if you happen to have the bug in the wrapper. There's no static guarantee that can figure out if there's no chdir back to original location.
- dllthomas 12y agoIf you have a bug in the wrapper, you fix it once in the wrapper, and it's more likely to have shown up in a test if that wrapper is used multiple places. There is no static guarantee that you wrote the wrapper right, but hopefully there is some guarantee that you used the wrapper right. Definitions can't outnumber uses (or errors in some of those definitions don't matter...).
- deleted 12y ago[deleted]