SDA SE Wiki

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

User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
research:cultivate:splitresolveanduse [2009/03/13 19:28]
127.0.0.1 external edit
research:cultivate:splitresolveanduse [2018/05/09 01:59] (current)
Line 1: Line 1:
  
 +==== resolve- and use-concerns mixed in a loop: ====
 +
 +=== abstract ===
 +
 +
 +before:<​Code lang-java>​
 +
 +
 +
 +                for (Iterator iter = ids.iterator();​ iter.hasNext();​) {
 +
 +                    String id = (String) iter.next();​
 +
 +
 +
 +                    RealObject o = resolve(id);​
 +
 +
 +
 +                    o.doSomething();​
 +
 +                }
 +
 +</​Code>​
 +after:<​Code lang-java>​
 +
 +
 +
 +                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();​
 +
 +                }
 +
 +</​Code>​
 +
 +
 +=== example ===
 +
 +before:
 +
 +<Code lang-java>​
 +
 +
 +
 +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);​
 +
 +}
 +
 +}
 +</​Code>​
 +
 +<Code lang-java>​
 +
 +
 +
 +                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);​
 +
 +                }
 +
 +</​Code>​
 +
 +after:
 +
 +<Code lang-java>​
 +
 +
 +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);​
 +
 +}
 +
 +
 +
 +}
 +</​Code>​
 +<Code lang-java>​
 +
 +
 +
 +                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);​
 +
 +                }
 +
 +            ​
 +
 +</​Code>​
 +
 +
 +
 +=== 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