Conditions | 10 |
Total Lines | 77 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 10 |
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 build.managers.deployer.MaintenanceDeployer._get_affected_ids() 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 | """Module for handling the deployment of maintenance windows.""" |
||
68 | 1 | def _get_affected_ids( |
|
69 | self, |
||
70 | window: MaintenanceWindow |
||
71 | ) -> dict[str, list[str]]: |
||
72 | 1 | explicit_switches = filter( |
|
73 | lambda switch: switch is not None, |
||
74 | map( |
||
75 | self.controller.switches.get, |
||
76 | window.switches |
||
77 | ) |
||
78 | ) |
||
79 | |||
80 | 1 | tot_switches = list(explicit_switches) |
|
81 | |||
82 | 1 | implicit_interfaces = chain.from_iterable( |
|
83 | map( |
||
84 | lambda switch: switch.interfaces.values(), |
||
85 | tot_switches |
||
86 | ) |
||
87 | ) |
||
88 | |||
89 | 1 | explicit_interfaces = filter( |
|
90 | lambda interface: interface is not None, |
||
91 | map( |
||
92 | self.controller.get_interface_by_id, |
||
93 | window.interfaces |
||
94 | ) |
||
95 | ) |
||
96 | |||
97 | 1 | tot_interfaces = list(chain(implicit_interfaces, explicit_interfaces)) |
|
98 | |||
99 | 1 | implicit_links = filter( |
|
100 | lambda link: link is not None, |
||
101 | map( |
||
102 | lambda interface: interface.link, |
||
103 | tot_interfaces |
||
104 | ) |
||
105 | ) |
||
106 | |||
107 | 1 | explicit_links = filter( |
|
108 | lambda link: link is not None, |
||
109 | map( |
||
110 | self.controller.napps[('kytos', 'topology')].links.get, |
||
111 | window.links |
||
112 | ) |
||
113 | ) |
||
114 | |||
115 | 1 | tot_links = list(chain(implicit_links, explicit_links)) |
|
116 | |||
117 | 1 | affected_switch_ids = list(set(map( |
|
118 | lambda switch: switch.id, |
||
119 | filter( |
||
120 | self.switch_not_in_maintenance, |
||
121 | tot_switches |
||
122 | ) |
||
123 | ))) |
||
124 | |||
125 | 1 | affected_interface_ids = list(set(map( |
|
126 | lambda interface: interface.id, |
||
127 | filter( |
||
128 | self.interface_not_in_maintenance, |
||
129 | tot_interfaces |
||
130 | ) |
||
131 | ))) |
||
132 | |||
133 | 1 | affected_link_ids = list(set(map( |
|
134 | lambda link: link.id, |
||
135 | filter( |
||
136 | self.link_not_in_maintenance, |
||
137 | tot_links |
||
138 | ) |
||
139 | ))) |
||
140 | |||
141 | 1 | return { |
|
142 | 'switches': affected_switch_ids, |
||
143 | 'interfaces': affected_interface_ids, |
||
144 | 'links': affected_link_ids, |
||
145 | } |
||
231 | return frozenset({'maintenance'}) |