refactor complicated ‘len’ (#16777)