从函数返回数据时的最佳实践是什么。返回Null还是空对象更好?又为什么一个要比另一个做呢?
考虑一下:
public UserEntity GetUserById(Guid userId)
{
//Imagine some code here to access database.....
//Check if data was returned and return a null if none found
if (!DataExists)
return null;
//Should I be doing this here instead?
//return new UserEntity();
else
return existingUserEntity;
}
让我们假装将有此计划,将有与该GUID数据库中没有用户信息的有效案例。我可以想象在这种情况下抛出异常是不合适的?另外,我的印象是异常处理会损害性能。
烙印99
料青山看我应如是
繁华开满天机