Open EmilGeorgiev opened 3 weeks ago
some of the else statements can be removed and simplify the code. For example:
If a variable is set in both branches of an if, it can be replaced with a single if:
var str string if b { str = "Hello" } else { str = "World" }
can be replaces with:
str := "World" if b { str = "Hello" }
Another example is if the else is at the end of the method/function it can be avoided:
var Version = func() string { if VersionMeta != "stable" { return GetFullVersion() } else { return GetStableVersion() } }()
var Version = func() string { if VersionMeta != "stable" { return GetFullVersion() } return GetStableVersion() }()
When in the unit test expect nil or expect some boolean:
if tt.expectNil { assert.Nil(t, got) } else { assert.NotNil(t, got) }
assert.Equal(t, tt.ExpectNil, got == nil)
Some complex if/else if/else ( this is example from the file ./dot/core/service.go):
if errors.Is(err, blocktree.ErrParentNotFound) && block.Header.Number != 0 { return err } else if errors.Is(err, blocktree.ErrBlockExists) || block.Header.Number == 0 { // this is fine } else { return err }
if !errors.Is(err, blocktree.ErrBlockExists) && block.Header.Number != 0 { return err }
Issue summary
some of the else statements can be removed and simplify the code. For example:
If a variable is set in both branches of an if, it can be replaced with a single if:
can be replaces with:
Another example is if the else is at the end of the method/function it can be avoided:
can be replaces with:
When in the unit test expect nil or expect some boolean:
can be replaces with:
Some complex if/else if/else ( this is example from the file ./dot/core/service.go):
can be replaces with: