Skip to content

Commit

Permalink
chore: replace if blocks with min/max functions
Browse files Browse the repository at this point in the history
Simplify code where possible.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Aug 16, 2024
1 parent a5bd770 commit 45cc868
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 39 deletions.
4 changes: 1 addition & 3 deletions hack/gotagsrewrite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ func findHighestProtoNum(structNode *ast.StructType) (int, error) {
return nil, err
}

if num > highestNum {
highestNum = num
}
highestNum = max(highestNum, num)

return nil, nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func (s *Server) SystemStat(ctx context.Context, in *emptypb.Empty) (*machine.Sy
maxCore := int64(-1)

for core := range in {
if core > maxCore {
maxCore = core
}
maxCore = max(maxCore, core)
}

slc := make([]*machine.CPUStat, maxCore+1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,7 @@ func (s *Server) DiskUsage(req *machine.DiskUsageRequest, obj machine.MachineSer
} else {
currentDepth := int32(strings.Count(fi.FullPath, archiver.OSPathSeparator)) - rootDepth

size := fi.FileInfo.Size()
if size < 0 {
size = 0
}
size := max(fi.FileInfo.Size(), 0)

// kcore file size gives wrong value, this code should be smarter when it reads it
// TODO: figure out better way to skip such file
Expand Down
4 changes: 1 addition & 3 deletions internal/app/machined/pkg/adapters/perf/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ func (a cpu) Update(stat *procfs.Stat) {
maxCore := int64(-1)

for core := range in {
if core > maxCore {
maxCore = core
}
maxCore = max(maxCore, core)
}

slc := make([]perf.CPUStat, maxCore+1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ func (d *DHCP4) Run(ctx context.Context, notifyCh chan<- struct{}) {
renewInterval /= 2
}

if renewInterval < minRenewDuration {
renewInterval = minRenewDuration
}
renewInterval = max(renewInterval, minRenewDuration)

for {
select {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ func (d *DHCP6) Run(ctx context.Context, notifyCh chan<- struct{}) {
renewInterval /= 2
}

if renewInterval < minRenewDuration {
renewInterval = minRenewDuration
}
renewInterval = max(renewInterval, minRenewDuration)

select {
case <-ctx.Done():
Expand Down
9 changes: 2 additions & 7 deletions internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ func (e *Events) Watch(f runtime.WatchFunc, opt ...runtime.WatchOptionFunc) erro
// event to be published
pos := e.writePos
minPos := e.writePos - int64(e.cap-e.gap)

if minPos < 0 {
minPos = 0
}
minPos = max(minPos, 0)

// calculate initial position based on options
switch {
Expand All @@ -140,9 +137,7 @@ func (e *Events) Watch(f runtime.WatchFunc, opt ...runtime.WatchOptionFunc) erro
} else {
pos -= int64(opts.TailEvents)

if pos < minPos {
pos = minPos
}
pos = max(pos, minPos)
}
case !opts.TailID.IsNil():
pos = minPos + int64(sort.Search(int(pos-minPos), func(i int) bool {
Expand Down
6 changes: 1 addition & 5 deletions internal/pkg/containers/cri/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,7 @@ func (i *inspector) buildContainer(container *runtimeapi.Container) (*ctrs.Conta
}

func safeCut(id string, i int) string {
if len(id) > i {
return id[:i]
}

return id
return id[:min(i, len(id))]
}

// Pods collects information about running pods & containers.
Expand Down
8 changes: 3 additions & 5 deletions internal/pkg/dashboard/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ func (fg *fieldGroup) String() string {
}

func (fg *fieldGroup) maxFieldNameLength() int {
max := 0
result := 0

for _, f := range fg.fields {
if len(f.Name) > max {
max = len(f.Name)
}
result = max(result, len(f.Name))
}

return max
return result
}

// padRight pads a string to the specified width by appending spaces to the end.
Expand Down
4 changes: 1 addition & 3 deletions internal/pkg/dashboard/components/graphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func (widget *BaseGraph) OnAPIDataChange(node string, data *apidata.Data) {
for i, name := range widget.DataLabels {
series := nodeData.Series[name]

if len(series) < width {
width = len(series)
}
width = min(width, len(series))

widget.Data[i] = widget.leftPadSeries(series[len(series)-width:], 2)
}
Expand Down

0 comments on commit 45cc868

Please sign in to comment.