0

imagine that you have this line:

var result = content['aPossbileValue'] || $el.data('root-path') + content.type +  '/something/' + content.slug;

The line length is too long, so i wonder how can i improve this?

Possible options:

var result = content['aPossbileValue'] 
  || $el.data('root-path') + content.type + '/something/' + content.slug;
var result = content['aPossbileValue'] ||
             $el.data('root-path') + content.type + '/something/' + content.slug;
var result = content['aPossbileValue'] 
             || $el.data('root-path') + content.type + '/something/' + content.slug

Or maybe it's considered a bad practice!

let me know your opinion ^^!

edudepetris
  • 714
  • 14
  • 27

2 Answers2

0

If you looking to reduce the length of the line, one possibility is to use destructuring assignment on the content object to first get the related properties. On a second step, you can use template literals to generate the default value assigned to result in case aPossibleValue results on a falsy value. Something like this:

let {aPossibleValue, type, slug} = content;
var result = aPossibleValue || $el.data('root-path') + `${type}/something/${slug}`;

But you should note, these are ES6 features, not supported on some browsers and/or versions. So, always check the browser compatibility section. In the case you can't have access this luxury, I will go with something like this:

let val = content.aPossibleValue, type = content.type, slug = content.slug;
var result = val | $el.data('root-path') + type + '/something/' + slug;
Shidersz
  • 16,846
  • 2
  • 23
  • 48
0

Using variables with descriptive name are more readable than long and complex lines. In your case, this would be better:

var customPath = $el.data('root-path') + type + '/something/' + slug;
var result = content['aPossibleValue'] || customPath;
novomanish
  • 163
  • 10