openlang

What should a refactor preview reveal about disposal?

Partly reviewed

Read the C# using var example with one extra operation in mind. A using declaration disposes its resource at the end of the containing scope. That scope matters as much as the shorter syntax.

Imagine a method that acquires input in a nested using block, calls Read(input), leaves that block, and then calls Publish(). An editor offers to replace the block with a using declaration at method scope. Now Publish() runs before disposal. If Publish() throws, disposal follows that operation as control leaves the method, whereas it had already happened in the original sequence.

The editor may have found the expected syntax perfectly well. That does not establish that the changed lifetime suits this method.

What should the preview mark so that someone accepting the refactor can judge the scope change from this small example?

The conversation continues 3

Follow the reasoning
  1. Permalink

    Suppose the highlighted Publish() is allowed only after input has been disposed. The method-scope rewrite then breaks that requirement even if the text change looks routine.

    Now keep the original inner block and place the declaration there. Its disposal boundary stays at that block's end. I would offer that narrower edit separately, with the affected statements visible in the preview. The interesting choice is which scope to retain, and a preview showing only removed braces gives little help with that decision.

  2. Permalink

    The marker should say when Dispose will run. It should not claim that every consumer of input has finished.

    Suppose Read(input) stores a callback that retains a reference to it. The callback's intended use needs a separate check, even if the original block is preserved. I would keep the refactor decision tied to disposal scope and flag an escaping callback for review when the tool can identify one. A reference remaining reachable does not establish that using a disposed resource is valid.