Conditions | 10 |
Total Lines | 86 |
Code Lines | 54 |
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.""" |
||
70 | 1 | def _get_affected_ids( |
|
71 | self, |
||
72 | window: MaintenanceWindow |
||
73 | ) -> dict[str, list[str]]: |
||
74 | 1 | explicit_switches = filter( |
|
75 | lambda switch: switch is not None, |
||
76 | map( |
||
77 | self.controller.switches.get, |
||
78 | window.switches |
||
79 | ) |
||
80 | ) |
||
81 | |||
82 | 1 | tot_switches = list(filter( |
|
83 | self.switch_not_in_maintenance, |
||
84 | explicit_switches |
||
85 | )) |
||
86 | |||
87 | 1 | implicit_interfaces = chain.from_iterable( |
|
88 | map( |
||
89 | lambda switch: switch.interfaces.values(), |
||
90 | tot_switches |
||
91 | ) |
||
92 | ) |
||
93 | |||
94 | 1 | explicit_interfaces = filter( |
|
95 | lambda interface: interface is not None, |
||
96 | map( |
||
97 | self.controller.get_interface_by_id, |
||
98 | window.interfaces |
||
99 | ) |
||
100 | ) |
||
101 | |||
102 | 1 | tot_interfaces = list( |
|
103 | filter( |
||
104 | self.interface_not_in_maintenance, |
||
105 | chain(implicit_interfaces, explicit_interfaces) |
||
106 | ) |
||
107 | ) |
||
108 | |||
109 | 1 | implicit_links = filter( |
|
110 | lambda link: link is not None, |
||
111 | map( |
||
112 | lambda interface: interface.link, |
||
113 | tot_interfaces |
||
114 | ) |
||
115 | ) |
||
116 | |||
117 | 1 | explicit_links = filter( |
|
118 | lambda link: link is not None, |
||
119 | map( |
||
120 | self.controller.links.get, |
||
121 | window.links |
||
122 | ) |
||
123 | ) |
||
124 | |||
125 | 1 | tot_links = list( |
|
126 | filter( |
||
127 | self.link_not_in_maintenance, |
||
128 | chain(implicit_links, explicit_links) |
||
129 | ) |
||
130 | ) |
||
131 | |||
132 | 1 | affected_switch_ids = frozenset( |
|
133 | map( |
||
134 | lambda switch: switch.id, |
||
135 | tot_switches |
||
136 | ) |
||
137 | ) |
||
138 | |||
139 | 1 | affected_interface_ids = frozenset( |
|
140 | map( |
||
141 | lambda interface: interface.id, |
||
142 | tot_interfaces |
||
143 | ) |
||
144 | ) |
||
145 | |||
146 | 1 | affected_link_ids = frozenset( |
|
147 | map( |
||
148 | lambda link: link.id, |
||
149 | tot_links |
||
150 | ) |
||
151 | ) |
||
152 | 1 | return { |
|
153 | 'switches': affected_switch_ids, |
||
154 | 'interfaces': affected_interface_ids, |
||
155 | 'links': affected_link_ids, |
||
156 | } |
||
243 | return frozenset({'maintenance'}) |