Skip to content

Commit 0c79e3e

Browse files
authored
Simplify and clean up the code (#548)
- simplify the usage of switch statements - use "lang" and "langs" as aliases for "language" (these aliases are already used in existing code) - reuse sortLanguageSummary - use gettempdir to fix a warning about directly using the '/tmp' path
1 parent d40c77c commit 0c79e3e

File tree

4 files changed

+67
-119
lines changed

4 files changed

+67
-119
lines changed

SCC-OUTPUT-REPORT.html

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
<tbody><tr>
1414
<th>Go</th>
1515
<th>27</th>
16-
<th>9629</th>
17-
<th>1462</th>
18-
<th>449</th>
19-
<th>7718</th>
20-
<th>1513</th>
21-
<th>256852</th>
22-
<th>4141</th>
16+
<th>9577</th>
17+
<th>1461</th>
18+
<th>447</th>
19+
<th>7669</th>
20+
<th>1413</th>
21+
<th>254907</th>
22+
<th>4126</th>
2323
</tr><tr>
2424
<td>processor/workers_test.go</td>
2525
<td></td>
@@ -33,13 +33,13 @@
3333
</tr><tr>
3434
<td>processor/formatters.go</td>
3535
<td></td>
36-
<td>1561</td>
37-
<td>209</td>
38-
<td>41</td>
39-
<td>1311</td>
40-
<td>263</td>
41-
<td>46071</td>
42-
<td>792</td>
36+
<td>1517</td>
37+
<td>208</td>
38+
<td>39</td>
39+
<td>1270</td>
40+
<td>163</td>
41+
<td>44230</td>
42+
<td>784</td>
4343
</tr><tr>
4444
<td>processor/formatters_test.go</td>
4545
<td></td>
@@ -93,13 +93,13 @@
9393
</tr><tr>
9494
<td>cmd/badges/main.go</td>
9595
<td></td>
96-
<td>382</td>
96+
<td>374</td>
9797
<td>60</td>
9898
<td>17</td>
99-
<td>305</td>
99+
<td>297</td>
100100
<td>59</td>
101-
<td>9675</td>
102-
<td>243</td>
101+
<td>9571</td>
102+
<td>239</td>
103103
</tr><tr>
104104
<td>processor/workers_tokei_test.go</td>
105105
<td></td>
@@ -130,16 +130,6 @@
130130
<td>37</td>
131131
<td>4585</td>
132132
<td>97</td>
133-
</tr><tr>
134-
<td>processor/structs.go</td>
135-
<td></td>
136-
<td>199</td>
137-
<td>21</td>
138-
<td>18</td>
139-
<td>160</td>
140-
<td>17</td>
141-
<td>5883</td>
142-
<td>139</td>
143133
</tr><tr>
144134
<td>cmd/badges/main_test.go</td>
145135
<td></td>
@@ -150,6 +140,16 @@
150140
<td>10</td>
151141
<td>4088</td>
152142
<td>106</td>
143+
</tr><tr>
144+
<td>processor/structs.go</td>
145+
<td></td>
146+
<td>199</td>
147+
<td>21</td>
148+
<td>18</td>
149+
<td>160</td>
150+
<td>17</td>
151+
<td>5883</td>
152+
<td>139</td>
153153
</tr><tr>
154154
<td>processor/workers_regression_test.go</td>
155155
<td></td>
@@ -294,15 +294,15 @@
294294
<tfoot><tr>
295295
<th>Total</th>
296296
<th>27</th>
297-
<th>9629</th>
298-
<th>1462</th>
299-
<th>449</th>
300-
<th>7718</th>
301-
<th>1513</th>
302-
<th>256852</th>
303-
<th>4141</th>
297+
<th>9577</th>
298+
<th>1461</th>
299+
<th>447</th>
300+
<th>7669</th>
301+
<th>1413</th>
302+
<th>254907</th>
303+
<th>4126</th>
304304
</tr>
305305
<tr>
306-
<th colspan="9">Estimated Cost to Develop (organic) $230,927<br>Estimated Schedule Effort (organic) 7.88 months<br>Estimated People Required (organic) 2.60<br></th>
306+
<th colspan="9">Estimated Cost to Develop (organic) $229,388<br>Estimated Schedule Effort (organic) 7.86 months<br>Estimated People Required (organic) 2.59<br></th>
307307
</tr></tfoot>
308308
</table></body></html>

cmd/badges/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def clone_and_process(filename, url, path):
115115
os.chdir(gettempdir())
116116

117117
rmtree(Path(gettempdir()) / 'scc-tmp-path')
118-
git.exec_command('clone', '--depth=1', url, 'scc-tmp-path', cwd='/tmp')
118+
git.exec_command('clone', '--depth=1', url, 'scc-tmp-path', cwd=gettempdir())
119119

120120
os.system('./scc -f json -o ' + str(Path(gettempdir()) / filename) + ' scc-tmp-path')
121121

cmd/badges/main.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,17 @@ func calculate(category string, wage int, res []processor.LanguageSummary) (stri
8888
var value int64
8989

9090
switch category {
91-
case "codes":
92-
fallthrough
93-
case "code":
91+
case "code", "codes":
9492
title = "Code lines"
9593
for _, x := range res {
9694
value += x.Code
9795
}
98-
case "blank":
99-
fallthrough
100-
case "blanks":
96+
case "blank", "blanks":
10197
title = "Blank lines"
10298
for _, x := range res {
10399
value += x.Blank
104100
}
105-
case "comment":
106-
fallthrough
107-
case "comments":
101+
case "comment", "comments":
108102
title = "Comments"
109103
for _, x := range res {
110104
value += x.Comment
@@ -116,9 +110,7 @@ func calculate(category string, wage int, res []processor.LanguageSummary) (stri
116110
}
117111

118112
value = int64(estimateCost(value, wage))
119-
case "lines": // lines is the default
120-
fallthrough
121-
case "line": // lines is the default
113+
case "line", "lines": // lines is the default
122114
fallthrough
123115
default:
124116
//

processor/formatters.go

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,28 @@ var openMetricsSummaryRecordFormat = "scc_%s{language=\"%s\"} %d\n"
7676
var openMetricsFileRecordFormat = "scc_%s{language=\"%s\",file=\"%s\"} %d\n"
7777

7878
func sortSummaryFiles(summary *LanguageSummary) {
79-
switch {
80-
case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "languages" || SortBy == "lang":
79+
switch SortBy {
80+
case "name", "names", "language", "languages", "lang", "langs":
8181
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
8282
return strings.Compare(a.Location, b.Location)
8383
})
84-
case SortBy == "line" || SortBy == "lines":
84+
case "line", "lines":
8585
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
8686
return cmp.Compare(b.Lines, a.Lines)
8787
})
88-
case SortBy == "blank" || SortBy == "blanks":
88+
case "blank", "blanks":
8989
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
9090
return cmp.Compare(b.Blank, a.Blank)
9191
})
92-
case SortBy == "code" || SortBy == "codes":
92+
case "code", "codes":
9393
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
9494
return cmp.Compare(b.Code, a.Code)
9595
})
96-
case SortBy == "comment" || SortBy == "comments":
96+
case "comment", "comments":
9797
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
9898
return cmp.Compare(b.Comment, a.Comment)
9999
})
100-
case SortBy == "complexity" || SortBy == "complexitys" || SortBy == "comp":
100+
case "complexity", "complexitys", "comp":
101101
slices.SortFunc(summary.Files, func(a, b *FileJob) int {
102102
return cmp.Compare(b.Complexity, a.Complexity)
103103
})
@@ -341,46 +341,46 @@ func toCSVSummary(input chan *FileJob) string {
341341
func getRecordsSortFunc() func(a, b []string) int {
342342
// Cater for the common case of adding plural even for those options that don't make sense
343343
// as it's quite common for those who English is not a first language to make a simple mistake
344-
switch {
345-
case SortBy == "name" || SortBy == "names":
344+
switch SortBy {
345+
case "name", "names":
346346
return func(a, b []string) int {
347347
return strings.Compare(a[2], b[2])
348348
}
349-
case SortBy == "language" || SortBy == "languages":
349+
case "language", "languages", "lang", "langs":
350350
return func(a, b []string) int {
351351
return strings.Compare(a[0], b[0])
352352
}
353-
case SortBy == "line" || SortBy == "lines":
353+
case "line", "lines":
354354
return func(a, b []string) int {
355355
i1, _ := strconv.ParseInt(a[3], 10, 64)
356356
i2, _ := strconv.ParseInt(b[3], 10, 64)
357357
return cmp.Compare(i2, i1)
358358
}
359-
case SortBy == "blank" || SortBy == "blanks":
359+
case "blank", "blanks":
360360
return func(a, b []string) int {
361361
i1, _ := strconv.ParseInt(a[6], 10, 64)
362362
i2, _ := strconv.ParseInt(b[6], 10, 64)
363363
return cmp.Compare(i2, i1)
364364
}
365-
case SortBy == "code" || SortBy == "codes":
365+
case "code", "codes":
366366
return func(a, b []string) int {
367367
i1, _ := strconv.ParseInt(a[4], 10, 64)
368368
i2, _ := strconv.ParseInt(b[4], 10, 64)
369369
return cmp.Compare(i2, i1)
370370
}
371-
case SortBy == "comment" || SortBy == "comments":
371+
case "comment", "comments":
372372
return func(a, b []string) int {
373373
i1, _ := strconv.ParseInt(a[5], 10, 64)
374374
i2, _ := strconv.ParseInt(b[5], 10, 64)
375375
return cmp.Compare(i2, i1)
376376
}
377-
case SortBy == "complexity" || SortBy == "complexitys":
377+
case "complexity", "complexitys":
378378
return func(a, b []string) int {
379379
i1, _ := strconv.ParseInt(a[7], 10, 64)
380380
i2, _ := strconv.ParseInt(b[7], 10, 64)
381381
return cmp.Compare(i2, i1)
382382
}
383-
case SortBy == "byte" || SortBy == "bytes":
383+
case "byte", "bytes":
384384
return func(a, b []string) int {
385385
i1, _ := strconv.ParseInt(a[8], 10, 64)
386386
i2, _ := strconv.ParseInt(b[8], 10, 64)
@@ -919,38 +919,7 @@ func fileSummarizeLong(input chan *FileJob) string {
919919
language = append(language, summary)
920920
}
921921

922-
// Cater for the common case of adding plural even for those options that don't make sense
923-
// as it's quite common for those who English is not a first language to make a simple mistake
924-
switch {
925-
case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "langs":
926-
slices.SortFunc(language, func(a, b LanguageSummary) int {
927-
return strings.Compare(a.Name, b.Name)
928-
})
929-
case SortBy == "line" || SortBy == "lines":
930-
slices.SortFunc(language, func(a, b LanguageSummary) int {
931-
return cmp.Compare(b.Lines, a.Lines)
932-
})
933-
case SortBy == "blank" || SortBy == "blanks":
934-
slices.SortFunc(language, func(a, b LanguageSummary) int {
935-
return cmp.Compare(b.Blank, a.Blank)
936-
})
937-
case SortBy == "code" || SortBy == "codes":
938-
slices.SortFunc(language, func(a, b LanguageSummary) int {
939-
return cmp.Compare(b.Code, a.Code)
940-
})
941-
case SortBy == "comment" || SortBy == "comments":
942-
slices.SortFunc(language, func(a, b LanguageSummary) int {
943-
return cmp.Compare(b.Comment, a.Comment)
944-
})
945-
case SortBy == "complexity" || SortBy == "complexitys":
946-
slices.SortFunc(language, func(a, b LanguageSummary) int {
947-
return cmp.Compare(b.Complexity, a.Complexity)
948-
})
949-
default:
950-
slices.SortFunc(language, func(a, b LanguageSummary) int {
951-
return cmp.Compare(b.Count, a.Count)
952-
})
953-
}
922+
language = sortLanguageSummary(language)
954923

955924
startTime := makeTimestampMilli()
956925
for _, summary := range language {
@@ -1263,14 +1232,7 @@ func maxIn(i []int) int {
12631232
return 0
12641233
}
12651234

1266-
m := i[0]
1267-
for _, x := range i {
1268-
if x > m {
1269-
m = x
1270-
}
1271-
}
1272-
1273-
return m
1235+
return slices.Max(i)
12741236
}
12751237

12761238
func meanIn(i []int) int {
@@ -1382,16 +1344,10 @@ func isLeapYear(year int) bool {
13821344
leapFlag := false
13831345
if year%4 == 0 {
13841346
if year%100 == 0 {
1385-
if year%400 == 0 {
1386-
leapFlag = true
1387-
} else {
1388-
leapFlag = false
1389-
}
1347+
leapFlag = year%400 == 0
13901348
} else {
13911349
leapFlag = true
13921350
}
1393-
} else {
1394-
leapFlag = false
13951351
}
13961352
return leapFlag
13971353
}
@@ -1456,40 +1412,40 @@ func sortLanguageSummary(language []LanguageSummary) []LanguageSummary {
14561412
// as it's quite common for those who English is not a first language to make a simple mistake
14571413
// NB in any non name cases if the values are the same we sort by name to ensure
14581414
// deterministic output
1459-
switch {
1460-
case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "languages":
1415+
switch SortBy {
1416+
case "name", "names", "language", "languages", "lang", "langs":
14611417
slices.SortFunc(language, func(a, b LanguageSummary) int {
14621418
return strings.Compare(a.Name, b.Name)
14631419
})
1464-
case SortBy == "line" || SortBy == "lines":
1420+
case "line", "lines":
14651421
slices.SortFunc(language, func(a, b LanguageSummary) int {
14661422
if order := cmp.Compare(b.Lines, a.Lines); order != 0 {
14671423
return order
14681424
}
14691425
return strings.Compare(a.Name, b.Name)
14701426
})
1471-
case SortBy == "blank" || SortBy == "blanks":
1427+
case "blank", "blanks":
14721428
slices.SortFunc(language, func(a, b LanguageSummary) int {
14731429
if order := cmp.Compare(b.Blank, a.Blank); order != 0 {
14741430
return order
14751431
}
14761432
return strings.Compare(a.Name, b.Name)
14771433
})
1478-
case SortBy == "code" || SortBy == "codes":
1434+
case "code", "codes":
14791435
slices.SortFunc(language, func(a, b LanguageSummary) int {
14801436
if order := cmp.Compare(b.Code, a.Code); order != 0 {
14811437
return order
14821438
}
14831439
return strings.Compare(a.Name, b.Name)
14841440
})
1485-
case SortBy == "comment" || SortBy == "comments":
1441+
case "comment", "comments":
14861442
slices.SortFunc(language, func(a, b LanguageSummary) int {
14871443
if order := cmp.Compare(b.Comment, a.Comment); order != 0 {
14881444
return order
14891445
}
14901446
return strings.Compare(a.Name, b.Name)
14911447
})
1492-
case SortBy == "complexity" || SortBy == "complexitys":
1448+
case "complexity", "complexitys":
14931449
slices.SortFunc(language, func(a, b LanguageSummary) int {
14941450
if order := cmp.Compare(b.Complexity, a.Complexity); order != 0 {
14951451
return order

0 commit comments

Comments
 (0)