I have just started to pickup coffeescript in the past few days. Thought I would share my first post about the -> vs => Seems to be a common gotcha when first starting out.
On Mon, Jan 23, 2012 at 8:13 PM, Mark Hahn <m...@hahnca.com> wrote: > I rarely need the fat arrow. Maybe you are using a coding style that does > need it?
> If you nest several fat arrows the javascript gets really messy and hard to > read.
I have heard this argument several times, but I still dont get it how you could avoid fat arrows. Do you know of any examples of such coding style?
> Do you know of any examples of such coding style?
I tend to use closures heavily and rarely use @, except in jQuery callbacks. I don't use classes as I find I can use closures anywhere I am tempted to use a class. Without classes and OOPS, @ isn't used much.
> I tend to use closures heavily and rarely use @, except in jQuery callbacks. > I don't use classes as I find I can use closures anywhere I am tempted to > use a class. Without classes and OOPS, @ isn't used much.
But closures provide only a way to encapsulate the code, by avoiding class or prototype objects you are losing the ability to reuse code, which is imho *huge* disadvantage compared to the little discomfort associated with the usage of fat arrows.
>________________________________ > From: Jarek Foksa <ja...@kiwi-themes.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 1:12 AM >Subject: Re: [coffeescript] Why is function binding not the default?
>> I tend to use closures heavily and rarely use @, except in jQuery callbacks. >> I don't use classes as I find I can use closures anywhere I am tempted to >> use a class. Without classes and OOPS, @ isn't used much.
>But closures provide only a way to encapsulate the code, by >avoiding class or prototype objects you are losing the ability to reuse >code, which is imho *huge* disadvantage compared to the little discomfort >associated with the usage of fat arrows.
IMHO which features from classes, prototypes, functional programming, etc. should be used depends on the design, architecture, and requirements; and JavaScript and CoffeeScript are quite flexible. JQuery is providing a lot of functionality and is using its own "object-oriented programming" (OOP) under the hood, so in many cases very basic if any "OOP" will be required in an application itself. I read somewhere about making functions act like objects in LISP, unfortunately I lost the link but this is entirely possible and relatively easy in JavaScript and CoffeeScript.
On Tue, Jan 24, 2012 at 12:26 PM, Steve Howell <showel...@yahoo.com> wrote: > Can you explain how classes and prototypes are essential to code reuse? You > don't need them for encapsulation, nor do you need them for namespacing.
> ________________________________ > From: Jarek Foksa <ja...@kiwi-themes.com> > To: coffeescript@googlegroups.com > Sent: Tuesday, January 24, 2012 1:12 AM > Subject: Re: [coffeescript] Why is function binding not the default?
>> I tend to use closures heavily and rarely use @, except in jQuery >> callbacks. >> I don't use classes as I find I can use closures anywhere I am tempted to >> use a class. Without classes and OOPS, @ isn't used much.
> But closures provide only a way to encapsulate the code, by > avoiding class or prototype objects you are losing the ability to reuse > code, which is imho *huge* disadvantage compared to the little discomfort > associated with the usage of fat arrows.
> Can you explain how classes and prototypes are essential to code reuse?
I can't think of any better way for code reuse. You could probably use some factory methods, call/apply or mixins, but they are not as flexible as prototypes.
Let's say that I wanted to implement edit button. With prototypes I would do something like this (I'm using __proto__ because it's easier to read):
Thanks for the explanation. You're describing inheritance, and I agree that classes/prototypes make that pattern pretty flexible and easy to code. Now I'm curious why don't use class/extends syntax. Is it too verbose?
>________________________________ > From: Jarek Foksa <ja...@kiwi-themes.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 4:46 AM >Subject: Re: [coffeescript] Why is function binding not the default?
>> Can you explain how classes and prototypes are essential to code reuse?
>I can't think of any better way for code reuse. You could probably use >some factory methods, call/apply or mixins, but they are not as flexible >as prototypes.
>Let's say that I wanted to implement edit button. With prototypes I would >do something like this (I'm using __proto__ because it's easier to read):
> Now I'm curious why don't use class/extends syntax. Is it too verbose?
After having read this [1] article several times, I conclued that prototypical inheritance done with Object.create() is the most elegant approach.
In JavaScript and CoffeeScript one object inherits from another object, "class ... extends" syntax from CS introduces another concept which is in my opinion unnecessary.
CoffeeScript classes are actually less verbose than prototypes [2], but they hide the true nature of the language.
If you think in OOPS then use prototypes. If you think in functions then use closures. I used to think in OOPS but after using node for over a year my brain has warped to functions.
The beauty of functions is that you don't have to plan ahead. You just start writing the functions you need and as the code grows you realize you are using closures without thinking. It is similar to the revolution in NOSQL databases. Let your structure grow organically instead of trying to plan in advance.
Refactor, refactor, refactor, and don't live with early bad decisions. And of course the less early decisions you have to make the less early bad decisions you will have.
Here's more food for thought on closure style vs. more-class-centric style. The context here is Ruby/Clojure, but I think the principles apply to CS/JS as well:
>________________________________ > From: Mark Hahn <m...@hahnca.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 9:55 AM >Subject: Re: [coffeescript] Why is function binding not the default?
>If you think in OOPS then use prototypes. If you think in functions then use closures. I used to think in OOPS but after using node for over a year my brain has warped to functions.
>The beauty of functions is that you don't have to plan ahead. You just start writing the functions you need and as the code grows you realize you are using closures without thinking. It is similar to the revolution in NOSQL databases. Let your structure grow organically instead of trying to plan in advance.
>Refactor, refactor, refactor, and don't live with early bad decisions. And of course the less early decisions you have to make the less early bad decisions you will have.
Steve, this is really nice, it would be great if someone could put these in CoffeeScript for those who don't really understand Ruby or Clojure (myself included).
On Tue, Jan 24, 2012 at 7:03 PM, Steve Howell <showel...@yahoo.com> wrote: > Here's more food for thought on closure style vs. more-class-centric style. > The context here is Ruby/Clojure, but I think the principles apply to CS/JS > as well:
> ________________________________ > From: Mark Hahn <m...@hahnca.com> > To: coffeescript@googlegroups.com > Sent: Tuesday, January 24, 2012 9:55 AM
> Subject: Re: [coffeescript] Why is function binding not the default?
> If you think in OOPS then use prototypes. If you think in functions then > use closures. I used to think in OOPS but after using node for over a year > my brain has warped to functions.
> The beauty of functions is that you don't have to plan ahead. You just > start writing the functions you need and as the code grows you realize you > are using closures without thinking. It is similar to the revolution in > NOSQL databases. Let your structure grow organically instead of trying to > plan in advance.
> Refactor, refactor, refactor, and don't live with early bad decisions. And > of course the less early decisions you have to make the less early bad > decisions you will have.
I'm not sure exactly what you mean, but I think you're saying that it would be nice to have some kind of write-up/talk that goes into this topic in more depth, more from a CS/JS perspective?
>________________________________ > From: Chris Brody <chris.br...@gmail.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 12:11 PM >Subject: Re: [coffeescript] Why is function binding not the default?
>Steve, this is really nice, it would be great if someone could put >these in CoffeeScript for those who don't really understand Ruby or >Clojure (myself included).
>On Tue, Jan 24, 2012 at 7:03 PM, Steve Howell <showel...@yahoo.com> wrote: >> Here's more food for thought on closure style vs. more-class-centric style. >> The context here is Ruby/Clojure, but I think the principles apply to CS/JS >> as well:
>> ________________________________ >> From: Mark Hahn <m...@hahnca.com> >> To: coffeescript@googlegroups.com >> Sent: Tuesday, January 24, 2012 9:55 AM
>> Subject: Re: [coffeescript] Why is function binding not the default?
>> If you think in OOPS then use prototypes. If you think in functions then >> use closures. I used to think in OOPS but after using node for over a year >> my brain has warped to functions.
>> The beauty of functions is that you don't have to plan ahead. You just >> start writing the functions you need and as the code grows you realize you >> are using closures without thinking. It is similar to the revolution in >> NOSQL databases. Let your structure grow organically instead of trying to >> plan in advance.
>> Refactor, refactor, refactor, and don't live with early bad decisions. And >> of course the less early decisions you have to make the less early bad >> decisions you will have.
Yes. Also on the Vimeo page, Brian Marick also has some videos about monads, a very powerful concept in Haskell, and I am reading a very interesting post about monads for Javascript: http://amix.dk/blog/post/19509
On Tue, Jan 24, 2012 at 9:28 PM, Steve Howell <showel...@yahoo.com> wrote: > I'm not sure exactly what you mean, but I think you're saying that it would > be nice to have some kind of write-up/talk that goes into this topic in more > depth, more from a CS/JS perspective?
> ________________________________ > From: Chris Brody <chris.br...@gmail.com> > To: coffeescript@googlegroups.com > Sent: Tuesday, January 24, 2012 12:11 PM
> Subject: Re: [coffeescript] Why is function binding not the default?
> Steve, this is really nice, it would be great if someone could put > these in CoffeeScript for those who don't really understand Ruby or > Clojure (myself included).
> On Tue, Jan 24, 2012 at 7:03 PM, Steve Howell <showel...@yahoo.com> wrote: >> Here's more food for thought on closure style vs. more-class-centric >> style. >> The context here is Ruby/Clojure, but I think the principles apply to >> CS/JS >> as well:
>> ________________________________ >> From: Mark Hahn <m...@hahnca.com> >> To: coffeescript@googlegroups.com >> Sent: Tuesday, January 24, 2012 9:55 AM
>> Subject: Re: [coffeescript] Why is function binding not the default?
>> If you think in OOPS then use prototypes. If you think in functions then >> use closures. I used to think in OOPS but after using node for over a >> year >> my brain has warped to functions.
>> The beauty of functions is that you don't have to plan ahead. You just >> start writing the functions you need and as the code grows you realize you >> are using closures without thinking. It is similar to the revolution in >> NOSQL databases. Let your structure grow organically instead of trying to >> plan in advance.
>> Refactor, refactor, refactor, and don't live with early bad decisions. >> And >> of course the less early decisions you have to make the less early bad >> decisions you will have.
closure style: feels JS-like to me and automatically gives you private functions class style: familiar syntax for folks coming from Python/Ruby/Java namespaced functions: most flexible, functions aren't tied to any particular object
The last example is somewhat inspired by various Clojure talks I've heard, although it's my own interpretation, not necessarily the FP party line.
>________________________________ > From: Chris Brody <chris.br...@gmail.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 12:11 PM >Subject: Re: [coffeescript] Why is function binding not the default?
>Steve, this is really nice, it would be great if someone could put >these in CoffeeScript for those who don't really understand Ruby or >Clojure (myself included).
>On Tue, Jan 24, 2012 at 7:03 PM, Steve Howell <showel...@yahoo.com> wrote: >> Here's more food for thought on closure style vs. more-class-centric style. >> The context here is Ruby/Clojure, but I think the principles apply to CS/JS >> as well:
>> ________________________________ >> From: Mark Hahn <m...@hahnca.com> >> To: coffeescript@googlegroups.com >> Sent: Tuesday, January 24, 2012 9:55 AM
>> Subject: Re: [coffeescript] Why is function binding not the default?
>> If you think in OOPS then use prototypes. If you think in functions then >> use closures. I used to think in OOPS but after using node for over a year >> my brain has warped to functions.
>> The beauty of functions is that you don't have to plan ahead. You just >> start writing the functions you need and as the code grows you realize you >> are using closures without thinking. It is similar to the revolution in >> NOSQL databases. Let your structure grow organically instead of trying to >> plan in advance.
>> Refactor, refactor, refactor, and don't live with early bad decisions. And >> of course the less early decisions you have to make the less early bad >> decisions you will have.
> closure style: feels JS-like to me and automatically gives you private > functions > class style: familiar syntax for folks coming from Python/Ruby/Java > namespaced functions: most flexible, functions aren't tied to any particular > object
> The last example is somewhat inspired by various Clojure talks I've heard, > although it's my own interpretation, not necessarily the FP party line.
> ________________________________ > From: Chris Brody <chris.br...@gmail.com> > To: coffeescript@googlegroups.com > Sent: Tuesday, January 24, 2012 12:11 PM
> Subject: Re: [coffeescript] Why is function binding not the default?
> Steve, this is really nice, it would be great if someone could put > these in CoffeeScript for those who don't really understand Ruby or > Clojure (myself included).
> On Tue, Jan 24, 2012 at 7:03 PM, Steve Howell <showel...@yahoo.com> wrote: >> Here's more food for thought on closure style vs. more-class-centric >> style. >> The context here is Ruby/Clojure, but I think the principles apply to >> CS/JS >> as well:
>> ________________________________ >> From: Mark Hahn <m...@hahnca.com> >> To: coffeescript@googlegroups.com >> Sent: Tuesday, January 24, 2012 9:55 AM
>> Subject: Re: [coffeescript] Why is function binding not the default?
>> If you think in OOPS then use prototypes. If you think in functions then >> use closures. I used to think in OOPS but after using node for over a >> year >> my brain has warped to functions.
>> The beauty of functions is that you don't have to plan ahead. You just >> start writing the functions you need and as the code grows you realize you >> are using closures without thinking. It is similar to the revolution in >> NOSQL databases. Let your structure grow organically instead of trying to >> plan in advance.
>> Refactor, refactor, refactor, and don't live with early bad decisions. >> And >> of course the less early decisions you have to make the less early bad >> decisions you will have.
I've wondered this myself. For me, fat arrows is far more natural. Relying on the behavior of *not* having function binding is cryptic. It's unclear to me why you'd want this to change to something potentially unrelated.
In DOM programming sometimes the element is passed as "this" to the functions, so you want to use the skinny arrow and then keep track of "self" via closure.
On the server side, I would think that you'd tend not to have APIs that use "this", so you would generally use the fat arrow there if you are using classes.
>________________________________ > From: "karlseg...@gmail.com" <karlseg...@gmail.com> >To: coffeescript@googlegroups.com >Sent: Tuesday, January 24, 2012 5:18 PM >Subject: [coffeescript] Re: Why is function binding not the default?
>I've wondered this myself. For me, fat arrows is far more natural. Relying on the behavior of *not* having function binding is cryptic. It's unclear to me why you'd want this to change to something potentially unrelated.