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