javascriptclass-designcode-complete

Issues about creating good interfaces on Javascript functions


How could I implement high-quality routines (mentioned by Steve McConnell, on Code Complete, chapter 7) on some Javascript code? For example, in this case:

$('#variable').on('click', function(){
                          //do some stuff
});

This is a very common snippet, but it is passing a function as parameter to another function. In my point of view, are not self-documented (it is not readable) and does not maintain the program abstraction as the book indicates; but is very commmon to see.


Solution

  • You could assign the function you are passing to a local variable so at the very least you can give it a name:

    var onClickCallback = function() {
                          //do some stuff
    };
    
    $('#variable').on('click', onClickCallback);