HNHacker News
TopNewBestAskShowJobs

ivquatch4

11 karma · joined April 2, 2013

submissionscomments
ivquatch4··on Explicit vs. Clever
The problem is that you have to run the code in your head to understand what it means. It obfuscates the intent. Just read it aloud and you'll see what I mean,

"Once upon a time there was a variable named, totals that was initialized to an empty array. Next, a for loop happened and some var, i appeared (his name doesn't really matter.) i was initialized to 0. Enter stage right, totals's buddy, orders. orders has a property named length. i was told that it shall be incremented by 1 so long as i was less than order's length property. Sadly, the for loop was just using i. i didn't know it would be disregarded once for loop was finished.

Not surprisingly, what happened next was quite remarkable. The total property of orders index i lept onto totals. i was incremented. Then the total property at index i lept onto totals... i was incremented... Finally i was greater than or equal to orders.length, so the for loop ended. totals lived on, changed by its journey while i was never seen or heard from again.

Study guide question: how did totals's character change by the end of the story and what did this mean?" "

The following code snippet means the same thing, but it's also self-documenting. Plus there's less accidental complexity:

    var totals = orders.map(function(order){ 
        return order.total;
    }); 
Read it aloud and you'll see what I mean,

"Once upon a time, there was a var named, totals. It was a list of order totals. Fin."