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