Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring advice and tools
    primarykey
    data
    text
    <p>I have some code that consists of a lot (several hundreds of LOC) of uggly conditionals i.e.</p> <pre><code>SomeClass someClass = null; if("foo".equals(fooBar)) { // do something possibly involving more if-else statments // and possibly modify the someClass variable among others... } else if("bar".equals(fooBar)) { // Same as above but with some slight variations } else if("baz".equals(fooBar)) { // and yet again as above } //... lots of more else ifs } else { // and if nothing matches it is probably an error... // so there is some error handling here } // Some code that acts on someClass GenerateOutput(someClass); </code></pre> <p>Now I had the idea of refactoring this kind of code something along the lines of:</p> <pre><code>abstract class CheckPerform&lt;S,T,Q&gt; { private CheckPerform&lt;T&gt; next; CheckPerform(CheckPerform&lt;T&gt; next) { this.next = next; } protected abstract T perform(S arg); protected abstract boolean check(Q toCheck); public T checkPerform(S arg, Q toCheck) { if(check(toCheck)) { return perform(arg); } // Check if this CheckPerform is the last in the chain... return next == null ? null : next.checkPerform(); } } </code></pre> <p>And for each if statment generate a subclass of CheckPerform e.g.</p> <pre><code>class CheckPerformFoo extends CheckPerform&lt;SomeInput, SomeClass, String&gt; { CheckPerformFoo(CheckPerform&lt;SomeInput, SomeClass, String&gt; next) { super(next); } protected boolean check(String toCheck) { // same check as in the if-statment with "foo" above" returs "foo".equals(toCheck); } protected SomeClass perform(SomeInput arg) { // Perform same actions (as in the "foo" if-statment) // and return a SomeClass instance (that is in the // same state as in the "foo" if-statment) } } </code></pre> <p>I could then inject the diffrent CheckPerforms into eachother so that the same order of checks are made and the corresponding actions taken. And in the original class I would only need to inject one CheckPerform object. Is this a valid approach to this type of problem? The number of classes in my project is likely to explode, but atleast I will get more modular and testable code. Should I do this some other way?</p> <p>Since these if-else-if-...-else-if-else statments are what I would call a recurring theme of the code base I would like to do this refactoring as automagically as possible. So what tools could I use to automate this? </p> <p>a) Some customizable refactoring feature hidden somewhere in an IDE that I have missed (either in Eclipse or IDEA preferably) b) Some external tool that can parse Java code and give me fine grained control of transformations c) Should I hack it myself using Scala? d) Should I manually go over each class and do the refactoring using the features I am familiar with in my IDE?</p> <p>Ideally the output of the refactoring should also include some basic test code template that I can run (preferably also test cases for the original code that can be run on both new and old as a kind of regression test... but that I leave for later).</p> <p>Thanks for any input and suggestions!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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