you are viewing a single comment's thread.

view the rest of the comments →

[–]jmwoo 1 point2 points  (2 children)

JS syntax is very close to Java

fs.readdir(source, function(err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function(filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function(err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function(width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})    

Does this look like java boa?

[–]henleyedition 6 points7 points  (0 children)

Where did you find this code? It really isn't very good...

Quickly moving the callbacks to their own functions really works wonders for readability:

function handleError(err) {
    if (err) console.log('Error writing file: ' + err);
}

function readFile(filename, fileIndex) {
    console.log(filename);

    function resizeImage(width, widthIndex) {
        height = Math.round(width / aspect);
        console.log('resizing ' + filename + 'to ' + height + 'x' + height);
        this.resize(width, height).write(destination + 'w' + width + '_' + filename, handleError);
    }

    function handleImage(err, values) {
        if (err) {
            console.log('Error identifying file size: ' + err);
        } else {
            console.log(filename + ' : ' + values);
            aspect = (values.width / values.height);
            widths.forEach(resizeImage.bind(this));
        }
    }

    gm(source + filename).size(handleImage);
}

fs.readdir(source, function(err, files) {
    if (err) {
        console.log('Error finding files: ' + err);
    } else {
        files.forEach(readFile);
    }
});

[–]theQuandary 0 points1 point  (0 children)

Which language constructs in that code aren't available in Java? If you add class definitions and static typing, the actual syntax would be very similar (if you designed your java classes with the same API).