Conditions | 10 |
Total Lines | 96 |
Code Lines | 39 |
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.framework.microkernel.resolver.Resolver.doRestore(FlawSolution) 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.framework.microkernel.resolver; |
||
241 | protected void doRestore(FlawSolution solution) |
||
242 | throws RelationPropagationException, DecisionPropagationException { |
||
243 | |||
244 | // list of restored decisions |
||
245 | Set<Decision> dRestored = new HashSet<>(); |
||
246 | // list of activated decisions |
||
247 | Set<Decision> dActivated = new HashSet<>(); |
||
248 | // list of restored relations |
||
249 | Set<Relation> rRestored = new HashSet<>(); |
||
250 | // list of activated relations |
||
251 | Set<Relation> rActivated = new HashSet<>(); |
||
252 | |||
253 | try { |
||
254 | |||
255 | // get the list of created decisions |
||
256 | for (Decision decision : solution.getCreatedDecisions()) { |
||
257 | // get decision component |
||
258 | DomainComponent dComp = decision.getComponent(); |
||
259 | // restore decision SILENT -> PENDING |
||
260 | dComp.restore(decision); |
||
261 | // add |
||
262 | dRestored.add(decision); |
||
263 | } |
||
264 | |||
265 | |||
266 | // check activated decisions |
||
267 | for (Decision decision : solution.getActivatedDecisions()) { |
||
268 | // get decision component |
||
269 | DomainComponent dComp = decision.getComponent(); |
||
270 | // activate decision PENDING -> ACTIVE |
||
271 | dComp.activate(decision); |
||
272 | // add |
||
273 | dActivated.add(decision); |
||
274 | } |
||
275 | |||
276 | // get the list of created relations |
||
277 | for (Relation relation : solution.getCreatedRelations()) { |
||
278 | // get reference component |
||
279 | DomainComponent refComp = relation.getReference().getComponent(); |
||
280 | // restore relation |
||
281 | refComp.restore(relation); |
||
282 | // add |
||
283 | rRestored.add(relation); |
||
284 | } |
||
285 | |||
286 | // check activated relations |
||
287 | for (Relation relation : solution.getActivatedRelations()) { |
||
288 | // get reference component |
||
289 | DomainComponent refComp = relation.getReference().getComponent(); |
||
290 | // activate relation |
||
291 | refComp.activate(relation); |
||
292 | // add relation to committed list |
||
293 | rActivated.add(relation); |
||
294 | } |
||
295 | |||
296 | // check consistency |
||
297 | this.tdb.verify(); |
||
298 | this.pdb.verify(); |
||
299 | |||
300 | } catch (DecisionPropagationException | ConsistencyCheckException ex) { |
||
301 | |||
302 | // deactivate activated relations |
||
303 | for (Relation rel : rActivated) { |
||
304 | |||
305 | // get reference component |
||
306 | DomainComponent refComp = rel.getReference().getComponent(); |
||
307 | // deactivate relation |
||
308 | refComp.deactivate(rel); |
||
309 | } |
||
310 | |||
311 | // remove restored relations |
||
312 | for (Relation rel : rRestored) { |
||
313 | // get reference component |
||
314 | DomainComponent refComp = rel.getReference().getComponent(); |
||
315 | // deactivate relation |
||
316 | refComp.delete(rel); |
||
317 | } |
||
318 | |||
319 | // deactivate decisions |
||
320 | for (Decision dec : dActivated) { |
||
321 | // get decision component |
||
322 | DomainComponent decComp = dec.getComponent(); |
||
323 | // deactivate decision |
||
324 | decComp.deactivate(dec); |
||
325 | } |
||
326 | |||
327 | // remove restored decisions |
||
328 | for (Decision dec: dRestored) { |
||
329 | // get decision component |
||
330 | DomainComponent decComp = dec.getComponent(); |
||
331 | // free decision |
||
332 | decComp.free(dec); |
||
333 | } |
||
334 | |||
335 | // throw exception |
||
336 | throw new DecisionPropagationException(ex.getMessage()); |
||
337 | } |
||
354 |