|
|
|
|
|
by dataflow
530 days ago
|
|
No. I've mentioned this multiple times but I feel like you're still missing what I'm saying about maintainability. (You didn't even reply to it at all.) To be very clear, I was explaining why, even if you somehow have a guarantee here that absolutely nothing ever fails, this code: stack.push_back(k);
absl::Cleanup _ = [&] { assert(stack.back() == k); stack.pop_back(); }
foo();
bar();
baz();
return 3;
is still better than this code w.r.t. maintainability and robustness: stack.push_back(k);
foo();
bar();
baz();
assert(stack.back() == k);
stack.pop_back();
return 3;
The reason, as I explained above, is the following:>> The fact that it is next to the setup statement (i.e. locality of information) is extremely important from a code health and maintainability standpoint. Having setup & cleanup be a dozen lines away from each other makes it far too easy to miss or forget one of them when the other is modified. Putting them next to each other prevents them from going out of sync and diverging over time. Fallibility is absolutely irrelevant to this point. It's about not splitting the source of truth into two separate spots in the code. This technique kills multiple birds at once, and handling errors better in the aforementioned cases is merely one of its benefits, but you should be doing it regardless. Do you see what I mean? |
|
For instance, this is the the scenario I expect to be harder to manage with exceptions & cleanup:
Without infallibility, you need a separate cleanup scope for each call you make. With this, the change to the private variable is still next to the operation that changes it, you just don't need to manage another control flow at the same time.EDIT: sorry, had the len's in the wrong spot before