Re:Making Coffeescript’s Whitespace More Significant
by tordek on Wednesday November 30, 2011 @03:43PM
(#3296822)
In your last example, `.find('.more_stuff')` works on the value returned from `.addClass('other')` (or so it'd seem), so it behaves differently.
Re:Making Coffeescript’s Whitespace More Significant
by Groxx on Wednesday November 30, 2011 @04:53PM
(#3297063)
Which, with jQuery, is the same as the results from the most-recent selector in the chain (in this case, the .find('.stuff') before it). Normally though, you'd be absolutely correct, and that example would need to nest the .addClass('other') inside its .find so it doesn't pollute the next .find:
$('.class')
.find('.sub').remove()
.find('.stuff')
.addClass('other')
.find('.more_stuff').removeClass('things')
.filter('li').appendTo('#my_list')
None of this is meant to imply that chaining things like this is a Good Idea™, and I avoid .end() like the plague and use intermediate variables. But when you don't need the root or intermediate results for anything else, yes, this is more readable, more easily optimized (you can't get it wrong, every level is cached for you), less prone to error, and significantly fewer characters / lines of code. That's simplifying and improving.
-- late-edit:
Less whitespace zigzaggery is also possible (I agree, not easy to read such dense zigzagging), and similarly easier with significant whitespace. My example was essentially just a trivial one, I tend to see larger ones where I see that kind of indentation at all. Is this better?