r/dailyprogrammer • 1 3 • Sep 09 '14

[Weekly #10] The Future

Weekly Topic:

Read enough blogs or forums and you can see the future. What trends or topics are coming down the line? Is it a new language? New design? New way to engineer software?

Last Week:

[Weekly #9] (http://www.reddit.com/r/dailyprogrammer/comments/2f6mo2/weekly_9_binary_trees/)

54 Upvotes

45 comments sorted by

View all comments

Show parent comment

2

u/lord_braleigh Sep 09 '14 edited Sep 09 '14

Actually, requiring that the "+" operator can be used isn't even sufficient to ensure that the program does what you want!

Try the foo() example out in your JavaScript console:

> function foo(x) { return x + 5; }
undefined
> foo(1)
6
> foo("1")
"15"

How often do you think programmers mix up 1 the int with "1" the string? Especially when scraping these numbers from webpages, where everything is a string?

3

u/bcgoss Sep 09 '14

Thanks, I had forgotten that case! I would point out that 1+5 = 6, not 8, if I understand your code correctly.

3

u/[deleted] Sep 10 '14

Also here:

js> function bar(x) { return 5 - x; }
js> bar(5)
0
js> bar(10)
-5
js> bar(2.3)
2.7
js> bar('wat')
NaN