Live data from Hacker News

Why ++[[]][+[]]+[+[]] = 10 in JavaScript

stackoverflow.com

41–50 of 80 posts

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#41

From wtfjs.com: ",,," == Array((null,'cool',false,NaN,4)); // true

Clever, but not as surprising... (null,'cool',false,NaN,4) evaluates to the last expression - 4 Array(4) creates an array with 4 elements. Array(4).toString() = ",,,"

You describe that as "not surprising"? Yikes.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#42

This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.

I am not a PL expert, but is this a strong vs. weak type problem or an implicit conversion problem? My layman’s (simplistic) model is that “weakly typed” languages allow entities to change their type at runtime, strongly typed languages do not. But what we have here is an expression where entities are being implicitly converted into intermediate values, along with operators that do different things based on the value…

Strong typing: A type system that I like and feel comfortable with Weak typing: A type system that worries me, or makes me feel uncomfortable

http://blog.steveklabnik.com/posts/2010-07-17-what-to-know-b...

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#43
post #23

Earlier quoted context omitted.

No they will, in every horribly thought out interview that someone who thinks they are smart writes in on a white board and asks applicants to solve it. As if these trick questions some how give insight into ones ability to solve real world problems. These are exactly the kind of obscure fringes of a languadge that an interviewer with an ego loves. When they reality is whether one can solve it or not, amounts to a hi…

Great, then you know who not to work for. I agree that 'magic code' and obfuscation is pretty much ego stroking, but this doesn't seem like a good argument against the whole idea of using weakly typed languages.

No I was not arguing against weak typing, just taking the opportunity to highlight how these kind of questions are used. I have no issue with weak typing. I don't get too dogmatic about such viewpoints. My comment was rather an attempt to highlight where a lot of this type of trivia knowledge is used and misapplied.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#44
post #32
post #24

Earlier quoted context omitted.

I have been in the industry for 16 years and have some pretty big code bases, in a variety of languages, under my belt (not trying to brag just setting the back-story). In thinking back, I cannot recall a single instance where any member of my team was bitten by an accidental conversion or a type safety issue. I have been fortunate to work with some good people, but some of them where average in their ability to actu…

I spent much of a week tracking down a bug in a Clojure program where primitive false was being sent through a network, and read back in as boxed false. It looked right when printed out, yet somehow the wrong branch of an if-statement was being taken. This was in a program under 2000 lines, in my first semester of college, before I had ever held a full-time programming job. Most people are not trained to notice these…

Sure my case is anecdotal at best, and maybe I have just been lucky, but my experience has been that implementing UI's with JavaScript is as fast of a development cycle as doing it in Java or C#. In fact the UI space is dominated by languages such as JavaScript and Objective-C where type safety is loose. I don't believe that fact means that they are superior, but they are as productive as strong typing for UI development in my experience. That being said, I do prefer strong typed languages for middleware and server development, some will differ on that, but it is what I am comfortable with and it works for me.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#46

This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.

No it's not. This is an argument against poor management, not any particular programming language feature. If you have someone writing code like this in production you have a management problem. Review code, use and create clearly defined code standards, use validators, use "strict" mode, and most of all use your brain. A bad craftsman always blames their tools.

Definitely agree. Unfortunately the tools sometime make it easier to shoot yourself in the foot. No matter how smart you are.

And sometime smart people tend to think they're invincible/bullet-proof. Not too many people have high-level discipline. And those that have high-level discipline tend to be shunned by "hackers" because "hackers" hate "processes". They just want to write fun code.

Hence the circle of evil.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#47

Earlier quoted context omitted.

Clever, but not as surprising... (null,'cool',false,NaN,4) evaluates to the last expression - 4 Array(4) creates an array with 4 elements. Array(4).toString() = ",,,"

You describe that as "not surprising"? Yikes.

You're surprised... why? You were expecting Python semantics?

Unsurprisingly, JavaScript is its own language.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#49
post #44
post #32

Earlier quoted context omitted.

I spent much of a week tracking down a bug in a Clojure program where primitive false was being sent through a network, and read back in as boxed false. It looked right when printed out, yet somehow the wrong branch of an if-statement was being taken. This was in a program under 2000 lines, in my first semester of college, before I had ever held a full-time programming job. Most people are not trained to notice these…

Sure my case is anecdotal at best, and maybe I have just been lucky, but my experience has been that implementing UI's with JavaScript is as fast of a development cycle as doing it in Java or C#. In fact the UI space is dominated by languages such as JavaScript and Objective-C where type safety is loose. I don't believe that fact means that they are superior, but they are as productive as strong typing for UI develop…

I think a lot of the hate against statically-typed languages is because people associate them with languages like C# and Java (as opposed to e.g.: OCaml), which lack not only many of the higher-order functional features that we've come to expect in both static and dynamic languages, but also the soundness guarantees that come from actual type-safety.

If you're more productive in JavaScript, it's probably not because you can write a function that converts either an string or an integer to an array2. More likely, it's because you don't need to manufacture a new class every time you want a one-line handler.

Post reply on HN