5 ms·
The first code example he uses immediately threw me for a loop. Why is he adding the return value of a function call to a function itself? bar() + bar???? func
by aerovistae 6y ago
The first code example he uses immediately threw me for a loop. Why is he adding the return value of a function call to a function itself? bar() + bar????
function bar(someNumber: number)
{
return foo(someNumber) + 1;
}
function baz(someNumber: number)
{
return bar(someNumber) + bar;
}
baz();
Moreover, baz is called with its only argument being undefined-- so the exception throwing code won't even throw, because it isn't 0. And _moreover_, this is typescript, so this would have thrown a type error since a required numeric argument is omitted.
This has so many problems. This is not a promising start to this article's correctness or thought-outtedness. Yet here it is at the top of hacker news...
- throwaway35911 6y agoSounds like you posted this comment after only reading that example at the beginning. Did you read the rest of the article? What were your thoughts at the end of it?
- aerovistae 6y agoCorrect. There's about 19 billion opinionated articles on the internet. If I start one off and it seems immediately wrong, I'm not going to take the time to read the entire essay. You have to have some kind of filtering heuristic for content.
- throwaway35911 6y agoYou'll take the time to leave a comment, though. And engage in follow-up discussion, apparently. It's not a long read and it has some useful content (that I mostly agree with) despite having a bunch of minor mistakes. I noticed them too, but instead of loudly dismissing the entire thing on a public forum my first instinct was to look for a repo so I could send a pull request (sadly I didn't find one).
- valand 6y agoAuthor here. Thanks for the correction. You caught me red-handed for not testing these codes in playground.
- ByteJockey 6y agoIn JS it's the function would be converted to a string representation of its code and concatenated to the result of `bar(someNumber)`. Some quick playing with ts-node says that the same thing should happen, depending on the return type of bar. Given the definitions above, it should throw a TypeError, but if bar returned a string, I think it would still behave as I said.
- formerly_proven 6y agoI was unsure if this comment was sarcasm so I tried and concatenating a function and a string in JS actually results in the source of the function being concatenated with the string.
- zamadatix 6y ago<rant> This whole "wow I thought implicit type coercion in JS was a joke" style comment got old 10 years ago. If you look at where JS came from (the 90s browser) and what it tried to do there then you should be left with few surprises looking it JS code snippets. If you don't understand what a language was designed for and how it evolved how can you be surprised at the results of code snippets? Because it's not the same as every other language? What would the point of the new language be then?</rant>
- adchari 6y agoIt’s not that the language is different, it’s that the default is stupid. It’s much more likely that if I’m trying to concatenate a string and a function, I’ve mistyped something, not that I want the source code of the function. You can always include some additional function which takes a function as an argument and prints the source code as a string for the 4 people in the world who rely on function to string coercion, why is it a default behavior with no visibility? I get it, JS is different from C or C++ and every other language but we call out poor design decisions by comparing them to expectations fostered by other languages
- 6y ago
- Stratoscope 6y agoPlus the undefined name 'number' in foo(). TypeScript flags that along with 'baz();'. What does puzzle me is that TS doesn't flag 'return bar(someNumber) + bar;' - I'm curious if anyone knows why it seems to allow that.
- josh64 6y agoI think because the return type hasn't been specified for the function, so it is inferring the return type from the return statement (which means it is "correct"). Don't forget JavaScript (and therefore typescript) will let you concatenate a lot of random types together without error.
- ThA0x2 6y agoThis is what happens when "coders" barely fumble through JavaScript, can barely hack a front-end together, and then self-promote themselves to "full-stack" developers without having a lick of clue on system basics, principles of computer science, application programming, etc. You know, the stuff people go to school for.