Skip to content

Commit a402df0

Browse files
authored
Maintainability (#58)
* min number and cognitive complexity * variable name * revert match constant and import math
1 parent 37c5713 commit a402df0

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

database/poll.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package database
22

33
import (
44
"context"
5+
"math"
6+
"slices"
57
"sort"
68
"time"
79

@@ -224,19 +226,15 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st
224226
for _, picks := range votes {
225227
// Go over picks until we find a non-eliminated candidate
226228
for _, candidate := range picks {
227-
if !containsValue(eliminated, candidate) {
228-
if _, ok := tallied[candidate]; ok {
229-
tallied[candidate]++
230-
} else {
231-
tallied[candidate] = 1
232-
}
229+
if !slices.Contains(eliminated, candidate) {
230+
tallied[candidate]++
233231
voteCount += 1
234232
break
235233
}
236234
}
237235
}
238236
// Eliminate lowest vote getter
239-
minVote := 1000000 //the smallest number of votes received thus far (to find who is in last)
237+
minVote := math.MaxInt //the smallest number of votes received thus far (to find who is in last)
240238
minPerson := make([]string, 0) //the person(s) with the least votes that need removed
241239
for person, vote := range tallied {
242240
if vote < minVote { // this should always be true round one, to set a true "who is in last"
@@ -343,15 +341,6 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) {
343341
return nil, nil
344342
}
345343

346-
func containsValue(slice []string, value string) bool {
347-
for _, item := range slice {
348-
if item == value {
349-
return true
350-
}
351-
}
352-
return false
353-
}
354-
355344
// orderOptions takes a RankedVote's options, and returns an ordered list of
356345
// their choices
357346
//
@@ -366,11 +355,11 @@ func containsValue(slice []string, value string) bool {
366355
func orderOptions(ctx context.Context, options map[string]int) []string {
367356
// Figure out all the ranks they've listed
368357
var ranks []int = make([]int, len(options))
369-
reverse_map := make(map[int]string)
358+
reverseMap := make(map[int]string)
370359
i := 0
371360
for option, rank := range options {
372361
ranks[i] = rank
373-
reverse_map[rank] = option
362+
reverseMap[rank] = option
374363
i += 1
375364
}
376365

@@ -379,7 +368,7 @@ func orderOptions(ctx context.Context, options map[string]int) []string {
379368
// normalise the ranks for counts that don't start at 1
380369
var choices []string = make([]string, len(ranks))
381370
for idx, rank := range ranks {
382-
choices[idx] = reverse_map[rank]
371+
choices[idx] = reverseMap[rank]
383372
}
384373

385374
return choices

0 commit comments

Comments
 (0)