【问题标题】:Readability vs Performance comparison可读性与性能比较
【发布时间】:2015-05-06 10:18:46
【问题描述】:

我正在查看我正在处理的项目中的一些代码,发现如下内容:

string personName = currentPerson.Name;
personModel.editPerson(idNr, personName);

上面是一个简单的例子,但也可能像下面这样:

string idNr= currentPerson.IdNr;
string personName = currentPerson.Name;
string age = currentPerson.Age;
...
string editor = AuthenticatedUser.Name;

personModel.editPerson(idNr, personName, age, gender, whatever, nationality, ..., currentTime, editor, weather, windspeed, topScorer, teethAmount, ...);

问题: 是否应将上述内容替换为:

personModel.editPerson(currentPerson.idNr, currentPerson.Name);

和:

personModel.editPerson(currentPerson.idNr, currentPerson.Name, currentPerson.Age, currentPerson.Gender, currentPerson.Whatever, currentPerson.Nationality, ..., theTime.current, weather.Status, wind.speed, scorers.topScorer, mouth.teethAmount, ...);

分别?

我认为为了可读性,将值放入变量会更好,但我猜性能会受到影响(即使是轻微的)。对于第一个示例,使用的参数很少,性能损失会更轻。

在我们的团队中,有人说最好以难以察觉的性能损失的低价获得可读性(特别是对于初级开发人员),而另一些人则说,如果这些变量过多而仅用于提高可读性的目的,则会影响最终会产生可能会引起注意的性能损失。

编辑

我将尝试用一个示例来解释我用值填充对象并在之后分配它们的意思。

想象一个有多个输入的表单:

public ActionResult _SavePerson(string id, string name, ...)
{
    personModel.editPerson(id, name, ...);
    ...

editPerson 方法:

public void editPerson(string id, string name, ...)
{
    webService1.client client = new ....;
    webService1.personType pt = new ...;
    pt.name = name;
    pt.id = id;
    pt. ....;
    client.setPerson(pt);
    ....
}

如果我要传递一个对象作为参数:

public ActionResult _SavePerson(string id, string name, ...)
{
    Person person = new ...;
    person.id = id;
    person.name = name;
    personModel.editPerson(person);
    ...

editPerson 方法:

public void editPerson(Person person)
{
    webService1.client client = new ....;
    webService1.personType pt = new ...;
    pt.name = person.name;
    pt.id = person.id;
    pt. ....;
    client.setPerson(pt);
    ....
}

你能理解我的疑惑吗?

【问题讨论】:

  • 我个人觉得没有中间变量更容易阅读,因为您可以准确地看到数据的来源以及传递的位置。通过添加变量,您只能看到它是从什么分配的,或者它被传递到哪里,而不是在一行中。
  • 性能不会受到影响。但在我的 看来,直接传递属性的可读性更好。变量增加了额外的不必要的间接性。另外,尽量不要命名那些做完全不同的事情的方法。我正在使用这样的代码库,我可以告诉你,拥有 20 种不同的名为 editPerson 的方法会让人想杀了你 :)
  • 为什么不personModel.editPerson(currentPerson)
  • 我会传递一个对象,而不是一个有 10 多个参数的方法。
  • 根据您的编辑,我知道您有不同的对象,具体取决于它们所使用的图层。在这种情况下,这就是映射器的用途! (因此您可以使用其他人的建议,每个参数一个实体)。

标签: c# .net performance readability


【解决方案1】:

我会使用Introduce Parameter Object 重构。如果您有一组自然组合在一起的参数(人名、人年龄等),则将它们分组到对象中并将其作为单个参数传递。

因此你已经有了这样的变量分组,你可以只传递当前的人对象:

personModel.editPerson(currentPerson);

正如Uncle Bob 所说,最好的理解和维护方法是没有参数的方法。一个参数很容易理解。二更难。我的经验法则 - 使用不超过 3 个参数(当然,这并不总是可行的,但我尝试遵循该规则)。

注意 - 如果您必须在某处传递大量参数,那么您的数据和逻辑可能是分开存在的。尝试将它们结合起来并避免传递数据。例如。而不是

 bankService.Charge(account.Id, account.Type, account.Balance, amount);

您可以将此逻辑考虑在内:

 account.Charge(amount); 

【讨论】:

  • 但是要将对象作为参数传递,您必须先填充对象属性,然后在方法中“分发它们”。这不就是“走不同的路去同一个地方”吗?
  • @chiapa 填充对象属性并分发它们是什么意思?如我所见,您已经拥有具有填充属性的 currentPerson 对象
  • @chiapa:你不应该创建一个对象只是为了在方法中传递它,但是如果你已经有了,为什么还要传递该变量的属性而不是变量本身呢?
  • @chiapa ASP.NET MVC 中的模型绑定器能够自动从多个参数构建复杂对象
  • 谢谢谢尔盖,虽然这个问题可能有点基于意见,但我会将您的答案标记为正确。当您说“专注于您的代码”时,您是对的
【解决方案2】:

如果您不再使用这些变量(idNrpersonName 等),编译很可能会忽略这些赋值并且性能将相同。

关于哪一个最易读的讨论我不能说太多:一个喜欢一个,我喜欢另一个。对此尚未达成共识,作为开发团队的您应该为自己达成共识。

如果您确实关心可读性,我会尽可能多地传递现成的对象。在添加或删除属性时,这也会保持方法签名,所以也许这是最好的(感谢Sergey):

personModel.editPerson(currentPerson);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 2011-06-10
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2010-11-14
    • 2015-10-22
    相关资源
    最近更新 更多