| Conditions | 13 |
| Total Lines | 76 |
| 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 entry_point() 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 | # Copyright 2014 Wilfred Hughes. See docs/Licensing.md. |
||
| 75 | def entry_point(argv): |
||
| 76 | """Either a file name: |
||
| 77 | $ ./trifle ~/files/foo.tfl |
||
| 78 | |||
| 79 | A code snippet: |
||
| 80 | $ ./trifle -i '1 2' |
||
| 81 | |||
| 82 | """ |
||
| 83 | if len(argv) == 2: |
||
| 84 | # open the file |
||
| 85 | filename = argv[1] |
||
| 86 | |||
| 87 | if not os.path.exists(filename): |
||
| 88 | print 'No such file: %s' % filename |
||
| 89 | return 2 |
||
| 90 | |||
| 91 | try: |
||
| 92 | env = env_with_prelude() |
||
| 93 | except OSError: |
||
| 94 | return 2 |
||
| 95 | code = get_contents(filename) |
||
| 96 | lexed_tokens = lex(code) |
||
| 97 | |||
| 98 | if is_thrown_exception(lexed_tokens, error): |
||
| 99 | print u'Uncaught error: %s: %s' % ( |
||
| 100 | lexed_tokens.exception_type.name, |
||
| 101 | lexed_tokens.message) |
||
| 102 | return 1 |
||
| 103 | |||
| 104 | parse_tree = parse(lexed_tokens) |
||
| 105 | try: |
||
| 106 | result = evaluate_all(parse_tree, env) |
||
| 107 | |||
| 108 | if is_thrown_exception(result, error): |
||
| 109 | # TODO: a proper stack trace. |
||
| 110 | print u'Uncaught error: %s: %s' % (result.exception_type.name, |
||
| 111 | result.message) |
||
| 112 | return 1 |
||
| 113 | except SystemExit: |
||
| 114 | return 0 |
||
| 115 | return 0 |
||
| 116 | |||
| 117 | elif len(argv) == 3: |
||
| 118 | if argv[1] == '-i': |
||
| 119 | try: |
||
| 120 | env = env_with_prelude() |
||
| 121 | except OSError: |
||
| 122 | return 2 |
||
| 123 | code_snippet = argv[2].decode('utf-8') |
||
| 124 | lexed_tokens = lex(code_snippet) |
||
| 125 | |||
| 126 | if is_thrown_exception(lexed_tokens, error): |
||
| 127 | print u'Uncaught error: %s: %s' % ( |
||
| 128 | lexed_tokens.exception_type.name, |
||
| 129 | lexed_tokens.message) |
||
| 130 | return 1 |
||
| 131 | |||
| 132 | parse_tree = parse(lexed_tokens) |
||
| 133 | |||
| 134 | try: |
||
| 135 | result = evaluate_all(parse_tree, env) |
||
| 136 | |||
| 137 | if is_thrown_exception(result, error): |
||
| 138 | # TODO: a proper stack trace. |
||
| 139 | print u'Uncaught error: %s: %s' % (result.exception_type.name, |
||
| 140 | result.message) |
||
| 141 | return 1 |
||
| 142 | else: |
||
| 143 | print result.repr().encode('utf-8') |
||
| 144 | |||
| 145 | except SystemExit: |
||
| 146 | return 0 |
||
| 147 | return 0 |
||
| 148 | |||
| 149 | print USAGE |
||
| 150 | return 1 |
||
| 151 | |||
| 159 |