r/ProgrammerHumor • • Jun 21 '18

Thanks Brendan for giving us the Javascript

Post image
3.1k Upvotes

302 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

4

u/Kered13 Jun 21 '18

Would you expect sum() to return undefined, throw an error, or return 0 (the empty sum)? Would you expect product() to return undefined, throw an error, or return 1 (the empty product)?

In both cases I would prefer the latter. This allows me to call them with empty lists and get sensible outputs. If an empty list is not valid for my use case I can check that myself, but for many use cases it is perfectly valid. max() and min() are no different. -Infinity is the empty max, and Infinity is the empty min.

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

1

u/Kered13 Jun 21 '18

We're not calling with an empty list. We're not giving any arguments.

I don't know much Javascript, but I'm assuming it's a varargs function, which is basically a list.

In case of minimum, one could argue that Inf is not a number so should never be shown, though it is an element of the floats, which means that it should be shown (Formally, a minimum must be within the set).

Since we're working in floats Infinity is most definitely an element, and an ordered one at that. Now if you called this with a NaN element I don't know what would happen and I don't know what would be reasonable. Throwing an exception would probably be best, but I could also see just ignoring the NaN. NaNs are weird.

I think the python implementation is better, as it allows empty sets when a default value is manually set.

This is because the Python min() and max() functions are designed to work with any type (that has comparison implemented), which means it cannot assume a minimum or maximum value for the type. Indeed there may be none at all (there is no maximum string, for example). Therefore the Python functions must take a default value. The Javascript functions only work on floats though, which have clearly defined minimum and maximum values, so there is no need for a default.

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

2

u/Kered13 Jun 21 '18

For a function like max() or sum() I disagree. Note that Python also has both vargs and list versions of these functions. max(3, 1, 2) and max([3, 1, 2]) are the same in Python, and so are max() and max([]) (in Python both of these return an error because there's no default, but the point is that they are the same thing).

4

u/inabahare Jun 21 '18

So you're telling me that functions that determaine the minimum and maximum values of two numbers return something weird when you don't give it any numbers? Really makes you think.

(Also btw, if one or more of the parameters aren't numbers you get NaN, which makes pretty good sense tbh)

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

-1

u/[deleted] Jun 21 '18 edited Jun 21 '18

Math.max() has nothing to do with the type system. It's an overloaded function that's clearly described in the docs. It should output negative Infinity (a number) because that's what the function docs say it will do if it is called without a number.

If you're dicking around with type coercion, you've probably made a mistake somewhere.