SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


resolve- and use-concerns mixed in a loop:

abstract

before:




                for (Iterator iter = ids.iterator(); iter.hasNext();) {

                    String id = (String) iter.next();



                    RealObject o = resolve(id);



                    o.doSomething();

                }

after:




                List<RealObject> realObjects = new ArrayList<RealObject>();

                for (Iterator iter = ids.iterator(); iter.hasNext();) {

                    String id = (String) iter.next();



                    realObjects.add(resolve(id));

                }

                for (Iterator iter = realObjects.iterator(); iter.hasNext();) {

                    RealObject o = (RealObject) iter.next();



                    o.doSomething();

                }

example

before:




for(iteratoriter=metrics.iterator();iter.hasnext();){

stringname=(string)iter.next();



descriptiondescription=descriptionrepository

.getinstance().getdescription(name);

if(description==null){

thrownewillegalargumentexception("unknownmetric>"

+name);

}

monitor.subtask(description.getname());

queryexecutor.executequery(description);

monitor.worked(1);

}

}



                for (Iterator iter = metrics.iterator(); iter.hasNext();) {

                    String name = (String) iter.next();



                    Description description = DescriptionRepository

                            .getInstance().getDescription(name);

                    if (description == null) {

                        throw new IllegalArgumentException("Unknown metric: "

                                + name);

                    }

                    monitor.subTask(description.getName());

                    queryExecutor.executeQuery(description);

                    monitor.worked(1);

                }

after:



setdescriptions=newhashset();

for(iteratoriter=metrics.iterator();iter.hasnext();){

stringname=(string)iter.next();



descriptiondescription=descriptionrepository

.getinstance().getdescription(name);

if(description==null){

thrownewillegalargumentexception("unknownmetric>"

+name);

}



descriptions.add(description);

}



for(iteratoriter=descriptions.iterator();iter.hasnext();){

descriptiondescription=(description)iter.next();



monitor.subtask(description.getname());

queryexecutor.executequery(description);

monitor.worked(1);

}



}



                Set descriptions = new HashSet();

                for (Iterator iter = metrics.iterator(); iter.hasNext();) {

                    String name = (String) iter.next();



                    Description description = DescriptionRepository

                            .getInstance().getDescription(name);

                    if (description == null) {

                        throw new IllegalArgumentException("Unknown metric: "

                                + name);

                    }



                    descriptions.add(description);

                }

                

                for (Iterator iter = descriptions.iterator(); iter.hasNext();) {

                    Description description = (Description) iter.next();



                    monitor.subTask(description.getName());

                    queryExecutor.executeQuery(description);

                    monitor.worked(1);

                }

            

why?

before, two concerns (resolving the descriptions and evaluating them) were tangled in one loop. afterwards, they are seperated and can be extracted in two methods that can be used in different places. the whole design gets more flexible. the resolving can be done as first step in an evaluation chain.

is this refactoring documented somewhere else? i didn't find it in Refactoring (Fowler).

the scope were name is used in the first loop is the smell I use for this refactoring, I think. name is only used in the first part of the loop, and description in the second, which smells like a “look up and use-tangling”.

prior to

  • extract method
  • move method
research/cultivate/splitresolveanduse.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2019