| Conditions | 17 | 
| Total Lines | 63 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 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 order_checklist_items() 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 | from skf.database import db | ||
| 41 | def order_checklist_items(checklist_items, lvl): | ||
| 42 | ordered_checklist_items = [] | ||
| 43 | for item in checklist_items.items: | ||
| 44 |         numbers = item.checklistID.split('.') | ||
| 45 | category = int(numbers[0]) | ||
| 46 | category_requirement = int(numbers[1]) | ||
| 47 | if (len(ordered_checklist_items) == 0): | ||
| 48 | ordered_checklist_items.append(item) | ||
| 49 | else: | ||
| 50 | y = 0 | ||
| 51 | while y < len(ordered_checklist_items): | ||
| 52 |                 numbers_ordered = ordered_checklist_items[y].checklistID.split('.') | ||
| 53 | category_ordered = int(numbers_ordered[0]) | ||
| 54 | category_requirement_ordered = int(numbers_ordered[1]) | ||
| 55 | if (category < category_ordered): | ||
| 56 | ordered_checklist_items.insert(y, item) | ||
| 57 | break | ||
| 58 | else: | ||
| 59 | if (category == category_ordered): | ||
| 60 | if (category_requirement < category_requirement_ordered): | ||
| 61 | ordered_checklist_items.insert(y, item) | ||
| 62 | break | ||
| 63 | y = y + 1 | ||
| 64 | if (y == len(ordered_checklist_items)): | ||
| 65 | ordered_checklist_items.insert(y, item) | ||
| 66 | |||
| 67 | if (not(lvl == 6)): | ||
| 68 | i = 0 | ||
| 69 | previousItemLevel = -1 | ||
| 70 | orderedWithEmpties = [] | ||
| 71 | for item in ordered_checklist_items: | ||
| 72 | if ((item.checklist_items.level == 0 and previousItemLevel == 0) or (item.checklist_items.content == "Resiliency Against Reverse Engineering Requirements" and not (lvl == 6))): | ||
| 73 | if (item.checklist_items.content == "Resiliency Against Reverse Engineering Requirements"): | ||
| 74 | orderedWithEmpties.append(item) | ||
| 75 | previousItemLevel = item.checklist_items.level | ||
| 76 |                     checklist_empty = checklists("0.0", "Requirements of Reverse Engineering can be added to form a level " + str(lvl-3) + "+R.", -1, 0) | ||
| 77 |                     checklists_kb_empty = checklists_kb("0.0", checklist_empty, 0, None) | ||
| 78 | orderedWithEmpties.append(checklists_kb_empty) | ||
| 79 | else: | ||
| 80 |                     checklist_empty = checklists("0.0", "No items for this category in this checklist level", -1, 0) | ||
| 81 |                     checklists_kb_empty = checklists_kb("0.0", checklist_empty, 0, None) | ||
| 82 | orderedWithEmpties.append(checklists_kb_empty) | ||
| 83 | orderedWithEmpties.append(item) | ||
| 84 | previousItemLevel = item.checklist_items.level | ||
| 85 | else: | ||
| 86 | orderedWithEmpties.append(item) | ||
| 87 | previousItemLevel = item.checklist_items.level | ||
| 88 | i = i + 1; | ||
| 89 | checklist_items.items = orderedWithEmpties | ||
| 90 | else: | ||
| 91 | orderedWithR6 = [] | ||
| 92 |         checklist_empty = checklists("0.0", "Using Requirements of Reverse Engineering you can form the levels L1+R or L2+R.", -1, 0) | ||
| 93 |         checklists_kb_empty = checklists_kb("0.0", checklist_empty, 0, None) | ||
| 94 | orderedWithR6.append(checklists_kb_empty) | ||
| 95 | for item in ordered_checklist_items: | ||
| 96 | if (item.checklist_items.level == 'R'): | ||
| 97 | checklist_modified = checklists(item.checklistID, item.checklist_items.content, 6, item.checklist_items.kbID) | ||
| 98 | modifiedItem = checklists_kb(item.checklistID, checklist_modified, item.kbID, item.kb_items) | ||
| 99 | orderedWithR6.append(modifiedItem) | ||
| 100 | else: | ||
| 101 | orderedWithR6.append(item) | ||
| 102 | checklist_items.items = orderedWithR6 | ||
| 103 | return checklist_items | ||
| 104 |