Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>12 parameters definitely sound too many to me. Options to reduce their numbers are:</p> <ol> <li><p><a href="http://sourcemaking.com/refactoring/introduce-parameter-object" rel="noreferrer">Introduce Parameter Object</a> by grouping logically related parameters into an object and passing that object instead of the individual parameters.</p></li> <li><p>Introduce a <a href="http://en.wikipedia.org/wiki/Builder_pattern" rel="noreferrer">Builder</a> (optionally with <a href="http://en.wikipedia.org/wiki/Method_chaining" rel="noreferrer">method chaining</a>). This does not reduce the actual parameter list but it makes the code more readable, and is especially useful if you have several different creation scenarios with varying parameters. So instead of </p> <pre><code>MyClass someObject = new MyClass(aFoo, aBar, aBlah, aBaz, aBorp, aFlirp, andAGoo); MyClass anotherObject = new MyClass(aFoo, null, null, aBaz, null, null, andAGoo); </code></pre> <p>you can have</p> <pre><code>MyClass someObject = new MyClassBuilder().withFoo(aFoo).withBar(aBar) .withBlah(aBlah).withBaz(aBaz).withBorp(aBorp).withFlirp(aFlirp) .withGoo(aGoo).build(); MyClass anotherObject = new MyClassBuilder().withFoo(aFoo).withBaz(aBaz) .withGoo(aGoo).build(); </code></pre></li> <li><p>(Maybe I should have started with this ;-) Analyse the parameters - is all of them really needed in the constructor (i.e. mandatory)? If a parameter is optional, you may set it via its regular setter instead of the constructor.</p></li> </ol>
 

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