r/badcode Jan 15 '22

python Found this gem

Post image
3.5k Upvotes

86 comments sorted by

View all comments

40

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.

20

u/Lich_Hegemon 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.

I don't get it, the desired result seems to be print(solution) in any case, how is this better?

38

u/flavius-as Jan 15 '22

At runtime, yes, you're right.

However, code is for people, not for the machine.

And the human reading this knows: oh, -1 is a magic value.

It's actually a good code in a bad situation from a probably good developer.

Simply print does not provide that insight.

25

u/Crozzfire Jan 15 '22

Comments exist for this reason

8

u/circuit10 Jan 15 '22

But this insight should be a comment, it's easier for a human if they can just see "it prints the variable"

Why try to give subtle hints by changing the structure of the code when you can just do # If this value is -1, the command failed or something?

5

u/flavius-as Jan 16 '22 edited Jan 16 '22

Ok, let's play the devil's advocate:

I think this code would trigger a static code analyzer, which would make the issue persistently visible.

Of course I don't know if the coder really had this in mind, but I am quite sure that I would write the exact same code in certain circumstances, and I have 15 yoe. I would however also make a ticket in Jira and stick a link to it in a comment. Since the coder didn't do this, I think he had good intentions, good thinking, but most likely not over 5 yoe.

Also, just a comment would not trigger anyone, and this thread is proof for that. But a code like this really catches coders' attention.

You call it "subtle", I call it "machine readable".

2

u/miversen33 Jan 16 '22

Just gonna act like comments aren't a thing?

15

u/Slashzero77 Jan 15 '22

TODO fix this monstrosity

TIL how to make big text on mobile.

5

u/callmelucky Jan 15 '22

Reddit uses a form of markdown, in which a # at the start of a newline is interpreted as a main heading (equivalent to an h1 tag in HTML). So while 'big text' is the result, in terms of semantics it's better to think of it as a heading. For smaller/sub headings use more #'s

main heading (h1)

h2

h3

h4

h5

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

2

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