Early returns in Go
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
Install this rule for wispbit
Add this rule to wispbit and it will run when you open a pull request
Install this rule for Coderabbit
Copy the configuration below and add it to your repository as .coderabbit.yml
in your project root.
reviews:
path_instructions:
- path: "*.go"
instructions: |
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
Install this rule for Greptile
Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
File Path Patterns:
Install this rule for GitHub Copilot
Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
File Path Patterns:
Install this rule for Graphite Diamond
Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
File Path Patterns:
Use with Cline
Copy the rule below and ask Cline to review your code using this rule
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
Use with OpenAI Codex
Copy the rule below and ask OpenAI Codex to review your code using this rule
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
Use with Cursor
Copy the rule below and ask Cursor to review your code using this rule
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
Use with Claude Code
Copy the rule below and ask Claude Code to review your code using this rule
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```
Install this rule for Windsurf
To set up rules for Windsurf Reviews, please see this documentation
Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad:
```go
func processData(data []string) (string, error) {
if len(data) > 0 {
if isValid(data) {
result := ""
for _, item := range data {
if item != "" {
// More nested code here
result += transform(item)
}
}
return result, nil
} else {
return "", errors.New("invalid data")
}
} else {
return "", errors.New("empty data")
}
}
```
Good:
```go
func processData(data []string) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
if !isValid(data) {
return "", errors.New("invalid data")
}
result := ""
for _, item := range data {
if item == "" {
continue
}
result += transform(item)
}
return result, nil
}
```