Skip to content

Commit 6df8b5a

Browse files
committed
Add section for else branches after loop exit
1 parent 4c7eb06 commit 6df8b5a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.adoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,51 @@ end
19531953
end
19541954
----
19551955

1956+
=== `else` branches after loop exit [[else-after-loop-exit]]
1957+
1958+
There is no need to wrap code inside an `else` when the `if` statement skips the rest of the loop.
1959+
1960+
[source,ruby]
1961+
----
1962+
# bad
1963+
[0, 1, 2, 3].each do |item|
1964+
if some_condition
1965+
do_something
1966+
next
1967+
else
1968+
do_something_else
1969+
end
1970+
end
1971+
1972+
[0, 1, 2, 3].each do |item|
1973+
if some_condition
1974+
do_something
1975+
break
1976+
else
1977+
do_something_else
1978+
end
1979+
end
1980+
1981+
[0, 1, 2, 3].each do |item|
1982+
if some_condition
1983+
do_something
1984+
return
1985+
else
1986+
do_something_else
1987+
end
1988+
end
1989+
1990+
# good
1991+
[0, 1, 2, 3].each do |item|
1992+
if some_condition
1993+
do_something
1994+
next
1995+
end
1996+
1997+
do_something_else
1998+
end
1999+
----
2000+
19562001
== Exceptions
19572002

19582003
=== `raise` vs `fail` [[prefer-raise-over-fail]]

0 commit comments

Comments
 (0)