可读性VS性能比较 [英] Readability vs Performance comparison

查看:256
本文介绍了可读性VS性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在审查一些code present在一个项目我工作,发现这样的事情:

I was reviewing some of the code present in a project I'm working on and found things like these:

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

以上是一个简单的例子,但它可能也像如下:

The above is a simple example but it may as well be like below:

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, ...);

分别?

我觉得对于可读性,把值代入变量是更好,但我猜的性能将受到影响(即使轻度)。对于第一个例子,其中几个参数时,性能损失会更轻。

I think for readability, putting the values into variables is better but I'm guessing the performance will suffer (even if lightly). For the first example, where few parameters are used, the performance loss will be even lighter.

在我们的团队,有人说这是更好地在不易察觉的性能损失低的价格能有可读性(专为初级开发人员),而另一些人说,有太多的这些变量可读性是只会目的将在末产生损失的性能可能被注意到。

In our team, some people say it is better to have readability (specially for junior developers) at the low price of imperceptible performance losses, while others say that having too much of these variables that serve only the purpose of readability will in the end produce a loss of performance that may be noticed.

修改

我会尽量解释我的意思与填充对象的价值观和一个例子后分发它们。

I will try to explain what I meant with filling an object with values and distributing them afterwards with an example.

与多个输入试想一个表格:

Imagine a form with multiple inputs:

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

该editPerson方法:

The editPerson method:

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);
    ....
}

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

If I was to pass an object as parameter:

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

该editPerson方法:

The editPerson method:

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);
    ....
}

你能在这里明白我的疑问?

Can you understand my doubt here?

推荐答案

我会用引入参数对象重构。如果你有一组参数自然走在一起(人名,人物年龄等),然后把它们组合成目标,并将其作为一个单独的参数。

I would use Introduce Parameter Object refactoring. If you have a group of parameters that naturally go together (person name, person age, etc) then group them into object and pass it as a single parameter.

因此​​,你已经有了变量,分组,你可以通过目前的人员目标:

Thus you already have such grouping of variables, you can just pass current person object:

personModel.editPerson(currentPerson);

由于 Bob大叔说,他们理解的最好方法和维护方法是不带参数。一个参数是很容易理解。二是更难。我的经验法则 - 使用不超过3个参数(当然,这并不总是可能的,但我尽量遵循这一规则)

As Uncle Bob says, the best method for understanding and maintaining is method without parameters. One parameter is easy to understand. Two is harder. My rule of thumb - use no more than 3 parameters (of course, this is not always possible, but I try to follow that rule).

请注意 - 如果你要通过很多参数的地方,那么很可能你的数据和逻辑分居。尽量把它们结合起来,避免传递数据。例如。而不是

Note - if you have to pass lot of parameters somewhere, then probably you have data and logic living separately. Try to combine them and avoid passing data. E.g. instead of

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

您可以将这种逻辑考虑:

You can move this logic into account:

 account.Charge(amount); 

这篇关于可读性VS性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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