Note that there are some explanatory texts on larger screens.

plurals
  1. POJava pattern for parameters of which only one needs to be non-null?
    primarykey
    data
    text
    <p>In the last time I often write long functions that have several parameters but use only one of them and the functionality is only different at a few keypoints that are scattered around the function. Thus splitting the function would create too many small functions without a purpose. Is this good style or is there a good general refactoring pattern for this? To be more clear, an example:</p> <pre><code>public performSearch(DataBase dataBase, List&lt;List&lt;String&gt;&gt; segments) {performSearch(dataBase,null,null,segments);} public performSearch(DataBaseCache dataBaseCache,List&lt;List&lt;String&gt;&gt; segments) {performSearch(null,dataBaseCache,null,segments);} public performSearch(DataBase dataBase, List&lt;String&gt; keywords {performSearch(dataBase,null,keywords,null);} public performSearch(DataBaseCache dataBaseCache,List&lt;String&gt; keywords) {performSearch(null,dataBaseCache,keywords,null);} /** either dataBase or dataBaseCache may be null, dataBaseCache is used if it is non-null, else dataBase is used (slower). */ private void performSearch(DataBase dataBase, DataBaseCache dataBaseCache, List&lt;String&gt; keywords, List&lt;List&lt;String&gt;&gt; segments) { SearchObject search = new SearchObject(); search.setFast(true); ... search.setNumberOfResults(25); if(dataBaseCache!=null) {search.setSource(dataBaseCache);} else {search.setSource(dataBase);} ... do some stuff ... if(segments==null) { // create segments from keywords .... segments = ... } } </code></pre> <p>This style of code works but I don't like all those null parameters and the possibilities of calling methods like this wrong (both parameters null, what happens if both are non-null) but I don't want to write 4 seperate functions either... I know this may be too general but maybe someone has a general solution to this principle of problems :-)</p> <p>P.S.: I don't like to split up a long function if there is no reason for it other than it being long (i.e. if the subfunctions are only ever called in that order and only by this one function) especially if they are tightly interwoven and would need a big amount of parameters transported around them.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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