I’m reading about CoffeeScript and am still trying to position the language, what can be done, what are the best practices, etc; I’m more used to strongly typed languages (AS3, Java, Scala) so my two questions may make you smile a bit 🙂
Question 1: Custom collections
What do you think about custom collections ? JS/CS is one of the weakest languages in this aspect; For instance there is no Array.remove and you have to use the cumbersome splice() method instead. Some function libraries (Like underscore) augment the API by providing functions taking arrays/objects as the first arg but given the choice I would prefer to write:
list fancyFunction 3, 4
rather than
fancyFunction list 3, 4
Let’s say I create a List class; Is it possible and if yes, what are the requisites for this class to be able to use CS’s comprehension syntax ? Worst case scenario, I guess the List could have a toArray() method and the usual CS operations could be performed on that returned value instead, but I hope there is a better solution.
Ideally, I would like to be able to define rich, custom collections, but not at the cost of losing comprehensions, etc.
Question 2: Mutability
What are people feelings about being very careful with mutability in CS/JS in general ?
When I read various code online, I have the impression everything is mutable and people generally think it’s better not to bother and have less lines of code instead.
For instance, mutable versus immutable basic Point class: (Hopefully I’m not doing something wrong)
Mutable
class Point
constructor: (@x, @y) ->
Immutable
class Point
constructor: (x, y) ->
@x = -> x
@y = -> y
Not that much more complicated but slightly weirder syntax. Another consideration is that JS is not the fastest thing known to man and having to create lots of objects in a loop just for the sake of being a purist might be counter productive, performance wise. I didn’t benchmark the cost of creating new Point objects versus altering the members of one though.
Imagine a big app with lots of modules communicating through thin APIs. Wouldn’t you want to pass immutable objects around ? Or do you perform defensive copies instead ?
Note: I am not trying to try and bend CS to the languages I know; Rather I want to know if it makes sense to reuse some of the concepts taken for granted in other languages.
Thanks
I think the short answer to this is that JavaScript is versatile, but it’s unsafe. There are no static types, and nothing is immutable (except, as in your example, by being isolated in a different scope). Some people try to fight this looseness—Google, for example, heavily annotates their code, JavaDoc-style. But mainstream JavaScript programmers don’t. They rarely hide instance variables behind getters, or throw an exceptions when you call their API with a string when they expected a boolean. That’s partly for practical reasons—JS is the only language I know of where people commonly talk about their code’s byte size—but it also reflects the language’s surrounding culture, where documentation and tests are valued far above hand-holding.
So, in short, I’d stick with
and write some good tests. You can’t protect against every possible misuse of your API, after all. Better to make its proper usage clear through good docs and tests.
By the way, Underscore does provide an alternate syntax that lets you write your code in the desired order:
Or you can extend the [Array] prototype. See Sugar.js for some fine examples.