Fixing the case where the generic Object variable is repeatedly used and using ternary operator