lodash (no safety) _(comments) .head() .pluck('examples') .head() .pluck('code') .value() ramda (no safety) compose( prop('code'), head, prop('examples') head )(comments) imperative (runtime safety) let _exampleCode = null; if(comments.length > 0) { const headComment = comments[0]; if(headComment.examples !== null && headComment.examples !== undefined) { if(typeof head.examples === 'array') { if(head.examples.length > 0) { const headExample = head.examples[0]; if(headExample.code !== null && headExample.code !== undefined) { if(typeof headExample.code === 'string') { _exampleCode = headExample.code; } } } } } sanctuary (runtime safety) head(comments).chain(get(Array, 'examples')).chain(head).chain(get(String, 'code'))
Maybe just personal preference, but I would find this easier to parse in the multi-line form:
Other than that I totally agree
S
(or something like it) is the way to go for this.