DEV Community

Discussion on: If Statement Golf: what's the longest one you've seen?

Collapse
 
bbasile profile image
Basile B. • Edited

Recently refactored this

else if (ue && customType != tok!"" && ue.unaryExpression && ue.unaryExpression.primaryExpression &&
                ue.unaryExpression.primaryExpression.identifierOrTemplateInstance &&
                ue.unaryExpression.primaryExpression.identifierOrTemplateInstance.identifier == customType &&
                ue.identifierOrTemplateInstance && ue.identifierOrTemplateInstance.identifier.text == "init")

into

else if (const IdentifierOrTemplateInstance iot = safeAccess(ue)
                .unaryExpression.primaryExpression.identifierOrTemplateInstance)

There was not that much conditions but since the ?. operator missed (D language) we had to check nested member against null to reach safely the end of the chain. Now the same is done with metaprograming magic.