Conditions | 9 |
Total Lines | 72 |
Code Lines | 41 |
Lines | 72 |
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:
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 -*- |
||
84 | View Code Duplication | def create_scanner( |
|
|
|||
85 | self, |
||
86 | name: str, |
||
87 | host: str, |
||
88 | port: int, |
||
89 | scanner_type: ScannerType, |
||
90 | credential_id: str, |
||
91 | *, |
||
92 | ca_pub: Optional[str] = None, |
||
93 | comment: Optional[str] = None, |
||
94 | ) -> Any: |
||
95 | """Create a new scanner |
||
96 | |||
97 | Arguments: |
||
98 | name: Name of the scanner |
||
99 | host: The host of the scanner |
||
100 | port: The port of the scanner |
||
101 | scanner_type: Type of the scanner. |
||
102 | credential_id: UUID of client certificate credential for the |
||
103 | scanner |
||
104 | ca_pub: Certificate of CA to verify scanner certificate |
||
105 | comment: Comment for the scanner |
||
106 | Returns: |
||
107 | The response. See :py:meth:`send_command` for details. |
||
108 | """ |
||
109 | if not name: |
||
110 | raise RequiredArgument( |
||
111 | function=self.create_scanner.__name__, argument='name' |
||
112 | ) |
||
113 | |||
114 | if not host: |
||
115 | raise RequiredArgument( |
||
116 | function=self.create_scanner.__name__, argument='host' |
||
117 | ) |
||
118 | |||
119 | if not port: |
||
120 | raise RequiredArgument( |
||
121 | function=self.create_scanner.__name__, argument='port' |
||
122 | ) |
||
123 | |||
124 | if not scanner_type: |
||
125 | raise RequiredArgument( |
||
126 | function=self.create_scanner.__name__, argument='scanner_type' |
||
127 | ) |
||
128 | |||
129 | if not credential_id: |
||
130 | raise RequiredArgument( |
||
131 | function=self.create_scanner.__name__, argument='credential_id' |
||
132 | ) |
||
133 | |||
134 | if not isinstance(scanner_type, ScannerType): |
||
135 | raise InvalidArgumentType( |
||
136 | function=self.create_scanner.__name__, |
||
137 | argument='scanner_type', |
||
138 | arg_type=ScannerType.__name__, |
||
139 | ) |
||
140 | |||
141 | cmd = XmlCommand("create_scanner") |
||
142 | cmd.add_element("name", name) |
||
143 | cmd.add_element("host", host) |
||
144 | cmd.add_element("port", str(port)) |
||
145 | cmd.add_element("type", scanner_type.value) |
||
146 | |||
147 | if ca_pub: |
||
148 | cmd.add_element("ca_pub", ca_pub) |
||
149 | |||
150 | cmd.add_element("credential", attrs={"id": str(credential_id)}) |
||
151 | |||
152 | if comment: |
||
153 | cmd.add_element("comment", comment) |
||
154 | |||
155 | return self._send_xml_command(cmd) |
||
156 | |||
223 |