Readability vs Performance comparison

对着背影说爱祢 提交于 2020-01-05 08:28:27

问题


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

The question: Should the above be replaced by:

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

and:

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

respectively?

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.

EDIT

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

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

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?


回答1:


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

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



回答2:


If you won't use the variables again (idNr, personName, etc.), the compile will most likely omit those assignments and the performance will be the same.

On the discussion on which of them is most readable I can't say much: one likes the one, I like the other. There isn't a consensus on that and you as a development team should make that consensus for your own.

If you do care about readability, I would pass in as much as ready made objects as possible. That also keeps maintaining method signatures when adding or deleting a property, so maybe this is best (thanks Sergey):

personModel.editPerson(currentPerson);


来源:https://stackoverflow.com/questions/30073895/readability-vs-performance-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!