PyonFX icon indicating copy to clipboard operation
PyonFX copied to clipboard

LBYL -> EAFP

Open CoffeeStraw opened this issue 4 years ago • 3 comments

At the moment of writing, a lot of PyonFX's code use LBYL approach ("look before you leap"). This means that a lot of type checking is done and slow down the whole process.

Instead, we should follow EAFP approach ("it’s easier to ask for forgiveness than permission") and the code should be reviewed using try ... except whenever it is possible.

Together with making the code follow the common Python coding style, we should also have a little boost in performance and improve readability.

CoffeeStraw avatar Apr 10 '21 09:04 CoffeeStraw

I personally prefer LBYL but without type checking. That's the reason of typethint. You inform the user of the types to pass and if they don't follow them, that's their fault not ours.

Ichunjo avatar Jul 30 '21 16:07 Ichunjo

I also feel like EAFP was more pythonic years ago when typehint wasn't a thing but don't quote me about that. Now with mypy and flake8 era, you have to explicitly handle types unless linters will yell at you. You can still cast type to the value but it begins pretty pepega to do that for everything just because you want to satisfy linters in my opinion

Ichunjo avatar Jul 30 '21 17:07 Ichunjo

Using try ... except we're not really doing type-checking anymore. Instead, we're just clarifying the error(s) to the user. Following EAFP is what I think it is better for the project: this is a library, so whatever makes final users' life easier (without really no cons) should be done.

For other kinds of projects, I may agree with your opinion.

CoffeeStraw avatar Jul 31 '21 09:07 CoffeeStraw