Conditions | 10 |
Total Lines | 54 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 10.2537 |
Changes | 1 | ||
Bugs | 0 | Features | 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 fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.listeners() 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 | /* |
||
40 | @Override |
||
41 | public Listener[] listeners() { |
||
42 | 1 | return new Listener[] { |
|
43 | 1 | new Listener<ObjectAdded>() { |
|
44 | @Override |
||
45 | public void on(ObjectAdded event) { |
||
46 | 1 | if (!(event.entry() instanceof BankEntry)) { |
|
47 | return; |
||
48 | } |
||
49 | |||
50 | 1 | final BankEntry entry = (BankEntry) event.entry(); |
|
51 | |||
52 | 1 | repository.add(entry.entity()); |
|
53 | 1 | } |
|
54 | |||
55 | @Override |
||
56 | public Class<ObjectAdded> event() { |
||
57 | 1 | return ObjectAdded.class; |
|
58 | } |
||
59 | }, |
||
60 | |||
61 | 1 | new Listener<ObjectDeleted>() { |
|
62 | @Override |
||
63 | public void on(ObjectDeleted event) { |
||
64 | 1 | if (!(event.entry() instanceof BankEntry)) { |
|
65 | return; |
||
66 | } |
||
67 | |||
68 | 1 | final BankEntry entry = (BankEntry) event.entry(); |
|
69 | |||
70 | 1 | repository.delete(entry.entity()); |
|
71 | 1 | } |
|
72 | |||
73 | @Override |
||
74 | public Class<ObjectDeleted> event() { |
||
75 | 1 | return ObjectDeleted.class; |
|
76 | } |
||
77 | }, |
||
78 | |||
79 | 1 | new Listener<ObjectQuantityChanged>() { |
|
80 | @Override |
||
81 | public void on(ObjectQuantityChanged event) { |
||
82 | 1 | if (!(event.entry() instanceof BankEntry)) { |
|
83 | return; |
||
84 | } |
||
85 | |||
86 | 1 | final BankEntry entry = (BankEntry) event.entry(); |
|
87 | |||
88 | 1 | repository.update(entry.entity()); |
|
89 | 1 | } |
|
90 | |||
91 | @Override |
||
92 | public Class<ObjectQuantityChanged> event() { |
||
93 | 1 | return ObjectQuantityChanged.class; |
|
94 | } |
||
99 |