Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: How would you prepare for javascripting interview? Date: Mon, 05 Sep 2016 22:13:18 +0100 Organization: A noiseless patient Spider Lines: 40 Message-ID: <87h99uxd75.fsf@bsb.me.uk> References: <50e38667-93da-4c5d-bc02-a78394f0ca3b@googlegroups.com> <1875389.irdbgypaU6@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="14001"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CQgQ7Px5gJLYY4e3tjdq7ZTIqdhdvdHo=" Cancel-Lock: sha1:cAVrtfZmrNhN5IY+YaNjo4RSPhs= sha1:Kk2X88hXOSMXqkRXaIE+VqpbAiE= X-BSB-Auth: 1.24bd822e96643c112b08.20160905221318BST.87h99uxd75.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:31246 Scott Sauyet writes: > Doc O'Leary wrote: >> Scott Sauyet wrote: > >>> Remember that the context was the functions `Math.max` and `Math.min`, >>> so a number-centric implementation makes perfect sense. >> >> No, it doesn’t. It makes *zero* sense for either function to return a >> number that wasn’t in the input set. That’s just a bad decision in the >> language’s design. > > I disagree. The realistic choices are > > (A) Return something of the correct type, even though it's not > in the (empty) input list. > > (B) Return some semaphore of a different type that signals that > things have gone wrong: `undefined` or `null` are most likely. > > (C) Throw an error. > > None of these choices is ideal, but we have to choose between them. I'd > say that the best ordering is A > C > B. I'd also distinguish a B/A option which is to return a value of a type derived from that of the list (or arguments). It's the ML/Haskell way[1] -- you return a 'Maybe Number' object that is either 'Nothing' or 'Just m' (where m is the number found). I realise you may have drawn up your list specifically in the context of ECMAScript where such a type is much less handy (though we now have destructuring bindings), but I thought I'd throw it into the mix anyway. [1] Though it's not used, as it happens, for 'maximum' in Haskell which opts for your (C). -- Ben.