| Conditions | 15 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 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 parse_inner() 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 | # There's already a 'parser' in the Python standard library, so we're |
||
| 40 | def parse_inner(tokens, top_level, expecting_list): |
||
| 41 | parsed_expressions = List() |
||
| 42 | saw_closing_paren = False |
||
| 43 | |||
| 44 | # TODO: This could cause a stack overflow for deeply nested literals. |
||
| 45 | while tokens: |
||
| 46 | token = tokens.pop(0) |
||
| 47 | |||
| 48 | if isinstance(token, OpenParen): |
||
| 49 | parsed = parse_inner(tokens, top_level=False, expecting_list=True) |
||
| 50 | |||
| 51 | if isinstance(parsed, TrifleExceptionInstance): |
||
| 52 | return parsed |
||
| 53 | |||
| 54 | parsed_expressions.append(parsed) |
||
| 55 | elif isinstance(token, OpenCurlyParen): |
||
| 56 | parsed = parse_inner(tokens, top_level=False, expecting_list=False) |
||
| 57 | |||
| 58 | if isinstance(parsed, TrifleExceptionInstance): |
||
| 59 | return parsed |
||
| 60 | |||
| 61 | parsed_expressions.append(parsed) |
||
| 62 | elif isinstance(token, CloseParen): |
||
| 63 | if top_level: |
||
| 64 | return TrifleExceptionInstance( |
||
| 65 | parse_failed, |
||
| 66 | u'Closing ) has no matching opening (.') |
||
| 67 | elif not expecting_list: |
||
| 68 | return TrifleExceptionInstance( |
||
| 69 | parse_failed, |
||
| 70 | u'Closing ) does not match opening {.') |
||
| 71 | else: |
||
| 72 | saw_closing_paren = True |
||
| 73 | break |
||
| 74 | elif isinstance(token, CloseCurlyParen): |
||
| 75 | if top_level: |
||
| 76 | return TrifleExceptionInstance( |
||
| 77 | parse_failed, |
||
| 78 | u'Closing } has no matching opening {.') |
||
| 79 | elif expecting_list: |
||
| 80 | return TrifleExceptionInstance( |
||
| 81 | parse_failed, |
||
| 82 | u'Closing } does not match opening (.') |
||
| 83 | else: |
||
| 84 | saw_closing_paren = True |
||
| 85 | break |
||
| 86 | else: |
||
| 87 | parsed_expressions.append(token) |
||
| 88 | |||
| 89 | if not top_level and not saw_closing_paren: |
||
| 90 | return TrifleExceptionInstance( |
||
| 91 | parse_failed, u'Open paren was not closed.') |
||
| 92 | |||
| 93 | if expecting_list: |
||
| 94 | return parsed_expressions |
||
| 95 | else: |
||
| 96 | return list_to_hashmap(parsed_expressions) |
||
| 97 | |||
| 114 |