| Conditions | 9 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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:
| 1 | |||
| 19 | def plural(text): |
||
| 20 | """ |
||
| 21 | >>> plural('activity') |
||
| 22 | 'activities' |
||
| 23 | """ |
||
| 24 | aberrant = { |
||
| 25 | 'knife': 'knives', |
||
| 26 | 'self': 'selves', |
||
| 27 | 'elf': 'elves', |
||
| 28 | 'life': 'lives', |
||
| 29 | 'hoof': 'hooves', |
||
| 30 | 'leaf': 'leaves', |
||
| 31 | 'echo': 'echoes', |
||
| 32 | 'embargo': 'embargoes', |
||
| 33 | 'hero': 'heroes', |
||
| 34 | 'potato': 'potatoes', |
||
| 35 | 'tomato': 'tomatoes', |
||
| 36 | 'torpedo': 'torpedoes', |
||
| 37 | 'veto': 'vetoes', |
||
| 38 | 'child': 'children', |
||
| 39 | 'woman': 'women', |
||
| 40 | 'man': 'men', |
||
| 41 | 'person': 'people', |
||
| 42 | 'goose': 'geese', |
||
| 43 | 'mouse': 'mice', |
||
| 44 | 'barracks': 'barracks', |
||
| 45 | 'deer': 'deer', |
||
| 46 | 'nucleus': 'nuclei', |
||
| 47 | 'syllabus': 'syllabi', |
||
| 48 | 'focus': 'foci', |
||
| 49 | 'fungus': 'fungi', |
||
| 50 | 'cactus': 'cacti', |
||
| 51 | 'phenomenon': 'phenomena', |
||
| 52 | 'index': 'indices', |
||
| 53 | 'appendix': 'appendices', |
||
| 54 | 'criterion': 'criteria', |
||
| 55 | |||
| 56 | |||
| 57 | } |
||
| 58 | |||
| 59 | if text in aberrant: |
||
| 60 | result = '%s' % aberrant[text] |
||
| 61 | else: |
||
| 62 | postfix = 's' |
||
| 63 | if len(text) > 2: |
||
| 64 | vowels = 'aeiou' |
||
| 65 | if text[-2:] in ('ch', 'sh'): |
||
| 66 | postfix = 'es' |
||
| 67 | elif text[-1:] == 'y': |
||
| 68 | if (text[-2:-1] in vowels) or (text[0] in string.ascii_uppercase): |
||
| 69 | postfix = 's' |
||
| 70 | else: |
||
| 71 | postfix = 'ies' |
||
| 72 | text = text[:-1] |
||
| 73 | elif text[-2:] == 'is': |
||
| 74 | postfix = 'es' |
||
| 75 | text = text[:-2] |
||
| 76 | elif text[-1:] in ('s', 'z', 'x'): |
||
| 77 | postfix = 'es' |
||
| 78 | |||
| 79 | result = '%s%s' % (text, postfix) |
||
| 80 | return result |
||
| 81 | |||
| 213 |