Conditions | 11 |
Total Lines | 74 |
Code Lines | 41 |
Lines | 74 |
Ratio | 100 % |
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 gvm.protocols.gmpv208.entities.notes.NotesMixin.create_note() 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 | # -*- coding: utf-8 -*- |
||
31 | View Code Duplication | def create_note( |
|
|
|||
32 | self, |
||
33 | text: str, |
||
34 | nvt_oid: str, |
||
35 | *, |
||
36 | days_active: Optional[int] = None, |
||
37 | hosts: Optional[List[str]] = None, |
||
38 | port: Optional[int] = None, |
||
39 | result_id: Optional[str] = None, |
||
40 | severity: Optional[Severity] = None, |
||
41 | task_id: Optional[str] = None, |
||
42 | threat: Optional[SeverityLevel] = None, |
||
43 | ) -> Any: |
||
44 | """Create a new note |
||
45 | |||
46 | Arguments: |
||
47 | text: Text of the new note |
||
48 | nvt_id: OID of the nvt to which note applies |
||
49 | days_active: Days note will be active. -1 on |
||
50 | always, 0 off |
||
51 | hosts: A list of hosts addresses |
||
52 | port: Port to which the note applies |
||
53 | result_id: UUID of a result to which note applies |
||
54 | severity: Severity to which note applies |
||
55 | task_id: UUID of task to which note applies |
||
56 | threat: Severity level to which note applies. Will be converted to |
||
57 | severity. |
||
58 | |||
59 | Returns: |
||
60 | The response. See :py:meth:`send_command` for details. |
||
61 | """ |
||
62 | if not text: |
||
63 | raise RequiredArgument( |
||
64 | function=self.create_note.__name__, argument='text' |
||
65 | ) |
||
66 | |||
67 | if not nvt_oid: |
||
68 | raise RequiredArgument( |
||
69 | function=self.create_note.__name__, argument='nvt_oid' |
||
70 | ) |
||
71 | |||
72 | cmd = XmlCommand("create_note") |
||
73 | cmd.add_element("text", text) |
||
74 | cmd.add_element("nvt", attrs={"oid": nvt_oid}) |
||
75 | |||
76 | if days_active is not None: |
||
77 | cmd.add_element("active", str(days_active)) |
||
78 | |||
79 | if hosts: |
||
80 | cmd.add_element("hosts", to_comma_list(hosts)) |
||
81 | |||
82 | if port: |
||
83 | cmd.add_element("port", str(port)) |
||
84 | |||
85 | if result_id: |
||
86 | cmd.add_element("result", attrs={"id": result_id}) |
||
87 | |||
88 | if severity: |
||
89 | cmd.add_element("severity", str(severity)) |
||
90 | |||
91 | if task_id: |
||
92 | cmd.add_element("task", attrs={"id": task_id}) |
||
93 | |||
94 | if threat is not None: |
||
95 | if not isinstance(threat, SeverityLevel): |
||
96 | raise InvalidArgumentType( |
||
97 | function="create_note", |
||
98 | argument="threat", |
||
99 | arg_type=SeverityLevel.__name__, |
||
100 | ) |
||
101 | |||
102 | cmd.add_element("threat", threat.value) |
||
103 | |||
104 | return self._send_xml_command(cmd) |
||
105 | |||
270 |