参考滥用:值得清理? [英] Ref Abuse: Worth Cleaning Up?

查看:135
本文介绍了参考滥用:值得清理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了使用 REF 关键字广泛和不必要的一些代码。原开发商显然是担心的对象将被克隆一样,如果 REF 未使用基本类型,并没有理会编写代码50K +行之前研究的问题。



这与其他不好的编码实践相结合,创造了一些情况是荒谬的表面上的危险。例如:




 客户人= NextInLine (); 
//人是爱丽丝
person.DataBackend.ChangeAddress(REF人,newAddress);
//现在的人可能是鲍勃,夏娃,或者为null



你能想象走进商店改变你的地址,然后走出去作为一个完全不同的人吗?






吓人,但是的在实践中的在此应用程序使用的 REF 似乎无害多余的。我无法辩解的大量的时间,将采取清理。为了帮助出售的想法,我提出以下问题:



还能如何裁判的不必要的使用是破坏性



<? p>我维修特别关注。似是而非的答案用实例是首选。



您也欢迎争论清理是没有必要的。


解决方案

我想说的最大危险是,如果该参数被设置为在函数内部由于某些原因:

 公共无效MakeNull(REF客户人)
{
//随机码
的人= NULL;
的回报;
}

现在,你不只是一个的不同的

人,你已经从存在完全抹去

只要谁正在开发这个应用程序的理解是:



< 。BLOCKQUOTE>

默认情况下,对象引用是按值传递







随着 REF 关键字,对象引用按引用传递。


< /块引用>

如果代码工作作为目前预计和开发人员理解上的差异,它可能不值得要采取删除所有的力气。


I have inherited some code that uses the ref keyword extensively and unnecessarily. The original developer apparently feared objects would be cloned like primitive types if ref was not used, and did not bother to research the issue before writing 50k+ lines of code.

This, combined with other bad coding practices, has created some situations that are absurdly dangerous on the surface. For example:


Customer person = NextInLine(); 
//person is Alice
person.DataBackend.ChangeAddress(ref person, newAddress);
//person could now be Bob, Eve, or null

Could you imagine walking into a store to change your address, and walking out as an entirely different person?


Scary, but in practice the use of ref in this application seems harmlessly superfluous. I am having trouble justifying the extensive amount of time it would take to clean it up. To help sell the idea, I pose the following question:

How else can unnecessary use of ref be destructive?

I am especially concerned with maintenance. Plausible answers with examples are preferred.

You are also welcome to argue clean-up is not necessary.

解决方案

I would say the biggest danger is if the parameter were set to null inside the function for some reason:

public void MakeNull(ref Customer person)
{
    // random code
    person = null;
    return;
}

Now, you're not just a different person, you've been erased from existence altogether!

As long as whoever is developing this application understands that:

By default, object references are passed by value.

and:

With the ref keyword, object references are passed by reference.

If the code works as expected now and your developers understand the difference, it's probably not worth the effort it's going to take remove them all.

这篇关于参考滥用:值得清理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆