Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This method is from Backbone-View. I'd like to refactor it but I don't have much experience with Coffee or JavaScript.

  linkStyle: ->
    if @model.get('published')
      'published'
    else
      if @model.get('started') 
        'started' 
      else 
        'default'
share|improve this question

1 Answer

up vote 5 down vote accepted

Well, there's not much to refactor, really. It's readable, and I assume it works as expected. But here are a couple of ideas anyway

Simplest one: Using else if just to avoid extra identation

linkStyle: ->
  if @model.get 'published'
    'published'
  else if @model.get 'started'
    'started' 
  else 
    'default'

Postfixing the conditions plus explicit returns:

linkStyle: ->
  return 'published' if @model.get 'published'
  return 'started' if @model.get 'started'
  'default' # implicit return

Avoiding duplicate strings:

linkStyle: ->
  for value in ['published', 'started']
    return value if @model.get value
  'default'

And so on...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.