Hacker News new | comments | show | ask | jobs | submit login

The problem is not callback, the problem is that callbacks exists in Javascript.

Callbacks themselves, when used wisely, can often enhance code readability, hell LISP has had function references since forever, but I think the most complain about callbacks are actually complains about callbacks in noisy languages, mostly likely languages with noisy syntaxes like Javascript and Java. When read that way, the disgust towards callbacks do seem to have merits. As the author has pointed out, the 2 getPhoto() functions at the end express and do exactly the same things, but obviously the CoffeeScript version reads better.

Callbacks have been around a long time and I've never heard of people complain as much about them as people have for Javascript and I conjecture the reasons are as follows:

1) There's no named parameters (keyword arguments) in Javascript, so people pass around objects literals into functions to emulate them. 2) Making lambdas in JS is too easy, but the syntax is too noisy. 3) Oh so many aliasing of this; 4) Self-chainable JS libraries like jQuery makes the style of calling multiple functions too easy. But lines can only go to long before becoming unwieldy, so people tend to indent chained method calls multiple times. 5) No modules and global namespace pollution is frown upon, so people are hesitant to flatten deeply nested callback chains. 6) There are a dozen ways to make a JS class and/or object depending on frameworks, and they are not at all compatible.

All of these "features" coagulate in JS into giant blobs of snot like this:

  <script>
    $(document).ready(function() {
      var $main = $("#main");
      $main.
        hide().
        click(function(e) {
          $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            success: function(data, textStatus, jqXHR) {
               data['rows'].forEach(function(line) {
                   $main.append($("<p>", {
                      className: "row"
                   }).append(line));
               });
            }
          });
        }).
        show();
    });
  </script>
Words for the wise, when you see a shiny new jQuery plugin, stop, think for 3 minutes, and then put it inside a Backbone View or whatever your favorite framework is other than jQuery*. If you don't know anything other than jQuery, now is probably the best time to learn a few.



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: