0
\$\begingroup\$

Mostly robocop and hound give "line is too long" for such code statements. I just wanted to know which one is better and preferable way.

This one is a way to avoid making the message long so just do multiple string interpolation:

timeGet = "<span class='message'>after #{getTime2(online, offline)}</span>"

logs[index].extra = {message: "Camera came online #{timeGet}"}

This next one is to not do multiple string interpolation but just expand the JSON object to the new line to avoid "line is too long". What do you think is a good and preferable way of coding?

logs[index].extra = {
  message: "Camera came online <span class='message'>after #{getTime2(online, offline)}</span>"
}
\$\endgroup\$
2
  • \$\begingroup\$ why -1 any reason? \$\endgroup\$ Jan 2, 2017 at 6:37
  • \$\begingroup\$ Not my downvote, but I have some thoughts. As a general rule, our questions are of the form: here's some code that does this; what do you think of it? As stands, this code doesn't do anything except modify two variables. One of which is not used for anything otherwise. \$\endgroup\$
    – mdfst13
    Jan 2, 2017 at 13:51

1 Answer 1

1
\$\begingroup\$

For long strings in CoffeeScript consider using the heredocs notation:

logs[index].extra = {
  message: """
           Camera came online
           <span class='message'>
             after #{getTime2(online, offline)}
           </span>
           """
}

or simply concatenate multiple lines:

logs[index].extra = {
  message: "Camera came online" +
           "<span class='message'>" +
           "  after #{getTime2(online, offline)}" +
           "</span>"
}

The extra newlines and multiple spaces shouldn't matter in HTML since they're all trimmed to one space by the renderer (except defined otherwise, e.g. in pre or code).


It wasn't your question, but please also consider this syntax for defining an object and adding a field to it. I think it's better readable due to less indentation, it's more explicit and doesn't make anyone thing you'd add on top of an existing object:

logs[index].extra = {}
logs[index].extra.message = "Camera came online" +
                            "<span class='message'>" +
                            "  after #{getTime2(online, offline)}" +
                            "</span>"

The code from this example is equivalent to the code above it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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