- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
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?
Edit Admin
I use TypeScript. Mostly...
Edit Admin
I use TypeScript. Mostly...
Admin
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.
Admin
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.
Edit Admin
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 ||
Admin
optional chaining does that.
foo?.bar
short circuits to undefined if foo is null or undefined.Admin
|| 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.Admin
3e10 is 30 billion, not 3 billion.
Admin
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...
Admin
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...
Admin
Or maybe this site will dry up if none of those coders recognize a WTF when they see it
Edit Admin
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.
Admin
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.
Edit Admin
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.Admin
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.
Admin
"In this case the problem is ... JavaScript " FTFY Lurk
Admin
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?)