ChainSafe / ChainSafe/gossamer

refactor: remove unnecessary Else

Open
#4,020 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
454
Forks
144
PR merge metrics
No merged PRs in 30d

Description

## Issue summary
some of the else statements can be removed and simplify the code. For example:

1. If a variable is set in both branches of an if, it can be replaced with a single if:
```go
var str string
if b {
str = "Hello"
} else {
str = "World"
}
```
can be replaces with:
```go
str := "World"
if b {
str = "Hello"
}
```
2. Another example is if the else is at the end of the method/function it can be avoided:
```go
var Version = func() string {
if VersionMeta != "stable" {
return GetFullVersion()
} else {
return GetStableVersion()
}
}()
```
can be replaces with:
```go
var Version = func() string {
if VersionMeta != "stable" {
return GetFullVersion()
}
return GetStableVersion()
}()
```

3. When in the unit test expect nil or expect some boolean:
```go
if tt.expectNil {
assert.Nil(t, got)
} else {
assert.NotNil(t, got)
}
```
can be replaces with:
```go
assert.Equal(t, tt.ExpectNil, got == nil)
```

4. Some complex if/else if/else ( this is example from the file ./dot/core/service.go):
```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
}
```
can be replaces with:
```go
if !errors.Is(err, blocktree.ErrBlockExists) && block.Header.Number != 0 {
return err
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.