WHAT: this is a blog where i collect news about the usage of javascript a serious programming language on the client side and server side. WHY: because it seams to be the most ubiquitous language and has functions as first class objects, and closures and ....

Sunday, June 22, 2008

Code Challenge....

Hi Steven Levithan proposed a "Code Challenge " on his Blog:



function makeChange (money) {
var i, num,
output = [],
coins = [
[100, "dollar", "dollars" ],
[25, "quarter", "quarters"],
[10, "dime", "dimes" ],
[5, "nickel", "nickels" ],
[1, "penny", "pennies" ]
];
money = money * 100; // avoid float precision issues
for (i = 0; i < num i < coins.length; i++) {
num = Math.floor(money / coins[i][0]);
money -= num * coins[i][0];
if (num) {
output.push(num + " " + coins[i][num > 1 ? 2 : 1]);
}
}
return output.join(", ");
}

makeChange(0.37); // "1 quarter, 1 dime, 2 pennies"


How would you improve this code to make it shorter, faster, or otherwise better?

My proposal:


function makeChange2 (m) {
// make it pennies and substract as many dollars as possible, then quarters etc.
var /*pennies*/p = m*100;
function x( /*name*/n, /*factor*/f ) { var /*coins*/c = Math.floor(p/f); p-=c*f; return (c>0? ', '+c+' '+n+ (c>1?'s':'') : ''); };
return (x('dollar',100)+x('quarter',25)+x('dime',10)+x('nickel',5)+(p>0?', '+p+' penn'+(p>1?'ies':'y'):'')).substr(2);
};




Why is this better?

  • Why creating a data structure, if you can express everything in function calls? Seperating both, results in extra data structures and code which handles this data structure and maintainers must understand all that extra just for ... nothing?

  • Having long variable names doesn't make code always more readable, compare:

    var c=Math.floor(p/f); p-=c*f; return c>0? ', '+c+' '+n+ (c>1?'s':'') : '';

    to

    var coins=Math.floor(pennies/factor); pennies-=coins*factor; return coins>0? ', '+coins+' '+name+ (coins>1?'s':'') : '';

    Using comments we can explain the abbreviated names once:
    c/*coins*/
    .
    Sometimes long variables names are just too verbose, looking like COBOL programms, saying nothing with lots of glyphs.
    Short names within a certain scope make things clearer. With long names the focus is not more on the operations but is blurred with the names. You have to search for the important operations.

  • It is also not always clear if multi-line formatting really helps that much, better type one line of usefull text explaining the *concept*. Human language is much more expressive then all this formatting stuff. Computers don't need formatting anyway.
    And scrolling through less code text is much more fun.
    If you are interested in the implementation you can read the line, learn the variable names and dive into the implementation.

    Look here, do you think this is so much better? I don't think so


    function makeChange2 (m) {

    var /*pennies*/p = m*100,

    /*exchange*/x = function( /*name*/n, /*factor*/f ) {
    var /*coins*/c = Math.floor(p/f);
    p-=c*f;
    return (c>0? ', '+c+' '+n+ (c>1?'s':'') : '');
    };

    return (x('dollar',100)
    +x('quarter',25)
    +x('dime',10)
    +x('nickel',5)
    +(p>0?', '+p+' penn'+(p>1?'ies':'y'):'')
    ).substr(2); // remove prefix ' ,'
    };





Oh yes i know join() should be faster
...

No comments: