News for hackers. Stuff that matters.
Re:Callback Hell (2016) by athenot on Wednesday February 15, 2017 @10:28AM (#13652623)
This is where syntactic sugar can be helpful. CoffeeScript can make the example a lot more readable by removing the unnecessary braketing when the last argument of a function is a callback (common case in node.js). The only thing that's still ugly is the .bind() applied to a function, which somewhat messes with the style. Other than that, this makes callbacks flow a lot more naturally when reading source code.

(Of course, as it is for all stylistic considerations, it's a matter of taste, not absolutes.)

    fs.readdir source, (err, files) ->
      if err
        console.log 'Error finding files: ' + err
      else
        files.forEach (filename, fileIndex) ->
          console.log filename
          gm(source + filename).size (err, values) ->
            if err
              console.log 'Error identifying file size: ' + err
            else
              console.log filename + ' : ' + values
              aspect = values.width / values.height
              widths
                .forEach ((width, widthIndex) ->
                  height = Math.round(width / aspect)
                  console.log "resizing #{filename} to #{height}x#{height}"
                  this.resize width, height
                    .write "#{dest}w#{width}_#{filename}", (err) ->
                    if err then console.log 'Error writing file: ' + err
                ).bind this
Re:Callback Hell (2016) by Mithaldu on Wednesday February 15, 2017 @10:31AM (#13652654)
Please don't perpetuate the else silliness of the (intentionally bad) first example there. Stick a return in front of the log calls, lose the else, and unindent all following.
Re:Callback Hell (2016) by athenot on Wednesday February 15, 2017 @10:42AM (#13652729)
Yes, that is of course preferable. There are much better error handling strategies than what the example gave.

I was only commenting on the readability aspect, all other things being equal.

Re:Callback Hell (2016) by susw on Wednesday February 15, 2017 @10:51AM (#13652825)
Use '=>' instead of '->' to automatically bind 'this'.
Re:Callback Hell (2016) by weberc2 on Wednesday February 15, 2017 @11:12AM (#13653009)
I absolutely disagree. Brackets are a big part of the reason the JS version is more readable than the CoffeeScript version, IMO. I don't understand why anyone would think readability would increase with syntactical ambiguity. This isn't even the traditional braces-vs-indentation argument either; I much prefer Python's syntax to CoffeeScript's, and I use both regularly.
Re:Callback Hell (2016) by j_r_f on Wednesday February 15, 2017 @12:22PM (#13653615)
That is almost definitely more messy looking than the alternative.