Conditions | 11 |
Total Lines | 71 |
Lines | 60 |
Ratio | 84.51 % |
Tests | 28 |
CRAP Score | 12.7405 |
Changes | 2 | ||
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 dict_property() 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 | # -*- coding: utf-8 -*- |
||
62 | 1 | def dict_property(path, anytype): |
|
63 | """ |
||
64 | Creates new strict-typed PROPERTY for classes inherited from :class:`dict` |
||
65 | """ |
||
66 | |||
67 | 1 | View Code Duplication | def decorator_str(fn): |
|
|||
68 | |||
69 | 1 | def _get(self): |
|
70 | 1 | v = pydash.get(self.raw, path) |
|
71 | |||
72 | 1 | if v is None: |
|
73 | 1 | return None |
|
74 | |||
75 | 1 | return six.text_type(v) |
|
76 | |||
77 | 1 | def _set(self, value): |
|
78 | v = six.text_type(value) |
||
79 | v = fn(self, v) |
||
80 | pydash.set_(self.raw, path, v) |
||
81 | |||
82 | return v |
||
83 | |||
84 | 1 | doc = fn.__doc__ |
|
85 | |||
86 | 1 | typename = type(six.text_type('')).__name__ |
|
87 | |||
88 | 1 | doc = _handle_auto_doc_for_property( |
|
89 | fn.__doc__, |
||
90 | typename |
||
91 | ) |
||
92 | |||
93 | 1 | p = property(_get, _set, None, doc) |
|
94 | |||
95 | 1 | return p |
|
96 | |||
97 | 1 | View Code Duplication | def decorator_other(fn): |
98 | |||
99 | 1 | def _get(self): |
|
100 | 1 | v = pydash.get(self.raw, path) |
|
101 | |||
102 | 1 | if v is None: |
|
103 | return None |
||
104 | |||
105 | 1 | return anytype(v) |
|
106 | |||
107 | 1 | def _set(self, value): |
|
108 | v = fn(self, value) |
||
109 | pydash.set_(self.raw, path, v) |
||
110 | |||
111 | return v |
||
112 | |||
113 | 1 | doc = fn.__doc__ |
|
114 | |||
115 | 1 | if doc is None: |
|
116 | doc = '<AUTO>' |
||
117 | |||
118 | 1 | typename = anytype.__name__ |
|
119 | |||
120 | 1 | doc = _handle_auto_doc_for_property( |
|
121 | fn.__doc__, |
||
122 | typename |
||
123 | ) |
||
124 | |||
125 | 1 | p = property(_get, _set, None, doc) |
|
126 | |||
127 | 1 | return p |
|
128 | |||
129 | 1 | if anytype == str: |
|
130 | 1 | return decorator_str |
|
131 | |||
132 | return decorator_other |
||
133 |