Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: labeled break and continue statements #659

Open
JacobFV opened this issue May 12, 2022 · 0 comments
Open

Feature: labeled break and continue statements #659

JacobFV opened this issue May 12, 2022 · 0 comments
Labels

Comments

@JacobFV
Copy link

JacobFV commented May 12, 2022

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 breaks and continues 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
@evhub evhub added the feature label May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants