• (nodebb)

    I understand that Javascript has all these quirks, but we still use it because it's what all browsers have.

    But i don't get it when people write code like this which actively relies on these weird rules. Why not write sane looking code?

  • (nodebb) in reply to Mr. TA

    I use TypeScript. Mostly...

  • (nodebb) in reply to Mr. TA

    I use TypeScript. Mostly...

  • Rob (unregistered)

    The coalescing operator doesn't return the original input if the calculation fails. It returns the original input if it's falsy (empty, null, undefined), otherwise it returns the result of the second part, even if it returns false. And if it the second part fails with an error, that error is thrown. So basically, the && is there to be safe against empty, null and undefined input.

  • Scragar (unregistered)

    There's a misunderstanding of how the && works, it actually evaluates the first(the passed in date), if that's not falsely it'll return the second(the years diff).

    Basically it just ensures that this returns falsey if there's an empty string/null/undefined passed in, and the number of years otherwise.

    Not the nicest way to do it, I much prefer a more explicit check at the top of the function, but it works and sometimes is handy for cleaning up code(think of it as a ternary that only needs the positive path defined).

    This is especially important because there's a falsely value that date can be that'll look like a successful return value(the number 0), much better to explicitly return null or similar to make the distinction clear to avoid bad behaviour propagating.

  • (nodebb)

    logically, && should return the first falsy value, eg if date is null it would return that right way. to get the last truthy value you would want ||

  • AzureDiamond (unregistered) in reply to Barbaz

    optional chaining does that. foo?.bar short circuits to undefined if foo is null or undefined.

  • erichamion (unregistered) in reply to Barbaz

    || would give the first truthy value, not the last. The && here looks like a null check on the argument value. If you pass a null, undefined, or otherwise falsey value as date, then the function just returns that falsey value back to you, rather than throwing an exception.

  • Ross Presser (unregistered)

    3e10 is 30 billion, not 3 billion.

  • Graculus (unregistered) in reply to Mr. TA

    No you still use Javascript because of Google.

    Javascript is appalling and should have been killed off by now. Thank God we didnt get LISP instead...

  • Mr. B (unregistered) in reply to Mr. TA

    Too many coders, not enough engineers.

    As one who's been on the job market for a little while now, it seems that these days the focus is very much on those who spend all their time a) grinding clever solutions to Leetcode problems and b) blasting out daily GitHub commits. Veteran engineers who actually take time to write good maintainable code based on solid fundamentals seem to be in much lower demand. Not that I'm bitter or anything :P

    On the plus side, this site will have plenty of content for years to come...

  • Nonymous (unregistered) in reply to Mr. B

    Or maybe this site will dry up if none of those coders recognize a WTF when they see it

  • (nodebb)

    The first function is assuming that 1 year is exactly 365.25 days. Which is mostly correct on average, but it'll start to have some odd off-by-errors if your date range is very close to an exact number of years. Then if you go past 1901 or 2099, it'll have even more of an error, but I bet the author doesn't expect this code to still be running in another 75 years.

  • LZ79LRU (unregistered) in reply to Mr. B

    Amen to that. Any monkey can type code. It's as easy as laying bricks. The hard part is making sure the wall is strait, level, in the right place and that it won't fall over the moment someone leans on it. And for that you need us engineers.

  • Argle (unregistered) in reply to LZ79LRU
    Comment held for moderation.
  • (nodebb) in reply to AzureDiamond

    Optional chaining is a relatively recent JS feature. And it's only useful for accessing properties, not for skipping the entire rest of the expression.

    ES6 also has the null coalescing operator ??. The difference from && is that the latter stops at any falsey value (e.g. 0 or ""), while ?? only stops at null or undefined.

  • Denilson (unregistered)

    As much as we can complain about people not being careful enough when coding, or not knowing how to code...

    In this case the problem is the terrible Date primitive that JavaScript provides. It's so bad that countless date/time libraries have been created.

  • Lurk (unregistered) in reply to Denilson

    "In this case the problem is ... JavaScript " FTFY Lurk

  • OldCoder (unregistered) in reply to adamantoise

    It isn't about how long the code might run, it concerns the dates that might get used as input.

    Try working on Pensions systems for a while and you'll soon hit some of those edge cases. (She was born when?)

Leave a comment on “A Double Date”

Log In or post as a guest

Replying to comment #679677:

« Return to Article