Skip to content

Commit 45cc868

Browse files
committed
chore: replace if blocks with min/max functions
Simplify code where possible. Signed-off-by: Dmitriy Matrenichev <[email protected]>
1 parent a5bd770 commit 45cc868

File tree

10 files changed

+13
-39
lines changed

10 files changed

+13
-39
lines changed

hack/gotagsrewrite/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ func findHighestProtoNum(structNode *ast.StructType) (int, error) {
194194
return nil, err
195195
}
196196

197-
if num > highestNum {
198-
highestNum = num
199-
}
197+
highestNum = max(highestNum, num)
200198

201199
return nil, nil
202200
})

internal/app/machined/internal/server/v1alpha1/v1alpha1_monitoring.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ func (s *Server) SystemStat(ctx context.Context, in *emptypb.Empty) (*machine.Sy
9393
maxCore := int64(-1)
9494

9595
for core := range in {
96-
if core > maxCore {
97-
maxCore = core
98-
}
96+
maxCore = max(maxCore, core)
9997
}
10098

10199
slc := make([]*machine.CPUStat, maxCore+1)

internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,7 @@ func (s *Server) DiskUsage(req *machine.DiskUsageRequest, obj machine.MachineSer
973973
} else {
974974
currentDepth := int32(strings.Count(fi.FullPath, archiver.OSPathSeparator)) - rootDepth
975975

976-
size := fi.FileInfo.Size()
977-
if size < 0 {
978-
size = 0
979-
}
976+
size := max(fi.FileInfo.Size(), 0)
980977

981978
// kcore file size gives wrong value, this code should be smarter when it reads it
982979
// TODO: figure out better way to skip such file

internal/app/machined/pkg/adapters/perf/cpu.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ func (a cpu) Update(stat *procfs.Stat) {
4444
maxCore := int64(-1)
4545

4646
for core := range in {
47-
if core > maxCore {
48-
maxCore = core
49-
}
47+
maxCore = max(maxCore, core)
5048
}
5149

5250
slc := make([]perf.CPUStat, maxCore+1)

internal/app/machined/pkg/controllers/network/operator/dhcp4.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ func (d *DHCP4) Run(ctx context.Context, notifyCh chan<- struct{}) {
182182
renewInterval /= 2
183183
}
184184

185-
if renewInterval < minRenewDuration {
186-
renewInterval = minRenewDuration
187-
}
185+
renewInterval = max(renewInterval, minRenewDuration)
188186

189187
for {
190188
select {

internal/app/machined/pkg/controllers/network/operator/dhcp6.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ func (d *DHCP6) Run(ctx context.Context, notifyCh chan<- struct{}) {
9595
renewInterval /= 2
9696
}
9797

98-
if renewInterval < minRenewDuration {
99-
renewInterval = minRenewDuration
100-
}
98+
renewInterval = max(renewInterval, minRenewDuration)
10199

102100
select {
103101
case <-ctx.Done():

internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_events.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ func (e *Events) Watch(f runtime.WatchFunc, opt ...runtime.WatchOptionFunc) erro
127127
// event to be published
128128
pos := e.writePos
129129
minPos := e.writePos - int64(e.cap-e.gap)
130-
131-
if minPos < 0 {
132-
minPos = 0
133-
}
130+
minPos = max(minPos, 0)
134131

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

143-
if pos < minPos {
144-
pos = minPos
145-
}
140+
pos = max(pos, minPos)
146141
}
147142
case !opts.TailID.IsNil():
148143
pos = minPos + int64(sort.Search(int(pos-minPos), func(i int) bool {

internal/pkg/containers/cri/cri.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,7 @@ func (i *inspector) buildContainer(container *runtimeapi.Container) (*ctrs.Conta
286286
}
287287

288288
func safeCut(id string, i int) string {
289-
if len(id) > i {
290-
return id[:i]
291-
}
292-
293-
return id
289+
return id[:min(i, len(id))]
294290
}
295291

296292
// Pods collects information about running pods & containers.

internal/pkg/dashboard/components/components.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ func (fg *fieldGroup) String() string {
4646
}
4747

4848
func (fg *fieldGroup) maxFieldNameLength() int {
49-
max := 0
49+
result := 0
5050

5151
for _, f := range fg.fields {
52-
if len(f.Name) > max {
53-
max = len(f.Name)
54-
}
52+
result = max(result, len(f.Name))
5553
}
5654

57-
return max
55+
return result
5856
}
5957

6058
// padRight pads a string to the specified width by appending spaces to the end.

internal/pkg/dashboard/components/graphs.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ func (widget *BaseGraph) OnAPIDataChange(node string, data *apidata.Data) {
4949
for i, name := range widget.DataLabels {
5050
series := nodeData.Series[name]
5151

52-
if len(series) < width {
53-
width = len(series)
54-
}
52+
width = min(width, len(series))
5553

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

0 commit comments

Comments
 (0)