Any idea how to write this piece of code nicer? ``...
# announcements
j
Any idea how to write this piece of code nicer?
Copy code
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
        try {
            customTm?.checkServerTrusted(chain, authType) ?: defaultTm.checkServerTrusted(chain, authType)
        } catch (e : CertificateException) {
            defaultTm.checkServerTrusted(chain, authType)
        }
    }
m
You could throw an Exception on the right side of the elvis operator
j
Yea…probably would be more readable but it has this “Using exception to control execution flow” anti-pattern feeling to it
m
Only alternative I can think of:
Copy code
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
        fun checkCustomTm() = return try {
				customTm?.checkServerTrusted(chain, authType)
			} catch (e : CertificateException) {
				null
			}
		}
		checkCustomTm() ?: defaultTm.checkServerTrusted(chain, authType)
    }
c
my 5 cents override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) { try { when(val tm = customTm){ null -> defaultTm.checkServerTrusted(chain, authType) else -> tm.checkServerTrusted(chain, authType) } } catch (e : CertificateException) { defaultTm.checkServerTrusted(chain, authType) } }