Conditions | 17 |
Total Lines | 57 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
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 memegen.routes.image.get_with_text() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | from flask import Blueprint, current_app, request, redirect |
||
61 | @blueprint.route("/<key>/<path:path>.jpg", endpoint='get') |
||
62 | @flaskparser.use_kwargs(OPTIONS) |
||
63 | def get_with_text(key, path, alt, font, watermark, preview, share, **size): |
||
64 | assert len(size) == 2 |
||
65 | options = dict(key=key, path=path, |
||
66 | alt=alt, font=font, watermark=watermark, **size) |
||
67 | if preview: |
||
68 | options['preview'] = True |
||
69 | if share: |
||
70 | options['share'] = True |
||
71 | |||
72 | text = domain.Text(path) |
||
73 | fontfile = current_app.font_service.find(font) |
||
74 | |||
75 | template = current_app.template_service.find(key, allow_missing=True) |
||
76 | if template.key != key: |
||
77 | options['key'] = template.key |
||
78 | return redirect(route('.get', **options)) |
||
79 | |||
80 | if path != text.path: |
||
81 | options['path'] = text.path |
||
82 | return redirect(route('.get', **options)) |
||
83 | |||
84 | if alt and "://memegen.link/" in alt: |
||
85 | options.pop('alt') |
||
86 | options['key'] = alt.split('/')[3] |
||
87 | return redirect(route('.get', **options)) |
||
88 | |||
89 | if alt and "://" in alt and key != 'custom': |
||
90 | options['key'] = 'custom' |
||
91 | return redirect(route('.get', **options)) |
||
92 | |||
93 | if alt and template.path == template.get_path(alt, download=False): |
||
94 | options.pop('alt') |
||
95 | return redirect(route('.get', **options)) |
||
96 | |||
97 | if font and not fontfile: |
||
98 | options.pop('font') |
||
99 | return redirect(route('.get', **options)) |
||
100 | |||
101 | final_watermark, valid = _get_watermark(request, text, watermark, share) |
||
102 | if not valid: |
||
103 | options.pop('watermark') |
||
104 | return redirect(route('.get', **options)) |
||
105 | |||
106 | image = current_app.image_service.create( |
||
107 | template, text, |
||
108 | style=PLACEHOLDER if alt == 'none' else alt, |
||
109 | font=fontfile, size=size, watermark=final_watermark, |
||
110 | ) |
||
111 | |||
112 | if not preview: |
||
113 | cache_filtered.add(key=key, path=path, alt=alt, font=font) |
||
114 | cache_unfiltered.add(key=key, path=path, alt=alt, font=font) |
||
115 | track(image.text) |
||
116 | |||
117 | return display(image.text, image.path, share=share) |
||
118 | |||
195 |