-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoffeeScript preferred styles #187
Conversation
@@ -129,6 +129,8 @@ CoffeeScript | |||
private variables and functions. | |||
* Prefer `==` and `!=` to `is` and `isnt` | |||
* Prefer `||` and `&&` to `or` and `and` | |||
* Prefer `@` to `this` for scoping. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "for scoping"? Can you give an example of when we don't prefer @
to this
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify this to just read, 'Prefer @
to this
'. I don't know of any situation in coffeescript that using this
is preferred over @
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used @
for properties, but not for passing the instance itself. For example:
new Post user: @
I would use this
instead of @
there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used it for passing the instance itself. I got used to it, but I'd really just prefer to have a guideline.
I do think that "Always use @
" is a simpler guideline, which has value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like 'Prefer @
to this
' (suggestion from @drapergeek )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabebw the notion to use @
instead of this
may be more about context..not scoping. I was trying to state that we should use @
when in reference to the object that 'owns' the currently executing code. This conversation came about when a client asked why we do:
constructor: ->
@someMethod()
vs.
constructor: ->
this.someMethod()
So perhaps:
When referencing properties, use @ instead of this
or
Use @ instead of this, unless passing the instance itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Joe. Instead of a broad rule like "use @
anywhere you'd use this
", I would prefer something more specific like "Prefer @
over this
for referencing instance properties". It honestly looks weird to me to see @
standalone. I've seen this happen generally in two cases:
$(@)
(for $(this)
):
$('.some_selector').on 'click', ->
$(@).hide()
@
as the final line of a method (for returning this
):
class Foo
set: (properties) ->
_.extend this, properties
@
I have two reasons for using a more specific guideline:
- Only using
@
for instance attributes and methods feels more like Ruby, so it's less of a context switch. - For a while it was actually discouraged and Jeremy Ashkenas briefly considered removing support for bare
@
in CoffeeScript (discussion), but ultimately nothing was done because of the controversy around it. Perhaps a weak point, although, it's been removed in the upcoming "CoffeeScriptRedux".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So yeah, basically what Jason said.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elliot, I like Prefer @ over this for referencing instance properties
. It doesn't say anything about using @
or this
when passing the instance, but makes it clear when we do care.
That guideline gets my vote.
I use single quotes, except for when I interpolate. Example: class App.SuggestionListView extends Backbone.View
ACTIVE_CLASS = 'active'
SUGGESTION_SELECTOR = '[data-role="suggestion"]'
updateTranslation: (selectedSuggestion) ->
$activeSuggestions = $(".#{ACTIVE_CLASS}") They are twice as fast to type and are easier on the eyes. I think we should leave this as a per-project matter, just like HAML vs HTML. |
Twice as fast -> Require half the effort ;) |
@kaishin I have no problem leaving it as a per-project matter. I was looking through the CoffeeScript in our past projects, and we've done a mix of both versions of quoting, so I was hoping to get some preference for consistency. Something like (i think) we do in Ruby, where we use single quotes unless we have to use double quotes, seems like where we are. I will put this part to bed if nobody finds value in setting a preference. No Preference:
Double Quote Preference:
|
I would prefer consistency between our Ruby guides and CoffeeScript guides. So whatever guideline we currently have there (single quotes are still preferred, I believe), then we should make the same guideline here. |
I agree with @mcmire. I think we should be consistent with our style between CoffeeScript and Ruby with things like quotation marks. |
Keep in mind, it looks like the ruby guide is about to switch to double quotes. #174 |
I can live with double quotes in Coffeescript. |
Unless there's a strong reason to diverge, I'd prefer to be consistent with what we're doing in Ruby. |
Yes, double quotes for all! |
Should we promote this to a generic "if your language supports both single
|
Would you think to check both "Coffeescript" and "Generic" sections? I'm not sure that I would. |
I think it's fine to keep guidelines separate for each language. There are people who know Ruby but not CoffeeScript and vice versa (not among us, likely, but elsewhere). |
+1 for keeping this specific to each language |
Any objections to merging this as-is? |
👍 |
I'm in favor. |
Merging |
* Use `@` instead of `this` for scoping consistency * Prefer double quotes for consistency
Prefer
@
overthis
for referencing instance propertiesPrefer double quotes to preserve interpolation, allow for more complex strings, make selectors easier to read and for overall consistency.
$("[data-role='title']")
or$("input[type='text']")
"Isn't this nice?"
"It is #{nice}"