I'm initializing a class in JavaScript with a bunch of complex property checks. Codacy is showing that this is bad design and recommends reducing cyclomatic complexity.
I'm at a loss for how to improve/streamline this, though.
var ok = require('objectkit')
var UsergridClient = function(opts) {
var self = this
// required properties
if (arguments.length === 2) {
self.orgId = arguments[0]
self.appId = arguments[1]
} else {
self.orgId = ok(opts).getIfExists('orgId') || ok(config).getIfExists('usergrid.orgId')
self.appId = ok(opts).getIfExists('appId') || ok(config).getIfExists('usergrid.appId')
}
// optional initializer properites
self.baseUrl = ok(opts).getIfExists('baseUrl') || ok(config).getIfExists('usergrid.baseUrl') || 'https://api.usergrid.com' // default: https://api.usergrid.com
self.clientId = ok(opts).getIfExists('clientId') || ok(config).getIfExists('usergrid.clientId') // default: undefined
self.clientSecret = ok(opts).getIfExists('clientSecret') || ok(config).getIfExists('usergrid.clientSecret') // default: undefined
self.tokenTtl = ok(opts).getIfExists('tokenTtl') || ok(config).getIfExists('usergrid.tokenTtl') || 3600 // time in seconds, default: 3600
self.authFallback = ok(opts).getIfExists('authFallback') || ok(config).getIfExists('usergrid.authFallback') || 'none' // ('none'|'app') default: 'none'
// optional post-init properties
self.paginationPreloadPages = 0 // number of pages to preload
self.paginationCacheTimeout = 300 * 1000 // default: 300 seconds
self.paginationCursors = [] // array of pagination cursors
if (self.orgId !== undefined && self.appId !== undefined) {
return self
} else {
throw new Error('"orgId" and "appId" parameters are required when instantiating UsergridClient')
}
}
opts
object, or a config file. At this point I don't even understand why it's being flagged for cyclomatically complex, unless it doesn't like the multiple||
inline conditionals. \$\endgroup\$