c# - 用Resharper重构,引入了bug,为什么?

标签 c# .net resharper

我有一个我认为带有 if 语句的方法,我决定在 Resharper 的帮助下重构它,后来发现它是我遇到很多错误的原因。

private bool isValid(User user)
{
    if (user == null)
        return false;
    if (user.IsBot)
        return true;
    if (user.GetClient() == null)
        return false;
    if (user.GetClient().GetData() == null)
        return false;
    if (user.GetClient().GetData().CurrentRoomId != _room.RoomId)
        return false;
    return true;
}

我把它重构成了这个

private bool isValid(User user)
{
    return user?.GetClient() != null && user.GetClient().GetData() != null && user.GetClient().GetData().CurrentRoomId == _room.RoomId;
}

将重构后的版本返回到原始版本后,所有错误都消失了。仅出于 self 提升的目的,有人可以告诉我我做错了什么吗?我什么都看不到,但很明显它破坏了很多东西,所以它一定有所不同。

最佳答案

原始版本更具可读性,并且在重构期间引入了一个错误。缺少 IsBot 检查。

您可以将方法重构为:

private bool isValid(User user)
{
    if (user == null)
        return false;
    if (user.IsBot)
        return true;
    return user.GetClient()?.GetData()?.CurrentRoomId == _room.RoomId;
}

仍然可读,但更短更切题。

关于c# - 用Resharper重构,引入了bug,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571470/

相关文章:

c# - 如何在 "azure function"上模拟 HttpRequest

c# - 如果使用 C# 的内容长度 > 7kb,则无法在 WebRequest 上发布

c# - 在 UWP 应用程序中更改 Flyout 的高度和宽度

c# - 什么决定一个变量是否被关闭?

c# - await Task.Run(() => semaphore.WaitOne()) 有什么问题吗?

c# - 键入时缺少 Visual Studio 2013 智能感知快速信息

具有泛型类型参数的类型的 C# 扩展方法

c# - 是否有隐藏的 "using"添加到 C# 类?

ecmascript-6 - Resharper 在函数外提示 'return' 语句

c# - 如何处理 ReSharper 的名称建议?