From 0c79e3e5c64d074f8b57316cf3dae23575c47478 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Mon, 28 Oct 2024 05:26:57 +0800 Subject: [PATCH] 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 --- SCC-OUTPUT-REPORT.html | 72 +++++++++++++++---------------- cmd/badges/example.py | 2 +- cmd/badges/main.go | 16 ++----- processor/formatters.go | 96 +++++++++++------------------------------ 4 files changed, 67 insertions(+), 119 deletions(-) diff --git a/SCC-OUTPUT-REPORT.html b/SCC-OUTPUT-REPORT.html index 7e53bee92..1ed4a258d 100644 --- a/SCC-OUTPUT-REPORT.html +++ b/SCC-OUTPUT-REPORT.html @@ -13,13 +13,13 @@ Go 27 - 9629 - 1462 - 449 - 7718 - 1513 - 256852 - 4141 + 9577 + 1461 + 447 + 7669 + 1413 + 254907 + 4126 processor/workers_test.go @@ -33,13 +33,13 @@ processor/formatters.go - 1561 - 209 - 41 - 1311 - 263 - 46071 - 792 + 1517 + 208 + 39 + 1270 + 163 + 44230 + 784 processor/formatters_test.go @@ -93,13 +93,13 @@ cmd/badges/main.go - 382 + 374 60 17 - 305 + 297 59 - 9675 - 243 + 9571 + 239 processor/workers_tokei_test.go @@ -130,16 +130,6 @@ 37 4585 97 - - processor/structs.go - - 199 - 21 - 18 - 160 - 17 - 5883 - 139 cmd/badges/main_test.go @@ -150,6 +140,16 @@ 10 4088 106 + + processor/structs.go + + 199 + 21 + 18 + 160 + 17 + 5883 + 139 processor/workers_regression_test.go @@ -294,15 +294,15 @@ Total 27 - 9629 - 1462 - 449 - 7718 - 1513 - 256852 - 4141 + 9577 + 1461 + 447 + 7669 + 1413 + 254907 + 4126 - Estimated Cost to Develop (organic) $230,927
Estimated Schedule Effort (organic) 7.88 months
Estimated People Required (organic) 2.60
+ Estimated Cost to Develop (organic) $229,388
Estimated Schedule Effort (organic) 7.86 months
Estimated People Required (organic) 2.59
\ No newline at end of file diff --git a/cmd/badges/example.py b/cmd/badges/example.py index 1130bec5f..b6faec7b0 100644 --- a/cmd/badges/example.py +++ b/cmd/badges/example.py @@ -115,7 +115,7 @@ def clone_and_process(filename, url, path): os.chdir(gettempdir()) rmtree(Path(gettempdir()) / 'scc-tmp-path') - git.exec_command('clone', '--depth=1', url, 'scc-tmp-path', cwd='/tmp') + git.exec_command('clone', '--depth=1', url, 'scc-tmp-path', cwd=gettempdir()) os.system('./scc -f json -o ' + str(Path(gettempdir()) / filename) + ' scc-tmp-path') diff --git a/cmd/badges/main.go b/cmd/badges/main.go index 739af210e..138307457 100644 --- a/cmd/badges/main.go +++ b/cmd/badges/main.go @@ -88,23 +88,17 @@ func calculate(category string, wage int, res []processor.LanguageSummary) (stri var value int64 switch category { - case "codes": - fallthrough - case "code": + case "code", "codes": title = "Code lines" for _, x := range res { value += x.Code } - case "blank": - fallthrough - case "blanks": + case "blank", "blanks": title = "Blank lines" for _, x := range res { value += x.Blank } - case "comment": - fallthrough - case "comments": + case "comment", "comments": title = "Comments" for _, x := range res { value += x.Comment @@ -116,9 +110,7 @@ func calculate(category string, wage int, res []processor.LanguageSummary) (stri } value = int64(estimateCost(value, wage)) - case "lines": // lines is the default - fallthrough - case "line": // lines is the default + case "line", "lines": // lines is the default fallthrough default: // diff --git a/processor/formatters.go b/processor/formatters.go index d06889a0d..e73854599 100644 --- a/processor/formatters.go +++ b/processor/formatters.go @@ -76,28 +76,28 @@ var openMetricsSummaryRecordFormat = "scc_%s{language=\"%s\"} %d\n" var openMetricsFileRecordFormat = "scc_%s{language=\"%s\",file=\"%s\"} %d\n" func sortSummaryFiles(summary *LanguageSummary) { - switch { - case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "languages" || SortBy == "lang": + switch SortBy { + case "name", "names", "language", "languages", "lang", "langs": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return strings.Compare(a.Location, b.Location) }) - case SortBy == "line" || SortBy == "lines": + case "line", "lines": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return cmp.Compare(b.Lines, a.Lines) }) - case SortBy == "blank" || SortBy == "blanks": + case "blank", "blanks": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return cmp.Compare(b.Blank, a.Blank) }) - case SortBy == "code" || SortBy == "codes": + case "code", "codes": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return cmp.Compare(b.Code, a.Code) }) - case SortBy == "comment" || SortBy == "comments": + case "comment", "comments": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return cmp.Compare(b.Comment, a.Comment) }) - case SortBy == "complexity" || SortBy == "complexitys" || SortBy == "comp": + case "complexity", "complexitys", "comp": slices.SortFunc(summary.Files, func(a, b *FileJob) int { return cmp.Compare(b.Complexity, a.Complexity) }) @@ -341,46 +341,46 @@ func toCSVSummary(input chan *FileJob) string { func getRecordsSortFunc() func(a, b []string) int { // Cater for the common case of adding plural even for those options that don't make sense // as it's quite common for those who English is not a first language to make a simple mistake - switch { - case SortBy == "name" || SortBy == "names": + switch SortBy { + case "name", "names": return func(a, b []string) int { return strings.Compare(a[2], b[2]) } - case SortBy == "language" || SortBy == "languages": + case "language", "languages", "lang", "langs": return func(a, b []string) int { return strings.Compare(a[0], b[0]) } - case SortBy == "line" || SortBy == "lines": + case "line", "lines": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[3], 10, 64) i2, _ := strconv.ParseInt(b[3], 10, 64) return cmp.Compare(i2, i1) } - case SortBy == "blank" || SortBy == "blanks": + case "blank", "blanks": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[6], 10, 64) i2, _ := strconv.ParseInt(b[6], 10, 64) return cmp.Compare(i2, i1) } - case SortBy == "code" || SortBy == "codes": + case "code", "codes": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[4], 10, 64) i2, _ := strconv.ParseInt(b[4], 10, 64) return cmp.Compare(i2, i1) } - case SortBy == "comment" || SortBy == "comments": + case "comment", "comments": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[5], 10, 64) i2, _ := strconv.ParseInt(b[5], 10, 64) return cmp.Compare(i2, i1) } - case SortBy == "complexity" || SortBy == "complexitys": + case "complexity", "complexitys": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[7], 10, 64) i2, _ := strconv.ParseInt(b[7], 10, 64) return cmp.Compare(i2, i1) } - case SortBy == "byte" || SortBy == "bytes": + case "byte", "bytes": return func(a, b []string) int { i1, _ := strconv.ParseInt(a[8], 10, 64) i2, _ := strconv.ParseInt(b[8], 10, 64) @@ -919,38 +919,7 @@ func fileSummarizeLong(input chan *FileJob) string { language = append(language, summary) } - // Cater for the common case of adding plural even for those options that don't make sense - // as it's quite common for those who English is not a first language to make a simple mistake - switch { - case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "langs": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return strings.Compare(a.Name, b.Name) - }) - case SortBy == "line" || SortBy == "lines": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Lines, a.Lines) - }) - case SortBy == "blank" || SortBy == "blanks": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Blank, a.Blank) - }) - case SortBy == "code" || SortBy == "codes": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Code, a.Code) - }) - case SortBy == "comment" || SortBy == "comments": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Comment, a.Comment) - }) - case SortBy == "complexity" || SortBy == "complexitys": - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Complexity, a.Complexity) - }) - default: - slices.SortFunc(language, func(a, b LanguageSummary) int { - return cmp.Compare(b.Count, a.Count) - }) - } + language = sortLanguageSummary(language) startTime := makeTimestampMilli() for _, summary := range language { @@ -1263,14 +1232,7 @@ func maxIn(i []int) int { return 0 } - m := i[0] - for _, x := range i { - if x > m { - m = x - } - } - - return m + return slices.Max(i) } func meanIn(i []int) int { @@ -1382,16 +1344,10 @@ func isLeapYear(year int) bool { leapFlag := false if year%4 == 0 { if year%100 == 0 { - if year%400 == 0 { - leapFlag = true - } else { - leapFlag = false - } + leapFlag = year%400 == 0 } else { leapFlag = true } - } else { - leapFlag = false } return leapFlag } @@ -1456,40 +1412,40 @@ func sortLanguageSummary(language []LanguageSummary) []LanguageSummary { // as it's quite common for those who English is not a first language to make a simple mistake // NB in any non name cases if the values are the same we sort by name to ensure // deterministic output - switch { - case SortBy == "name" || SortBy == "names" || SortBy == "language" || SortBy == "languages": + switch SortBy { + case "name", "names", "language", "languages", "lang", "langs": slices.SortFunc(language, func(a, b LanguageSummary) int { return strings.Compare(a.Name, b.Name) }) - case SortBy == "line" || SortBy == "lines": + case "line", "lines": slices.SortFunc(language, func(a, b LanguageSummary) int { if order := cmp.Compare(b.Lines, a.Lines); order != 0 { return order } return strings.Compare(a.Name, b.Name) }) - case SortBy == "blank" || SortBy == "blanks": + case "blank", "blanks": slices.SortFunc(language, func(a, b LanguageSummary) int { if order := cmp.Compare(b.Blank, a.Blank); order != 0 { return order } return strings.Compare(a.Name, b.Name) }) - case SortBy == "code" || SortBy == "codes": + case "code", "codes": slices.SortFunc(language, func(a, b LanguageSummary) int { if order := cmp.Compare(b.Code, a.Code); order != 0 { return order } return strings.Compare(a.Name, b.Name) }) - case SortBy == "comment" || SortBy == "comments": + case "comment", "comments": slices.SortFunc(language, func(a, b LanguageSummary) int { if order := cmp.Compare(b.Comment, a.Comment); order != 0 { return order } return strings.Compare(a.Name, b.Name) }) - case SortBy == "complexity" || SortBy == "complexitys": + case "complexity", "complexitys": slices.SortFunc(language, func(a, b LanguageSummary) int { if order := cmp.Compare(b.Complexity, a.Complexity); order != 0 { return order