Hacker News new | ask | show | jobs
by chime 4861 days ago
I've made some pretty large apps in CS, by myself and in teams. So many good things in CS that I can no longer live without when writing front-end code:

1. @ = this

2. -> to => solves almost every context problem

3. Object notation by simply using colons ':'

    $('body').css
      color: 'red'
      background: 'blue'
4. statement if condition

5. jsondata?[2]?.hierarchy?.url

6. Optional brackets allow very terse/clean code:

    setTimeout ->
      statement
    , 1000
7. (function_argument = 'default_value') ->

8. Automatic return on last line

9. I also like how CS handles scoping, even though others might not. My very few globals are ALLCAPS and everything else is local scoped. I don't do things like {log, tan} = Math in the global scope just to save a few keystrokes elsewhere.

Add jQuery/Backbone/Underscore/Bootstrap to the mix and you can develop some very large, complex apps with CS in a very clean way. Of course, people may not like some of the above syntax but I love not having to write/parse 2x as much code.

2 comments

CS offers a lot of nice syntax sugar, indeed. It helps with many common mistakes made in JS code (missing `var`s and use of '==' operator come to mind).

However, it also provides a few new ways to shoot yourself in the foot. Automatic return may be one example. Generally, thanks to terser syntax, the code may become illegible very quickly—especially in an environment with many contributors and little care about coding style.

IMO successful CoffeeScript usage in larger projects requires strict coding guidelines. Maybe the language would even benefit from more ‘centralized’ and opinionated approach, like Python's PEPs.

> 8. Automatic return on last line

This is all good and fine until you realize that this will bite your ass when you need to write performance-sensitive code. So you end up with lots of explicit 'return' statements at the end of your functions.

> 9. things like {log, tan} = Math in the global scope just to save a few keystrokes elsewhere.

It's not only for saving keystrokes, but may also be improving performance. Some reason people add `local _G = _G` to the top of Lua source files to speed up access to _G.