Explicit null and empty check in loop function