Conditions | 14 |
Total Lines | 100 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.platinum.ai.deliberative.solver.Solver.contextSwitch(SearchSpaceNode,SearchSpaceNode) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package it.cnr.istc.pst.platinum.ai.deliberative.solver; |
||
162 | protected void contextSwitch(SearchSpaceNode last, SearchSpaceNode extracted) |
||
163 | throws PlanRefinementException { |
||
164 | |||
165 | // compare the two nodes |
||
166 | if (last != null) { |
||
167 | |||
168 | // prepare a list of operators to retract |
||
169 | List<Operator> toRetract = new ArrayList<>(); |
||
170 | // prepare a list of operators to propagate |
||
171 | List<Operator> toPropagate = new ArrayList<>(); |
||
172 | |||
173 | // get the list of operators of the nodes |
||
174 | List<Operator> lastNodeOperators = last.getOperators(); |
||
175 | List<Operator> extractedNodeOperators = extracted.getOperators(); |
||
176 | |||
177 | // check min length between the two lists |
||
178 | int minLength = Math.min(lastNodeOperators.size(), extractedNodeOperators.size()); |
||
179 | // check potentially common operators |
||
180 | boolean common = true; |
||
181 | for (int i = 0; i < minLength; i++) { |
||
182 | |||
183 | // check common flag |
||
184 | if (common && !lastNodeOperators.get(i).equals(extractedNodeOperators.get(i))) { |
||
185 | common = false; |
||
186 | } |
||
187 | |||
188 | // check if no common operators have been found |
||
189 | if (!common) { |
||
190 | |||
191 | // add operator to the different lists |
||
192 | toRetract.add(lastNodeOperators.get(i)); |
||
193 | toPropagate.add(extractedNodeOperators.get(i)); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // check other operators to retract |
||
198 | for (int i = minLength; i < lastNodeOperators.size(); i++) { |
||
199 | toRetract.add(lastNodeOperators.get(i)); |
||
200 | } |
||
201 | |||
202 | // check other operators to propagate |
||
203 | for (int i = minLength; i < extractedNodeOperators.size(); i++) { |
||
204 | toPropagate.add(extractedNodeOperators.get(i)); |
||
205 | } |
||
206 | |||
207 | |||
208 | // retract operators in reverse order |
||
209 | Collections.reverse(toRetract); |
||
210 | // retract all operators |
||
211 | for (Operator operator : toRetract) { |
||
212 | // retract operator |
||
213 | this.pdb.retract(operator); |
||
214 | } |
||
215 | |||
216 | // list of committed operators |
||
217 | List<Operator> committed = new ArrayList<>(); |
||
218 | |||
219 | try { |
||
220 | |||
221 | // propagate operators in chronological order |
||
222 | for (Operator operator : toPropagate) { |
||
223 | |||
224 | // propagate operator |
||
225 | this.pdb.propagate(operator); |
||
226 | // add committed operator |
||
227 | committed.add(operator); |
||
228 | } |
||
229 | |||
230 | } catch (OperatorPropagationException ex) { |
||
231 | |||
232 | // retract committed operators in reverse order |
||
233 | Collections.reverse(committed); |
||
234 | for (Operator operator : committed) { |
||
235 | // retract operator |
||
236 | this.pdb.retract(operator); |
||
237 | } |
||
238 | |||
239 | // also restore retracted operators |
||
240 | Collections.reverse(toRetract); |
||
241 | for (Operator operator : toRetract) { |
||
242 | |||
243 | try { |
||
244 | |||
245 | // restore operator |
||
246 | this.pdb.propagate(operator); |
||
247 | |||
248 | } catch (OperatorPropagationException exx) { |
||
249 | warning("[ContextSwitch] Error while restoring operators after failure:\n" |
||
250 | + "- message: " + ex.getMessage() + "\n"); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | // throw exception |
||
255 | throw new PlanRefinementException("Error while propagating node:\n" + extracted + "\n- message: " + ex.getMessage() + "\n"); |
||
256 | } |
||
257 | |||
258 | } else { |
||
259 | |||
260 | // simply propagate extracted node |
||
261 | this.propagate(extracted); |
||
262 | } |
||
326 |