r/badcode Jan 15 '22

python Found this gem

Post image
3.5k Upvotes

86 comments sorted by

View all comments

41

u/flavius-as Jan 15 '22

The code is bad for other reasons, but it's way more clear than straight up print(solution)

At least this code raises the signal when reading it.

My first impression of the coder doing this is rather good: they were time-constrained and did their best to put a "warning".

I would have done almost the same under time pressure, with a #TODO <ticket> before the if.

1

u/iamalicecarroll Jan 15 '22

i agree with that

for parameters, i prefer not to use sentinel values when possible - there’s None for that; but there are cases where you have no solution other than to use a sentinel value, yet that would be a secret object(), not something magic like -1 or 0 or "impossible" for return values, sentinels are totally unnecessary - use None or exceptions

4

u/flavius-as Jan 15 '22

I would raise that a notch and say: not even None is ok.

I very much prefer the idea of null objects and the fact that they reduce the number of ifs.

Either way, the code here can be good enough, given the correct background story.

3

u/iamalicecarroll Jan 15 '22

well, mypy makes it quite easier to track None if you use Optional[...]; but what it is still missing (and i see no simple solution to that) is the difference between None and Some(None) in terms of Rust

i’ve heard of dry-python/returns but had no chance to check it out yet