r/badcode • • May 05 '20

python Next Level

Post image
840 Upvotes

56 comments sorted by

145

u/[deleted] May 05 '20

For that time when you want all strings to count as negative.

30

u/[deleted] May 05 '20

They could at least have checked for ValueError instead of just any kind of error

13

u/zacharypamela May 05 '20 edited May 05 '20

For that, you'll want to call isNotNumber, which tries to calls lower on the arg.

5

u/RFC793 May 05 '20

lower

4

u/zacharypamela May 05 '20

Damn. That's what I get for trying to remember the names of functions I rarely use. I think lcase might be VBA.

4

u/RFC793 May 05 '20

Yeah LCASE$ goes back to at least QBasic. Perhaps original BASIC as well?

1

u/dagbrown May 06 '20

Nah, original BASIC ran on machines that had no lowercase so LCASE$ didn't make any sense.

BASIC is a much older language than people think.

1

u/ethanjf99 May 06 '20

I played with it in the 80s as a kid ... how old is it?

3

u/dagbrown May 06 '20 edited May 06 '20

Oh, much, much older than you are.

The first BASIC interpreter that was actually called BASIC came out in 1964. People had been working on various versions of the idea since the 1950s.

The core idea behind the BASIC operating environment is a REPL (you know, the interactive environment which is an idea that comes from LISP-land), but a programming language which is kind of like FORTRAN but without the silly FORTRAN syntax. Add the idea that you can store a program in RAM just by prepending line numbers in front of your lines of code, instead of having to make designated source code files which you have to edit offline, and you set up a situation which was perfect for the microcomputers of the 1980s.

1

u/ethanjf99 May 06 '20

Oh yeah—I vividly remember keying in the small BASIC games on the back of PC magazine!

-7

u/Raknarg May 06 '20

If you're programming in python, there's no excuse to not be using mypy and type hinting

40

u/nickcash May 05 '20
>>> isNegative(1+1j)
True

27

u/sarlok May 05 '20

NaN is totally negative. I see that now.

8

u/pgbabse May 06 '20

NaN: NegativeAndNumber

Was there really any doubt?

25

u/StealthRabbi May 05 '20

abomination.py

Does it count if you do it purposefully?

19

u/[deleted] May 05 '20

When I suggested cleaning up code by using exceptions, this is not what I had in mind.

10

u/Square789 int mystery=*((int*)rand()) May 05 '20

The real sin here is that weird colon-placement.

3

u/recycle4science May 06 '20

What a tangent.

9

u/PedroPossato May 05 '20

return n!=abs(n)

1

u/Reversal_ May 09 '20

return n < 0

1

u/TheMightyBiz May 06 '20

Zero tho

2

u/ethanjf99 May 06 '20
return n && n != Math.abs(n); // JavaScript

2

u/[deleted] May 06 '20
const isNegativeImpl = require('is-negative');

function isNegative(n) {
    return isNegativeImpl(n);
}

19

u/mario_olofo May 05 '20

And i though that Math.abs(a) != a was bad enough

6

u/Mexenstein May 05 '20

Why not return math.sqrt(n*n) == n?

9

u/oogleh May 06 '20

Why not return n < 0 ? Because it's /r/badcode

4

u/pizza_tent May 06 '20

Elaborate

1

u/alex_hawks May 06 '20

Weird stupid edge cases. Stupid overflow errors

1

u/Mexenstein May 06 '20

Please don't take my comment seriously, it was meant as a joke.

3

u/Dubmove May 05 '20

Yes, 100 IQ code

7

u/Pooneapple May 05 '20

return n < 0

13

u/Dubmove May 05 '20

We don't do this here.

3

u/Pooneapple May 05 '20

return ( n ** 2 ) > 0?

1

u/SirNoodlehe May 06 '20

But that will always return true except for 0

2

u/Plus_Cryptographer May 06 '20

I think you should throw an error on n == 0 just so you have a triple state of positive, negative and 0.

2

u/r3ds70rm May 05 '20

Interesting way, lol. It hurts

2

u/KingErroneous May 06 '20

Wait until python can handle complex numbers...

6

u/Nebuli2 May 06 '20

Python does handle complex numbers. It's honestly a little bit weird that math.sqrt doesn't handle them. -4 ** 0.5 returns 2j, for example.

1

u/Beall619 May 06 '20

if(int(n)<0)

1

u/TheOneWhoWil sadistic May 06 '20

Shouldn't you get wiser input so the code has at least a use.

1

u/OrionSoul May 06 '20

i've stared at the code for about 5 minutes and can't see what's wrong with it, and now i'm too afraid to ask

4

u/neotecha May 06 '20

What's the definition of "negative"? It's "any number less than zero", right?

Consider this function:

def is_negative(n):
    return n <  0

That's it. Nothing more is needed.

Instead, while the function in the picture works, it goes about the problem in an illogical way. Try-Except blocks should be reserved to error handling, and handling logic like this is a definite code smell

3

u/_mnml May 06 '20

it's a number that has gone through some shit have respect

1

u/crispybacon_NZ May 17 '20

Try-except blocks should be reserved for error handling, but it's more Pythonic: "easier to ask forgiveness than get permission" or something.

Dang near every code review, somebody will argue about this....

1

u/neotecha May 18 '20

I can't agree that it's more pythonic. Relying on a behavior edge case breaks the "Explicit is better than implicit" rule.

Dang near every code review, somebody will argue about this....

I don't disagree

1

u/lambda0101 May 06 '20

that's like being smart in a dumb way.

1

u/Farpafraf May 06 '20

This is clearly written as a joke... does it still count?

1

u/Blurp_06 May 06 '20

What are if statements?

1

u/Yaroster May 06 '20

Oh my god

1

u/TheMsDosNerd Aug 01 '20

>>> a = 1 + 1j - 1j

>>> a == 1

True

>>> isNegative(a)

True

82

u/[deleted] May 05 '20

[removed] — view removed comment

5

u/RFC793 May 05 '20 edited May 05 '20

Sure. But what if you are passed a string with double, triple, etc negatives? Clearly we need something like this:

len(list(itertools.takewhile(lambda c: c=='-', str(n))))%2 == 1

However, in production code, surely you want to replace the modulo with:

def isOdd(n):
    return str(n)[-1] in (1,3,5,7,9)

0

u/[deleted] May 05 '20

[deleted]

49

u/onthefence928 May 05 '20

still negative, just in the emotional way