Skip to content

Commit fe0398d

Browse files
committed
Fix logical operators task.
This was asking for a goal that conflicted with moreWaysToReturn.go -- logicalOperators.go considered paths that may circumvent a return statement as acceptable, while moreWaysToReturn.go wanted these maybe cases flagged as problems. Here we take moreWaysToReturn.go as correct and amend the good/bad labels in logicalOperators.go accordingly, also adding a few more test cases.
1 parent 1655c03 commit fe0398d

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

logicalOperators.go

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,76 +50,134 @@ func someOtherCondition() bool {
5050
return true
5151
}
5252

53-
func logicalAndThenBranchGood() {
53+
func logicalAndThenBranchSometimesBad() {
5454

5555
if errorSource() != ErrNone && someOtherCondition() {
56-
// Good: in the then-block, where we're certain there has been an error,
57-
// we return early. In the other branch an error is possible but not certain.
56+
// Bad: there is a route from a positive error test around the 'return' statement.
5857
return
5958
}
6059
doSomething()
6160

6261
}
6362

64-
func logicalAndThenBranchBad() {
63+
func logicalAndThenBranchAlwaysBad() {
6564

6665
if errorSource() != ErrNone && someOtherCondition() {
67-
// Bad: in the then-block, where we're certain there has been an error,
68-
// we do not return early.
66+
// Bad: there is no return statement at all.
6967
insteadOfReturn()
7068
}
7169
doSomething()
7270

7371
}
7472

75-
func logicalAndElseBranchUncertain() {
73+
func logicalAndElseBranchAlwaysBad2() {
7674

7775
if errorSource() == ErrNone && someOtherCondition() {
7876
doSomething()
7977
} else {
80-
// Uncertain: an error is not a precondition for entering either branch.
81-
// We should be conservative and NOT flag this function as a problem.
78+
// Bad: On error we may enter either branch, but neither one returns.
8279
insteadOfReturn()
8380
}
8481
doSomething()
8582

8683
}
8784

88-
func logicalOrElseBranchGood() {
85+
func logicalAndThenBranchGood() {
86+
87+
if someOtherCondition() && errorSource() != ErrNone {
88+
// Good: whenever an error is indicated we return.
89+
return
90+
}
91+
doSomething()
92+
93+
}
94+
95+
func logicalAndElseBranchGood() {
96+
97+
if someOtherCondition() && errorSource() == ErrNone {
98+
// Good: whenever an error is indicated we return.
99+
doSomething()
100+
} else {
101+
return
102+
}
103+
104+
}
105+
106+
func logicalAndElseBranchGood2() {
107+
108+
if errorSource() == ErrNone && someOtherCondition() {
109+
// Good: whenever an error is indicated we return.
110+
doSomething()
111+
} else {
112+
return
113+
}
114+
doSomething()
115+
116+
}
117+
118+
func logicalOrElseBranchSometimesBad() {
89119

90120
if errorSource() == ErrNone || someOtherCondition() {
91121
doSomething()
92122
} else {
93-
// Good: in the else-block, where we're certain there has been an error,
94-
// we return early. In the other branch an error is possible but not certain.
123+
// Bad: there is a route from a failing error test that bypasses the return statement.
95124
return
96125
}
97126
doSomething()
98127

99128
}
100129

101-
func logicalOrElseBranchBad() {
130+
func logicalOrElseBranchAlwaysBad() {
102131

103132
if errorSource() == ErrNone || someOtherCondition() {
104133
doSomething()
105134
} else {
106-
// Bad: in the else-block, where we're certain there has been an error,
107-
// we do not return early.
135+
// Bad: regardless of error status, we do not return.
108136
insteadOfReturn()
109137
}
110138
doSomething()
111139

112140
}
113141

114-
func logicalOrThenBranchUncertain() {
142+
func logicalOrThenBranchAlwaysBad() {
115143

116144
if errorSource() != ErrNone || someOtherCondition() {
117-
// Uncertain: an error is not a precondition for entering either branch.
118-
// We should be conservative and NOT flag this function as a problem.
145+
// Bad: regardless of error status, we do not return.
119146
insteadOfReturn()
120147
} else {
121148
doSomething()
122149
}
123150
doSomething()
124151

125152
}
153+
154+
func logicalOrThenBranchGood() {
155+
156+
if someOtherCondition() || errorSource() != ErrNone {
157+
// Good: whenever an error is indicated we return.
158+
return
159+
}
160+
doSomething()
161+
162+
}
163+
164+
func logicalOrElseBranchGood() {
165+
166+
if someOtherCondition() || errorSource() == ErrNone {
167+
// Good: whenever an error is indicated we return.
168+
doSomething()
169+
} else {
170+
return
171+
}
172+
173+
}
174+
175+
func logicalOrThenBranchGood2() {
176+
177+
if errorSource() != ErrNone || someOtherCondition() {
178+
// Good: whenever an error is indicated we return.
179+
return
180+
}
181+
doSomething()
182+
183+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy