| Conditions | 17 |
| Total Lines | 52 |
| Code Lines | 46 |
| Lines | 52 |
| 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 textmessage.TextMessageItem.on_delete() 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 | import falcon |
||
| 142 | @staticmethod |
||
| 143 | def on_delete(req, resp, id_): |
||
| 144 | if not id_.isdigit() or int(id_) <= 0: |
||
| 145 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 146 | description='API.INVALID_TEXT_MESSAGE_ID') |
||
| 147 | |||
| 148 | cnx = None |
||
| 149 | cursor = None |
||
| 150 | try: |
||
| 151 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 152 | cursor = cnx.cursor() |
||
| 153 | except Exception as e: |
||
| 154 | if cursor: |
||
| 155 | cursor.close() |
||
| 156 | if cnx: |
||
| 157 | cnx.disconnect() |
||
| 158 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 159 | |||
| 160 | try: |
||
| 161 | cursor.execute(" SELECT id FROM tbl_text_messages_outbox WHERE id = %s ", (id_,)) |
||
| 162 | row = cursor.fetchone() |
||
| 163 | except Exception as e: |
||
| 164 | if cursor: |
||
| 165 | cursor.close() |
||
| 166 | if cnx: |
||
| 167 | cnx.disconnect() |
||
| 168 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 169 | |||
| 170 | if row is None: |
||
| 171 | if cursor: |
||
| 172 | cursor.close() |
||
| 173 | if cnx: |
||
| 174 | cnx.disconnect() |
||
| 175 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 176 | description='API.TEXT_MESSAGE_NOT_FOUND') |
||
| 177 | |||
| 178 | try: |
||
| 179 | cursor.execute(" DELETE FROM tbl_text_messages_outbox WHERE id = %s ", (id_,)) |
||
| 180 | cnx.commit() |
||
| 181 | except Exception as e: |
||
| 182 | if cursor: |
||
| 183 | cursor.close() |
||
| 184 | if cnx: |
||
| 185 | cnx.disconnect() |
||
| 186 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 187 | |||
| 188 | if cursor: |
||
| 189 | cursor.close() |
||
| 190 | if cnx: |
||
| 191 | cnx.disconnect() |
||
| 192 | |||
| 193 | resp.status = falcon.HTTP_204 |
||
| 194 | |||
| 195 |