Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To me the easiest approach would involve a <code>Map&lt;String, Action&gt;</code>, i.e. mapping various strings to specific actions to perform. This way the lookup would be simpler and more performant than the manual comparison in your <code>CheckPerform*</code> classes, getting rid of much duplicated code.</p> <p>The actions can be implemented similar to your design, as subclasses of a common interface, but it may be easier and more compact to use an enum with overridden method(s). You may see an example of this in <a href="https://stackoverflow.com/questions/2913495/refactoring-if-else-logic/2913518#2913518">an earlier answer of mine</a>.</p> <p>Unfortunately I don't know of any automatic refactoring which could help you much in this. Earlier when I did somewhat similar refactorings, I wrote unit tests and did the refactoring step-by-step, manually, using automated support at the level of Move Method et al. Of course since the unit tests were pretty similar to each other in their structure, I could reuse part of the code there.</p> <h3>Update</h3> <p>@Sebastien pointed out in his comment, that I missed the possible sub-ifs within the bigger if blocks. One can indeed use a hierarchy of maps to resolve this. However, if the hierarchy starts to be really complex with a lot of duplicated functionality, a further improvement might be to implement a <a href="http://en.wikipedia.org/wiki/Domain-specific_language" rel="nofollow noreferrer">DSL</a>, to move the whole mapping out of code into a config file or DB. In its simplest form it might look something like</p> <pre><code>foo -&gt; com.foo.bar.SomeClass.someMethod biz -&gt; com.foo.bar.SomeOtherClass.someOtherMethod baz -&gt; com.foo.bar.YetAnotherClass.someMethod bar -&gt; com.foo.bar.SomeOtherClass.someMethod biz -&gt; com.foo.bar.DifferentClass.aMethod baz -&gt; com.foo.bar.AndAnotherClass.anotherMethod </code></pre> <p>where the indented lines configure the sub-conditions for each bigger case.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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