DEV Community

Baltasar García Perez-Schofield
Baltasar García Perez-Schofield

Posted on • Edited on

¿Y si Python permitiera 'end' como fin de bloque?

En Python, los bloques de código (el código dentro de un bucle, por ejemplo) se marcan utilizando indentación. Es decir, a partir de la siguiente línea que termina con dos puntos (':'), cada instrucción tiene que estar indentada hacia la derecha, hasta el final del bloque.

for _ in range(10):
    print("Python mola!")
Enter fullscreen mode Exit fullscreen mode

Normalmente, no supone un gran problema, pero hay un escenario que me pone "muy malo", como se puede ver a continuación.

i = 0
while i < 10:
    if i % 2 == 0:
        i += 2

    i += 1
Enter fullscreen mode Exit fullscreen mode

Me refiero al final de ambos bloques, el del if y el del while. Para mi, esas dos instrucciones de incremento de i es como si quedasen descolgadas.

Veamos ahora si se pudiera utilizar end como recurso sintáctico para delimitar el final de los bloques. Esto es algo que proporciona Juía y multitud de dialectos de BASIC. ¿Cómo quedaría?

i = 0
while i < 10:
    if i % 2 == 0:
        i += 2
    end

    i += 1
end
Enter fullscreen mode Exit fullscreen mode

Para mi queda todo mucho más ordenado y limpio... sería como decía, un recurso sintáctico, no tendría que traducirse en nada al código objeto, y sería opcional.

¿Y tú, como lo ves? ¿Sería tan terrible?

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more