Skip to content

Instantly share code, notes, and snippets.

@twoolie
Last active December 23, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twoolie/81ac8e0c2ed6b4dc5aff to your computer and use it in GitHub Desktop.
Save twoolie/81ac8e0c2ed6b4dc5aff to your computer and use it in GitHub Desktop.

Instead of

((i) ->
    //Do something with i
)(annoying.and.complicated[variable].name[5])

You can write

do (i=annoying.and.complicated[variable].name[5]) ->
    //Do something with i

Also, I always have these two functions in scope.

after = (ms,cb) -> setTimeout(cb,ms)
every = (ms,cb) -> setInterval(cb,ms)

Which makes formulating your example trivial

for i in [1..5]
    do (i) ->
        after i*1000, ->
            console.log i

Or even:

for i in [1..5] then do (i) -> after i*1000, -> console.log i

Finally, I think your gripes about object literal syntax are FUD. Nothing is forcing you to omit braces in object literals, it is merely a convenience. Further, idiomatic coffeescript would have you write the following to make it clear that the items are part of the same object.

someFunction 500,
    abc : 123
    def : 456

If you actually need them to be different objects, you write it like this.

someFunction 500,
    abc: 123
    def: 456
,
    abc: 789
    def: 012

This syntactic form is especially useful for jquery people

$.ajax "http://maps.googleapis.com/maps/api/geocode/output",
    method: "GET"
    params:
        address: "1600 Amphitheatre Parkway, Mountain View, CA"
        sensor: false
    success: (data) ->
        alert data.results.geometry.location
    error: (_, textStatus, errorThrown) ->
        alert "#{textStatus}: #{errorThrown}"
        
$("div.special").css
    backgroundColor: "pink"
    color: "darkred"
    border: "1px dashed purple"

The point is that EVERY language has multiple ways of writing the same thing. The trick is to make sure that you follow the established language idioms (or establish your own styleguide, and stick to it).

All of the optional brackets in coffeescript are just that. Optional. If you find that a piece of code is ambiguous, add some brackets and remove the ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment