Conditions | 14 |
Total Lines | 59 |
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:
Complex classes like Html.generate_attribute() 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 | """ |
||
37 | @staticmethod |
||
38 | def generate_attribute(name, value): |
||
39 | """ |
||
40 | Returns a string proper conversion of special characters to HTML entities of an attribute of a HTML tag. |
||
41 | |||
42 | :param str name: The name of the attribute. |
||
43 | :param str value: The value of the attribute. |
||
44 | |||
45 | :rtype: str |
||
46 | """ |
||
47 | html = '' |
||
48 | |||
49 | # Boolean attributes. |
||
50 | if name in ('autofocus', |
||
51 | 'checked', |
||
52 | 'disabled', |
||
53 | 'hidden', |
||
54 | 'ismap', |
||
55 | 'multiple', |
||
56 | 'novalidate', |
||
57 | 'readonly', |
||
58 | 'required', |
||
59 | 'selected', |
||
60 | 'spellcheck'): |
||
61 | if value: |
||
62 | html += ' ' |
||
63 | html += name |
||
64 | html += '="' |
||
65 | html += name |
||
66 | html += '"' |
||
67 | |||
68 | # Annoying boolean attribute exceptions. |
||
69 | elif name in ('draggable', 'contenteditable'): |
||
70 | if value is not None: |
||
71 | html += ' ' |
||
72 | html += name |
||
73 | html += '="true"' if value else '="false"' |
||
74 | |||
75 | elif name == 'autocomplete': |
||
76 | if value is not None: |
||
77 | html += ' ' |
||
78 | html += name |
||
79 | html += '="on"' if value else '="off"' |
||
80 | |||
81 | elif name == 'translate': |
||
82 | if value is not None: |
||
83 | html += ' ' |
||
84 | html += name |
||
85 | html += '="yes"' if value else '="no"' |
||
86 | |||
87 | else: |
||
88 | if value is not None and value != '': |
||
89 | html += ' ' |
||
90 | html += Html.escape(name) |
||
91 | html += '="' |
||
92 | html += Html.escape(value) |
||
93 | html += '"' |
||
94 | |||
95 | return html |
||
96 | |||
165 |