Conditions | 11 |
Total Lines | 53 |
Code Lines | 25 |
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 pandoc_latex_margin._main.margin() 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 | #!/usr/bin/env python |
||
54 | def margin(elem: Element, doc: Doc) -> list[Element] | None: |
||
55 | """ |
||
56 | Add margin to element if needed. |
||
57 | |||
58 | Arguments |
||
59 | --------- |
||
60 | elem |
||
61 | A pandoc element |
||
62 | doc |
||
63 | The pandoc document |
||
64 | |||
65 | Returns |
||
66 | ------- |
||
67 | list[Element] | None |
||
68 | A list of pandoc elements or None. |
||
69 | """ |
||
70 | # Is it in the right format and is it a Div or CodeBlock? |
||
71 | if doc.format in ("latex", "beamer") and elem.tag in ("Div", "CodeBlock"): |
||
72 | left = None |
||
73 | right = None |
||
74 | |||
75 | # Get the classes |
||
76 | classes = set(elem.classes) |
||
77 | |||
78 | # Loop on all fontsize definition |
||
79 | for definition in doc.defined: |
||
80 | # Are the classes correct? |
||
81 | if classes >= definition["classes"]: |
||
82 | left = definition["left"] |
||
83 | right = definition["right"] |
||
84 | break |
||
85 | |||
86 | # Is there a latex-margin-left attribute? |
||
87 | if "latex-left-margin" in elem.attributes: |
||
88 | left = get_correct_margin(elem.attributes["latex-left-margin"]) |
||
89 | |||
90 | # Is there a latex-margin-right attribute? |
||
91 | if "latex-right-margin" in elem.attributes: |
||
92 | right = get_correct_margin(elem.attributes["latex-right-margin"]) |
||
93 | |||
94 | if left is not None or right is not None: |
||
95 | if left is None: |
||
96 | left = "0pt" |
||
97 | if right is None: |
||
98 | right = "0pt" |
||
99 | return [ |
||
100 | RawBlock( |
||
101 | "\\begin{pandocchangemargin}{" + left + "}{" + right + "}", "tex" |
||
102 | ), # noqa: E501 |
||
103 | elem, |
||
104 | RawBlock("\\end{pandocchangemargin}", "tex"), |
||
105 | ] |
||
106 | return None |
||
107 | |||
220 |