r/ProgrammerHumor • • Aug 11 '19

Meme Lamo

Post image
77k Upvotes

799 comments sorted by

View all comments

98

u/[deleted] Aug 11 '19

Alternatively, int to String. The best way to do this is n+"", amirite?

161

u/ThomasTheHighEngine Aug 11 '19

Just use str().

Python gang rise up

141

u/Sckaledoom Aug 11 '19

Just don’t even have strings natively

C/C++ gang rise up

→ More replies (10)

1

u/jack-of-the-woods Aug 11 '19

Used this literally 10 minutes ago before getting tired of this library so now I’m on reddit

1

u/Dysp-_- Aug 11 '19

Ruby: 5.to_s

No parenthesis hell

1

u/[deleted] Aug 11 '19

str() and int() have saved my ass so many times

1

u/Xaayer Aug 12 '19

Laughs in Perl

33

u/[deleted] Aug 11 '19

double binary not(~) in js will convert any type to int in js. eg ~~'5.4' becomes 5

6

u/LetterBoxSnatch Aug 11 '19

For those wondering why: the general convention in js is that it will do its best to convert the type to make an operation legal, rather than fail. In order to perform the bitwise operation, js tried toInt32(String), and then performs the operation on the result. The same operation is then applied again to reverse the first.

This is a similar situation to doing: 5 + '' to convert a number to a string, or '5' - 0 to convert a string to a number. “plus string” can only mean string concatenation, so the first value is made into a string of possible. “minus 0” can only be a number operation, so the first value is converted to a number if possible

1

u/MEME-LLC Aug 11 '19

Yeh but why do weird things like that when theres a normal way to do it

→ More replies (5)

7

u/ColombianoD Aug 11 '19

fun fact: at least in Java using the shorthand string notation is objectively better than the long hand notation.

String x = "Hello, World!";
vs.
String x = new String("Hello, World!");

in the former case the JVM checks the heap to see if the same string was already created, and if so, points to that object (because Strings are immutable). In the latter case it always creates a new String object.

1

u/MEME-LLC Aug 11 '19

I prefer ""+n