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