Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31295
| From | Scott Sauyet <scott@sauyet.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: How would you prepare for javascripting interview? |
| Date | 2016-09-10 02:14 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <nqvqac$64e$1@dont-email.me> (permalink) |
| References | (8 earlier) <nqhg6o$st6$1@dont-email.me> <nqkl2e$6bt$1@dont-email.me> <nqmpag$kit$1@dont-email.me> <nqo0jf$a4v$2@dont-email.me> <nqpr7i$l7t$1@dont-email.me> |
Doc O'Leary wrote:
> Scott Sauyet wrote:
>> Doc O'Leary wrote:
>>> There is a *reason* why languages developed mechanisms other than
>>> “return a valid value and let the programmer figure out if it’s
>>> *actually* a good result, or just some kind of error”.
>>
>> I've argued (most recently in a response to Gene Wirchenko) that this
>> is not only a good result, but the only reasonable value.
>>
>> It allows us to assert laws such as
>>
>> max([max(list1), max(list2)]) === max(concat(list1, list2))
>
> The purpose of Computer Science is *not* to assert mathematical laws.
> It is to do useful work.
So if we're going to write functions to be used in a mathematical
context, we should write functions that are mathematically lucid.
Mathematical laws like the one above make it *easier* to write useful
code. Imagine you have a list of temperatures with timestamps and have
to display the highs for each month and for the year. You partition the
readings by month, pluck the temperatures for each, find the maxima, and
then you can simply take the maximum of that list to to find the annual
high. It's more efficient, and it's even possible that the part of the
system that takes the annual maximum only knows these monthly maxima, not
the raw data.
What makes this possible? The law above.
If instead, you were to return `null` on an empty list, what happens when
it turns out that the sensor was down for all of April and no data was
recorded? April's value is now `null`, and the annual value is `null` or
`NaN` or whatever you choose to return in this case. If you choose to
throw, it's even worse. You're trying to get an annual value, from data
which is perfectly legitimate, but you're implementation is causing you
to throw an exception.
> It is *wrong* to return a result *as if*
> useful work was done when it is highly likely such a result is not only
> in error, but will likely propagate that error.
How would you define the maximum function? I would choose this: "max
returns the smallest value that is no smaller than any element in the
list." That seems succinct, clear and covers all cases (for Numbers, at
least.) ISTM that this works for lists of any size, including empty ones.
>>> It’s best dealt with abstractly, because the concept applies to
>>> things other than numbers. If you can order it, you can determine a
>>> “max” value. And if there is *no* values to order, it is ludicrous
>>> to say the correct result is a valid value you have simply
>>> fabricated.
>>
>> This is no more fabricated than 0 is fabricated as the sum of an empty
>> list of numbers, than 1 is fabricated as the product of an empty list
>> of numbers, or than `true` is fabricated as the result of a call to
>> `all` on an empty list.
>
> They are all fabricated. Done so to reflect their respective identity
> element, sure, but all of them are equally *wrong* behavior from a CS
> perspective.
So it seems reasonable to you that
sum(filter(isOdd, ints)) + sum(filter(isEven, ints))
could have different behavior than
sum(ints)
knowing that `ints` is a non-empty list of integers, and every one
matches either `isEven` or `isOdd`? To me, that's ludicrous.
>> These are the obvious and useful results.
>
> Internally, yes, they are useful in the way that many other hacks are
> useful. But it is not the sign of a competent developer to architect a
> library that *always* returns valid results like that.
Sure, let's all sporadically returns some invalid results. We like to
keep our users on their toes, right? WTF?
> Pull your head out of your math book and actually *think* about the work
> you’re trying to do.
Well, if I'm writing mathematical functions, a math book seems a really
good place to start. And while I do have formal training in mathematics,
this has very little to do with that. It's just common sense.
>> You clipped the bit where I mentioned custom types and how since the
>> user would already have to do the ordering, asking her to also signal
>> the proper behavior here is not unreasonable.
>
> It is in a polymorphic sense. Or do you expect max() to be implemented
> to return -Infinity by *all* developers for all objects? Again, I
> suspect you’ve never really architected a system of substantial
> complexity.
That's funny; that's what I'm feeling about you at this point. I've
worked on small, medium, and large systems. Nothing the size of GMail,
say, but my last project included a custom web framework (similar in
scope to Angular but designed quite a bit differently) a custom business
rules engine, a large utility library, a powerful global event bus, and,
of course, lots of business logic, in about 200K lines of JS. I served
as lead developer and de facto front-end architect as well as coach and
mentor to a large team of mixed-level developers.
I've been writing Javascript since 1998, and it's been my main focus
since 2008. I have one major open source library to my name as well as
many smaller contributions to others.
In other words, I've been around. And yet, you seem to feel that you
know my skill level based on a disagreement about one function, one which
a number of other smart people on this thread are suggesting similar
things as me. That makes me wonder if you're just trolling, or if you
simply don't understand software design very well.
>> I'm suggesting that those who define the types to be used with a
>> generic `max`/`min` would have to define their own boundary values, or
>> signal that they don't exist, not that such a function could do it for
>> an arbitrary type. But I also insist that ±Infinity is the only
>> reasonable choice for Javascript's Number type.
>
> And that kind of thinking is why a lot of people see JavaScript as
> amateur hour. We don’t need some overwrought system to allow you to
> keep doing bad things out of a sense of mathematical purity. All we
> need is a standard that says which operations need 0, 1, 2, or more
> arguments to produce behavior that makes (common) sense, and what the
> error behavior is when those pre-conditions aren’t met.
Javascript *is* amateur hour if you're coming from Haskell, LISP, OCaml,
or some well-designed language. If you don't understand that, you don't
really understand programming. That does not mean we should avoid doing
the best we can.
But we always have to choose our priorities. For a function called
`Math.max`, I would always choose mathematical correctness over possible
abstraction to other types. If I wanted to use the same implementation
for other types I would proceed as above. If not, there would have to be
very serious design decisions. It's quite reasonable for those creating
types to *want* analagous behavior to `max([]); //=> -Infinity`; we'd
have to decide if we wanted to support that when desired or to insist
that we'd have to throw. And of course, all of this is contingent on
being able to determine some sort of context; if we have a single `max`
function that's supposed to infer the type, then we can of course know
nothing about an empty list. Of course we have similar issues with `max
([1, 'a', true, new Date(), {}])`. These are the joys of working in JS,
I'm afraid.
>> This is simply the result of my suggestion that this should be left to
>> those implementing ordering for the types. I don't know better than
>> they do; neither do you.
>
> It seems I do know better, though. And, worse, I happen to know from
> *experience* that your approach of just letting everyone do what they
> want willy-nilly results in exactly what I said: an utter mess.
Are you going to even let them determine how to order their elements? Or
do you think you know better than them about that too?
This is nonsense. This is part of API design, making choices.
You seem to be saying that this is perfectly reasonable:
Ord a :: {
lt :: (a, a) -> Bool,
eq :: (a, a) -> Bool
}
But one that offers additional optional elements of `bottom :: a` and
`top :: a` is ridiculous. I simply don't see why.
>>>> It's not that I think this is a bad point. I simply don't believe
>>>> it's as clear-cut as described.
>>>
>>> It is. Perhaps one day you’ll be experienced enough to understand
>>> why.
>>
>> It is not. Perhaps one day you'll understand enough math to recognize
>> why.
>
> My math is fine. It’s your computer science that isn’t up to snuff.
Nope, try again.
>> To me `null` or `undefined` would be true garbage. Instead of a
>> function whose results I can simply use, I would get something that
>> then requires type-checking, and then manual intervention if I get the
>> wrong type. If this throws, I cannot compose it with other functions,
>> and need to build complex logic flows rather than simple functional
>> pipelines.
>
> Your (local) simplicity can be maintained if you raise exceptions.
> The fact is that unexpected behavior and errors need to be accounted
> for. You can’t just fall back to mathematical purity and say “Well,
> you’re going to have to live with the results that came back because it
> satisfies a proof someone made 400 years ago.” Everything more complex
> than that comes at a cost.
I think you're missing the point.
Badly.
-Infinity is a number. It's a valid number that can be used in
comparisons, in further `max` calls, even in much arithmetic. Granted,
it might not be the best for final displays, but as these should always
be pushed to the edges of your program, it's pretty easy to handle these
in a single place.
A `null` on the other hand really is action at a distance, and it's very
hard to know the original cause of one.
A raised exception will work, but it adds substantial complexity to a
program. They should be used for exceptional circumstances, not for
normal operations. In my `filter(isOdd)` example above, it is not
exceptional for a list of integers to have no odd elements. Using an
exception to handle such a case, or adding coding gymnastics to avoid
getting into such a condition, is adding complexity for very little gain.
>> I have no fundamental issue with the thought of instead offering a
>> `Maybe Number` type (or more generally a `Maybe a` one) for an API, but
>> this whole discussion is in the context of a function already
>> documented to return a number.
>
> Only because JavaScript defines it solely in Math.
Ahh, so you finally realized the context of this discussion?!
> Many other languages define similar operations more abstractly on
> collections.
Yes, they do. Now, if I were to interview you for a job, and were to
say, "This is something of a trick question. Most people find it a
surprising fact that Math.max() < Math.min(). Can think of why it might
be so?" and then started to guide you through it, would you try to answer
my question and engage in the problem at hand, or would you try to
convince me that TC39 or Brendan Eich or whoever had fallen asleep on the
job by not trying to make this a more generic function? Because, believe
me, while I might find that discussion interesting, I would also notice
that you don't seem able to work on the problem assigned.
> I encourage you to expand your thinking beyond mathematics to the real
> world uses of these things. Talk to some actual humans about what
> *they* would say is the maximum value of 3, 2, 1, and even 0 items. I
> would wager a large amount of money that they’ll all agree to what the
> result should be down to 1 item, and that essentially *nobody* would
> offer up -Infinity as the result for 0 items.
It's come up on this thread, and I work with a number of functional
programmers for whom this is totally natural... for numbers. You're
trying to abstract in a way that I find interesting, but not relevant to
a discussion of the function `Math.max`.
>> If you have a solution for it, do you always throw on an empty list, do
>> you always return a `null`/`undefined` signal, or do you leave it up
>> the type to decide?
>
> That’s just it: for an empty list, abstractly, there is no “type” of the
> nothing it contains. The behavior you choose has to be consistent with
> all kinds of objects. So, sure, it might make the most sense to raise
> an exception, even for functions like sum() that might otherwise
> commonly return a 0 value for numbers.
That is only a concern if you are writing a generic `max` function. I'd
actually be curious to see one you wrote. The more I think about it, the
less trivial it seems. You'd need to write a type system that determined
the least common supertype of pairs of elements; you'd have to ensure
that the ordering for subtypes is consistent with that of their parents,
or only allow lists containing the same exact type, which sounds very
restrictive (why couldn't you have a square in a list of rectangles?);
you'd have to enforce rules such as `a < b && b < a; //=> a ~= b`.
It sounds like a big job. Do you already have an implementation at
hand? If not, do you think this is a simple task?
>> I don't know what sort of position you hold. I work in a large company
>> with an ongoing mix of old and new software for developers to work on.
>> A very important skill is trying to figure out how a system or a
>> component works, and why it's written the way it is. Watching a
>> candidate go through this exercise, I believe, helps me determine how
>> skilled she is at this.
>
> Not really. Again, you stop with the hack itself when the *real*
> demonstration of CS skill is everything we’re discussing here. It is
> trivially easy to discover and put up with bad code, because bad code is
> everywhere. That is not a rare skill you need to select for. The
> places I aim to work for are the ones that appreciate the need to get to
> solutions that are *better* than existing ones.
If you cannot discover how something works, I don't want you on my team.
Even if you could design something better from scratch, you're no good to
me if you can't go in and understand what others have done in the system.
That doesn't mean that I don't want to know about how much better you can
do. But if you can't understand existing code, I probably won't trust
your designs anyway, because I have no idea if you really understand the
requirements and the constraints.
-- Scott
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How would you prepare for javascripting interview? justaguy <lichunshen84@gmail.com> - 2016-07-18 09:10 -0700
Re: How would you prepare for javascripting interview? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2016-07-18 18:37 +0200
Re: How would you prepare for javascripting interview? justaguy <lichunshen84@gmail.com> - 2016-07-18 11:09 -0700
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-07-21 01:04 +0000
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-07-21 14:24 +0100
Re: How would you prepare for javascripting interview? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-23 16:25 +0200
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-07-23 15:56 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-03 19:39 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-04 15:56 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-05 00:06 +0100
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-05 14:49 +0100
Re: How would you prepare for javascripting interview? $Bill <news@todbe.com> - 2016-09-05 10:01 -0700
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-06 09:36 -0700
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-05 15:05 +0100
Re: How would you prepare for javascripting interview? Andreas Bergmaier <andber93@web.de> - 2016-09-05 20:31 +0200
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-06 15:15 +0000
Re: How would you prepare for javascripting interview? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-09-06 19:40 +0200
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-06 19:25 +0100
Re: How would you prepare for javascripting interview? Andreas Bergmaier <andber93@web.de> - 2016-09-06 22:16 +0200
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-07 17:17 +0100
Re: How would you prepare for javascripting interview? Andreas Bergmaier <andber93@web.de> - 2016-09-07 20:07 +0200
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-08 14:36 +0100
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-05 18:46 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-05 20:32 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-05 17:27 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-05 20:12 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-06 16:48 +0000
Re: How would you prepare for javascripting interview? Andreas Bergmaier <andber93@web.de> - 2016-09-06 19:56 +0200
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-07 20:16 +0000
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-07 10:01 -0700
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-07 20:21 +0000
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-09 11:41 -0700
Re: How would you prepare for javascripting interview? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-09-09 15:35 -0700
Re: How would you prepare for javascripting interview? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-09-09 20:40 -0700
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-10 15:12 +0000
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-12 11:14 -0700
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-13 21:07 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-06 16:20 +0000
Re: How would you prepare for javascripting interview? Ken Tilton <kentilton@gmail.com> - 2016-09-05 11:37 -0700
Re: How would you prepare for javascripting interview? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-09-06 19:26 +0200
Re: How would you prepare for javascripting interview? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-09-08 12:41 -0700
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-05 20:37 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-05 22:13 +0100
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-06 02:33 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-06 16:02 +0000
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-06 09:50 -0700
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-07 01:36 +0000
Re: How would you prepare for javascripting interview? Gene Wirchenko <genew@telus.net> - 2016-09-07 10:07 -0700
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-10 00:03 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-10 00:12 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-10 15:34 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-11 01:32 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-12 19:26 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-13 01:02 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-13 21:38 +0000
Re: How would you prepare for javascripting interview? "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-09-14 00:56 +0200
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-15 15:09 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-14 00:47 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-15 15:34 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-11 02:45 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-12 19:10 +0000
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-12 21:06 +0100
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-07 03:12 +0000
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-07 19:53 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-10 02:14 +0000
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-10 11:25 +0100
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-10 15:49 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-10 18:39 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-11 02:45 +0000
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-11 16:23 +0100
Re: How would you prepare for javascripting interview? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-12 20:32 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-12 18:49 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-13 00:27 +0000
Re: How would you prepare for javascripting interview? Tim Streater <timstreater@greenbee.net> - 2016-09-13 08:51 +0100
Re: How would you prepare for javascripting interview? Jon Ribbens <jon+usenet@unequivocal.eu> - 2016-09-13 12:33 +0000
Re: How would you prepare for javascripting interview? Scott Sauyet <scott@sauyet.com> - 2016-09-14 01:17 +0000
Re: How would you prepare for javascripting interview? Tim Streater <timstreater@greenbee.net> - 2016-09-14 11:15 +0100
Re: How would you prepare for javascripting interview? Doc O'Leary <droleary@2015usenet1.subsume.com> - 2016-09-15 15:02 +0000
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-13 10:06 +0100
Re: How would you prepare for javascripting interview? John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-06 19:53 +0100
Re: How would you prepare for javascripting interview? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-18 22:30 +0200
Re: How would you prepare for javascripting interview? justaguy <lichunshen84@gmail.com> - 2016-07-18 14:56 -0700
Re: How would you prepare for javascripting interview? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-07-19 16:01 -0300
Re: How would you prepare for javascripting interview? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-07-19 16:16 -0300
csiph-web