Conditions | 22 |
Total Lines | 84 |
Code Lines | 65 |
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 ospd.vts.Vts.add() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # Copyright (C) 2014-2021 Greenbone Networks GmbH |
||
85 | def add( |
||
86 | self, |
||
87 | vt_id: str, |
||
88 | name: str = None, |
||
89 | vt_params: str = None, |
||
90 | vt_refs: str = None, |
||
91 | custom: str = None, |
||
92 | vt_creation_time: str = None, |
||
93 | vt_modification_time: str = None, |
||
94 | vt_dependencies: str = None, |
||
95 | summary: str = None, |
||
96 | impact: str = None, |
||
97 | affected: str = None, |
||
98 | insight: str = None, |
||
99 | solution: str = None, |
||
100 | solution_t: str = None, |
||
101 | solution_m: str = None, |
||
102 | detection: str = None, |
||
103 | qod_t: str = None, |
||
104 | qod_v: str = None, |
||
105 | severities: str = None, |
||
106 | ) -> None: |
||
107 | """Add a vulnerability test information. |
||
108 | |||
109 | IMPORTANT: The VT's Data Manager will store the vts collection. |
||
110 | If the collection is considerably big and it will be consultated |
||
111 | intensible during a routine, consider to do a deepcopy(), since |
||
112 | accessing the shared memory in the data manager is very expensive. |
||
113 | At the end of the routine, the temporal copy must be set to None |
||
114 | and deleted. |
||
115 | """ |
||
116 | if not vt_id: |
||
117 | raise OspdError('Invalid vt_id {}'.format(vt_id)) |
||
118 | |||
119 | if self.vt_id_pattern.fullmatch(vt_id) is None: |
||
120 | raise OspdError('Invalid vt_id {}'.format(vt_id)) |
||
121 | |||
122 | if vt_id in self.vts: |
||
123 | raise OspdError('vt_id {} already exists'.format(vt_id)) |
||
124 | |||
125 | if name is None: |
||
126 | name = '' |
||
127 | |||
128 | vt = {'name': name} |
||
129 | if custom is not None: |
||
130 | vt["custom"] = custom |
||
131 | if vt_params is not None: |
||
132 | vt["vt_params"] = vt_params |
||
133 | if vt_refs is not None: |
||
134 | vt["vt_refs"] = vt_refs |
||
135 | if vt_dependencies is not None: |
||
136 | vt["vt_dependencies"] = vt_dependencies |
||
137 | if vt_creation_time is not None: |
||
138 | vt["creation_time"] = vt_creation_time |
||
139 | if vt_modification_time is not None: |
||
140 | vt["modification_time"] = vt_modification_time |
||
141 | if summary is not None: |
||
142 | vt["summary"] = summary |
||
143 | if impact is not None: |
||
144 | vt["impact"] = impact |
||
145 | if affected is not None: |
||
146 | vt["affected"] = affected |
||
147 | if insight is not None: |
||
148 | vt["insight"] = insight |
||
149 | |||
150 | if solution is not None: |
||
151 | vt["solution"] = solution |
||
152 | if solution_t is not None: |
||
153 | vt["solution_type"] = solution_t |
||
154 | if solution_m is not None: |
||
155 | vt["solution_method"] = solution_m |
||
156 | |||
157 | if detection is not None: |
||
158 | vt["detection"] = detection |
||
159 | |||
160 | if qod_t is not None: |
||
161 | vt["qod_type"] = qod_t |
||
162 | elif qod_v is not None: |
||
163 | vt["qod"] = qod_v |
||
164 | |||
165 | if severities is not None: |
||
166 | vt["severities"] = severities |
||
167 | |||
168 | self.vts[vt_id] = vt |
||
169 | |||
214 |