Conditions | 9 |
Total Lines | 78 |
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:
1 | #!/usr/bin/env python |
||
77 | def Minimze(self, line): |
||
78 | # if defined -> ifdef |
||
79 | line = re.sub(r'\s*#\s*if\s*defined\((\S*)\)\s*\Z', r'#ifdef \1', line) |
||
80 | # if !defined -> ifndef |
||
81 | line = re.sub(r'\s*#\s*if\s*!defined\((\S*)\)\s*\Z', r'#ifndef \1', line) |
||
82 | |||
83 | # C comment |
||
84 | if self.c_comment: |
||
85 | m = self.C_COMMENT_END_REGEX.match(line) |
||
86 | if m: |
||
87 | self.c_comment = False |
||
88 | line = m.group(1) |
||
89 | else: |
||
90 | return "" |
||
91 | m = self.C_COMMENT_BEGIN_REGEX.match(line) |
||
92 | if m: |
||
93 | self.c_comment = True |
||
94 | line = m.group(1) |
||
95 | line = line.strip() |
||
96 | if len(line) == 0: |
||
97 | return "" |
||
98 | |||
99 | # C++ comment |
||
100 | if self.COMMENT_REGEX.match(line): |
||
101 | return "" |
||
102 | # include guard |
||
103 | if self.IsUnnecessaryIncludeGuard(line): |
||
104 | return "" |
||
105 | |||
106 | # string store |
||
107 | str_l = [] |
||
108 | line = self.StoreStrings(line, str_l) |
||
109 | |||
110 | # remove comment and strip |
||
111 | line = re.sub('//[\S \t]*', '', line) |
||
112 | line = line.strip(' \t') |
||
113 | # remvoe \r |
||
114 | line = line.rstrip() |
||
115 | line += '\n' |
||
116 | # remove preprocessor directive unnecessary whitespace |
||
117 | line = re.sub('^\s*#\s*', '#', line) |
||
118 | line = re.sub('^\s*#(.+?)[ \t]+', r'#\1 ', line) |
||
119 | |||
120 | # remove unnecessary whitespace |
||
121 | line = re.sub('\s+(".*?")', r' \1', line) |
||
122 | line = re.sub(r'\)\s+>', r')>', line) |
||
123 | line = re.sub(';[ \t]+', ';', line) |
||
124 | line = re.sub('[ \t]+', ' ', line) |
||
125 | line = re.sub('([\w)\]]+)\s+([&|\+\-<>=\?]+)[ \t]+([^>])', r'\1\2\3', line) |
||
126 | line = re.sub('\s*([{\+\-\*/%=<>&|!]+=)[ \t]*', r'\1', line) |
||
127 | line = re.sub('<\s+(\w)', r'<\1', line) |
||
128 | line = re.sub('\s+:[ \t]+(\w)', r':\1', line) |
||
129 | line = re.sub('\s*,[ \t]*', ',', line) |
||
130 | line = re.sub('\s*\)', ')', line) |
||
131 | line = re.sub('\)\s+{', '){', line) |
||
132 | line = re.sub('\)\s+const', ')const', line) |
||
133 | if not re.match('#define\s+.*\s+{.*', line): |
||
134 | line = re.sub('\s*{\s*', '{', line) |
||
135 | line = re.sub('\s*}\s*', '}', line) |
||
136 | |||
137 | # define HOGE(x) vs define HOGE (x) |
||
138 | m = re.match('^#define\s+(\w+)\s+(\([^)]*?\))$', line) |
||
139 | line = re.sub('\s*\([ \t]*', '(', line) |
||
140 | if m: |
||
141 | line = re.sub('^#define\s+(\w+)(\([^)]*?\))$', r'#define \1 \2', line) |
||
142 | else: |
||
143 | line = re.sub('^#define\s+(\w+\([^)]*?\))(\S+)', r'#define \1 \2', line) |
||
144 | |||
145 | # 0x00000X => 0xX |
||
146 | line = re.sub('0x0+([0-9A-Fa-f])', r'0x\1', line) |
||
147 | # 0x0 => 0 |
||
148 | line = re.sub('0x([0-9])([^0-9A-Fa-f])', r'\1\2', line) |
||
149 | |||
150 | # string restore |
||
151 | line = self.RestoreStrings(line, str_l) |
||
152 | |||
153 | line = re.sub('^#define\s+(\w+)=', r'#define \1 =', line) |
||
154 | return line |
||
155 | |||
244 |