Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with refactoring some legacy code
    text
    copied!<p>So I was trying to refactor this <code>SomeClass</code> that has lots of different responsibilities. The shown above method seemed like a good place to start trimming down this class, so I thought about putting it in a IO specific class (and to easily allowing mocking, when needed).</p> <pre><code>class SomeClass{ ... public void m() { ... emptyDirectory(something); ... } private void emptyDirectory(File dir) { File[] dirContent = dir.listFiles(); if (dirContent != null) for (File f : dirContent) { if (f.isDirectory()) emptyDirectory(f); try { if (!f.delete()) { IOError problem = new IOError(symbolTable.getRefinement().getFileName(), f.toString(), f.isDirectory()); problemManager.add(problem); } } catch (SecurityException e) { IOError problem = new IOError(symbolTable.getRefinement().getFileName(), f.toString(), f.isDirectory()); problemManager.add(problem); } } } } } </code></pre> <p>The problem is that our system has a error logging mechanism that works just like a compiler's one (it will report the error but everything should continue its work. when you try to compile a program, it won't stop the compilation process on the first time it encounters an error).</p> <p>I'd like to have my IO class agnostic about this error reporting thing, so my idea was to throw exception from the IO method and then have <code>m()</code> catching it and doing the rest of the error handling. The problem is that both the <code>IOException</code> and <code>SecurityException</code> won't tell me what the name of the file is.</p> <p>I know I could create an exception of my own, but if I start creating exceptions of my own for such simple things, I'll have to create hundreds of exceptions for the rest of my code, also! </p> <p>I'd like to keep the refactor as simple as possible.</p> <p>How would you deal with refactoring?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload