0
\$\begingroup\$

The code below is converting: data from one format to another

# input sample
{
  productText: [
    {
      "language": "en",
      "version": "1",
      "sequence": 1,
      "text": "blah",
      "textType": "ROMANCE"
    }
  ]
}

# output sample
{
  productText: [
    {
      sequence: 1,
      textType: "ROMANCE"
      text: [
        {
          text: "blah",
          language: "en",
          version: "1",
        }
      ]
    }
  ] 
} 

The source:

deserialize: (profileJSON)->
  return profileJSON unless profileJSON.productText?
  profileJSON.productText = _(profileJSON.productText).reduce(
    (acc, el)->
      current  = _(acc).findWhere(
        _(el).pick('sequence', 'textType')
      )
      unless current?
        current = _(el).pick('sequence', 'textType')
        current.text = []
        acc.push(current)
      current.text.push(
        _(el).pick('version', 'text', 'language')
      )
      return acc
    []
  )
  return profileJSON

My colleague said that I overuse underscore. Is he right? Is it a good piece of code?

\$\endgroup\$
3
\$\begingroup\$

Interesting question,

I am not sure there is such a thing as over-use of underscore, and what the drawback would be. I think that code looks okay from that perspective.

However, from a once over:

  • deserialize as a function name is unfortunate, especially since it does not deserialize JSON
  • profileJSON is equally unfortunate, it does not contain JSON
  • This seems out of order, and a roundabout way of doing things:

    current.text = []
    acc.push(current)
    current.text.push(
      _(el).pick('version', 'text', 'language')
    )
    

    could be more like

    current.text = [ _(el).pick('version', 'text', 'language') ]
    acc.push(current)
    
  • You are wrapping el 3 times, wrap it once and assign it to a variable, perhaps that is what your colleague meant?

  • You call _(el).pick('sequence', 'textType') twice, again you should have cached this, see above
  • acc and el are meaningless variables and don't give me any insight as to what is inside productText
\$\endgroup\$
  • \$\begingroup\$ I think you made a lot of assumption about context of this code(and the most are wrong). This little function make part of conversion from third party API response(profile which is JSON) to our internal application format. name "deserialize" has meaning in a context. Question isn't about a context, but about a style. _(el).pick('sequence', 'textType') piece is a kind of duplication, but it makes code more readable \$\endgroup\$ – kharandziuk Sep 10 '14 at 15:02
  • \$\begingroup\$ @kharandziuk "piece is a kind of duplication, but it makes code more readable" makes no sense to me. \$\endgroup\$ – Renato Gama Sep 10 '14 at 16:30
  • 2
    \$\begingroup\$ @kharandziuk Note that, as per the guidelines, reviewers are free to review any and all aspects of the code. Style, context, naming, and logic are all reviewable. So even if deserialize makes sense in your context, a) well, we don't know that (unless you tell us in the question), and b) it might still be a less-than-great name regardless. Doesn't mean you have to change anything, of course, it's just the job a good review to point out what seems odd. \$\endgroup\$ – Flambino Sep 10 '14 at 18:18
  • 2
    \$\begingroup\$ I'd agree that there's no such thing as an overuse of underscore (unless we're talking about the duplication). If you're including a library anyway, you might as well get your money's worth. Eliminating use of a library might make sense, but limiting use doesn't really make sense if it's there to use. \$\endgroup\$ – Flambino Sep 10 '14 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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