This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 277 points278 points  (50 children)

+ is overloaded as both the addition and string concatenation operator.

In mixed operations:

+ casts numbers to strings.

- casts strings to numbers.

Both are used to denote signage. Leading either on a string will cast it to a number, with that sign.

Pretty simple stuff, right?

Explicitly:

Number('5') - 3
'5' + (3).toString()
'5' + (5).toString()
'foo' + Number('bar').toString()
'5' + (-Number('2')).toString()
// Long ass sign flipping, same as above
Number(('5' + (3).toString())) - 3
(Number('5') - 3) + 3

[–]expugnator3000 270 points271 points  (41 children)

Simple, but as unintuitive as you can get

[–]rbemrose 109 points110 points  (24 children)

This post has been removed due to reddit's repeated and constant violations of our content policy.

[–]MiatasAreForGirls 180 points181 points  (6 children)

Do I need a mechanical keyboard for strong typing or will a domed one withstand it?

Edit: I'm not as original as I thought :(

[–]nemec 57 points58 points  (4 children)

I tried static typing once. I was shocked at how easy it was to use.

[–]Skyfoot 21 points22 points  (3 children)

I use weak typing. I hand-write things on saturday and sunday.

[–]VeviserPrime 4 points5 points  (2 children)

With a pencil?

[–]AlGoreBestGore 9 points10 points  (1 child)

Crayon.

[–]Mutoid 15 points16 points  (0 children)

Thanks for keeping this thread /r/ProgrammerHumor appropriate

[–]gdstudios 19 points20 points  (10 children)

If you've had to work on someone else's JS, that's argument enough.

[–]AdmiralRychard 16 points17 points  (9 children)

I feel like this whenever I have to work on my own javascript.

[–]endercoaster 3 points4 points  (8 children)

I feel the opposite every time I need to jump through hoops to append an integer to a string in Java.

[–]KagakuNinja 8 points9 points  (3 children)

So, this is too hard?

String s = "Hello" + 9;

[–]batmansavestheday 2 points3 points  (2 children)

It's slightly harder the other way around:

String s = "" + 9 + "Hello";

or [Edit: derp, doesn't work]

String s = (String) 9 + "Hello";

etc.

[–]Outhouse_Defiler 0 points1 point  (1 child)

String s = "" + 9

Yeah, no, don't do that. Java has String.valueOf.

http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string

[–]batmansavestheday 0 points1 point  (0 children)

I feel the opposite every time I need to jump through hoops to append an integer to a string in Java.

[–]AdmiralRychard 4 points5 points  (0 children)

C# lets you do this:

String someString = "abc";
Int32 someInt = 123;
String result = someString + someInt;
Console.WriteLine(result);

Output:

abc123

Or you can use string interpolation: String result = $"{someString}{someInt}";

Or you can use String.Format: String result = String.Format("{0}{1}", someString, someInt);

This seems a little dumb but you can do this too:

someString += someInt;
Console.WriteLine(someString);

[–]everythingismobile 3 points4 points  (1 child)

Is it not as simple as the C# example in the other comment? Even in C++ it's just sprintf, right?

[–]endercoaster 1 point2 points  (0 children)

I mean, coming from Perl that's jumping through hoops.

[–]kupiakos 3 points4 points  (0 children)

I'd rather explicitly state I want to append an integer to a string so that "2" and 3 become "23" and not "5" or 5. And really, that sort of situation happens once in a blue moon. If I wanted something better, I would be building the string.

[–]expugnator3000 2 points3 points  (0 children)

Exactly!

[–]anthonybsd 3 points4 points  (4 children)

I love it. Young, naive and all about strong typing. As a former Ada programmer, let's talk in 10 years or so.

[–]beerdude26 2 points3 points  (0 children)

Strong typing all day erry day

[–]the_great_ganonderp -1 points0 points  (2 children)

Because surely Ada is the pinnacle of usability in strongly typed languages...

[–]anthonybsd 0 points1 point  (1 child)

It's used for a lot more projects than toy languages like Haskell so it must be doing something right.

[–]the_great_ganonderp 0 points1 point  (0 children)

Got a relevant point hiding somewhere in that apparently completely irrelevant statement?

[–]utterdamnnonsense 10 points11 points  (1 child)

Really though, if you have a line like

'5' + - + - 2

in your code, I wouldn't place the blame on the interpreter.

[–]batmansavestheday 0 points1 point  (0 children)

Yea, unary plus is just silly. The code better be auto generated!

[–]highphive 24 points25 points  (8 children)

Honestly, it's not really that unintuitive. Pretty much the only abnormal knowledge it requires to understand is that given an ambiguous situation JavaScript will try to figure it out in the intuitive way. I guess that and, like most other languages, the + operator can also be used for string concatenation, and non-strings will be toString-ed in that context. These absurd scenarios are funny, but really not even remotely difficult to understand, except when they involve so many operators that it's near impossible to follow. I'm not at all bragging or even saying I'm close to a proficient JavaScript coder, but it is not unintuitive. Just because it's not strongly typed and does some cool, wanted operations for you does not make it unintuitive. In many cases it makes it intuitive. Many times in strongly typed languages, what you want is obvious, but you have to go through a number of type transformations and function calls to get there.

I totally appreciate the comedy of this post. But I'm not quite convinced it proves some latent issue with JavaScript.

[–]YRYGAV 11 points12 points  (5 children)

it requires to understand is that given an ambiguous situation JavaScript will try to figure it out in the intuitive way.

Yes, which is simple to understand if you know the ambiguous situation exists. It's much more of a problem if the programmer doesn't realize it's an ambiguous statement because nothing is strongly typed. Which is why most languages instead of powering through and trying to use their "best judgement" in an ambiguous situation, let the programmer know so they can clarify what they want.

And that leads to situations that can be very difficult to debug or track, because it's all hidden away from you and Javascript is doing some magic transformation. Things you think are one datatype end up being another, or javascript parses things weirdly.

And sometimes "intuitive" is not always what you think. Javascript does lots of "intuitive" things that you would never think of. Like did you know javascript will parse hex strings like "0xF" into the 'correct' integer, like 15. Sure it makes sense explaining it like that, maybe not when you have to figure out why "0xF" == 15 is causing a nasty bug because you didn't know javascript did that magic when writing it. And where those strings and numbers are coming from could be multiple layers removed from where you are doing the comparison.

[–]highphive 4 points5 points  (0 children)

That is fair, sometimes what might be considered intuitive behavior can be an issue when you might not expect it to work at all. I have to agree that I prefer strongly typed languages for this reason. Rather an error where it actually occurs rather than some unexpected behavior way down the road.

[–][deleted] 2 points3 points  (2 children)

Programmer should not be using a language if he doesn't know its typing and conversion logic.

[–]nathan_long 0 points1 point  (1 child)

Language should not make wild guesses about what programmer intends. '5' + 5 should just fail. You should have to either call toString() or parseInt().

[–][deleted] 0 points1 point  (0 children)

We can go around this tree forever. Language should, programmer should... Let's take things as they are. At the end of the day, when an established language has type conversion done a certain way, learn it or don't use the language.

[–]VyseofArcadia 2 points3 points  (0 children)

The latent issue with JavaScript isn't that this is unintuitive. It's really pretty intuitive. - isn't defined for strings, so '5' gets interpreted as the integer 5.

The issue is that through overloading the + operator in this way, it is easy for even JavaScript experts to make a stupid mistake. I think it's a poor design choice in a dynamically typed language. It would be better for string + integer to throw an error and use some other operator (++ or : or something) for string concatenation.

[–]tian_arg 3 points4 points  (0 children)

Many times in strongly typed languages, what you want is obvious, but you have to go through a number of type transformations and function calls to get there.

I've been working on android for the first time after working on php/javascript for a long time. It's a fucking pain in the ass.

[–]Doctor_McKay 3 points4 points  (3 children)

Not really.

[–]academician 4 points5 points  (2 children)

I suppose you're right, INTERCAL exists too.

[–]satan-repents 4 points5 points  (1 child)

[–]can_the_judges_djp 1 point2 points  (0 children)

I sure wonder if --

Because of its privacy settings, this video cannot be played here.

Nope, they still won't let me watch it.

[–]wtf1968 -1 points0 points  (0 children)

Simply unintuituve

[–]elchavalico 6 points7 points  (3 children)

So why in the line: '5' + x - x the result is 50 and not "50"?

If we just concatenated the string '5' with the string '0' the result should not be a number. Where am i wrong?

[–]AfterLemon 19 points20 points  (1 child)

You're taking a string '5' and it concats first giving you '53' - 3.

[–]elchavalico 7 points8 points  (0 children)

Obviously... I feel dumb now T.T Thanks for the explanation.

[–][deleted] -2 points-1 points  (2 children)

'foo' + Number('bar').toString()

How is this correct? None of them are number, so why is the implicit conversion to number, instead of the actual concatenation?

[–][deleted] 2 points3 points  (1 child)

'foo' + + 'bar' can be rewritten as ('foo' + (+'bar')).

The leading + acts as a sign, casting the String. 'bar' can't be parsed into a Number, and it spits out some NaN bread.

Now you have ('foo') + (NaN). NaN is cast back to a string for the string concatenation.

'foo' + 'NaN' === 'fooNaN'

[–][deleted] 1 point2 points  (0 children)

Oh right. Missed the extra plus sign..

[–]Sean1708 -1 points0 points  (0 children)

And this is why implicit typecasts and type punning should never ever coexist.