coconut
coconut copied to clipboard
Feature: labeled `break` and `continue` statements
Are there plans to add support for labeled for
, while
, break
, and continue
statements?
Reasons
This feature is primarily intended for correctness' sake, but also convenience.
Correctness: Say I'm refactoring:
while ...:
# big code block 1
for ...:
# big code block 2
# big code block 3
break
# big code block 4
into something like
while ...:
# big code block 1
# big code block 3
break
# big code block 4
The break
statement was intended to only apply to the inner loop, but amidst the KLOC's I accidently left the break statement inside the outer loop. This is not correct. Labeled break's would prevent this issue.
Convenience: I often write small loops to check any possible valid condition and apply this check to an entire sequence. Usually, simplicity requires lifting the entire nested loops into their own function. For example,
"""Checks if a board state ever had a 1 adjacent to the player"""
def readable_check(trajectory):
for pos, board in trajectory:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
return True
return False
trajectory = ...
# check if player was ever touching a 1
check = readable_check(trajectory) # readable, not concise, not inline
check = any(board[pos+dir] == 1 for board in trajectory for pos in [N, S, E, W]) # concise, not super readable, inline
# readable, not concise, inline
check: bool
for pos, board in trajectory:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
check = True
break
if check:
break
else:
check = False
Maybe it's just my opinion, but I think labeled break
s and continue
s would be a more pleasant way to express myself here:
# readable, reasonably-concise, inline
check: bool
trajectory_loop:
for pos, board in trajectory:
positions_loop:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
check = True
break trajectory_loop
else:
check = False