开发者

Which version is better?



Response execute(String para1,String para2){

    String xmlRequest = buildRequest(para1, para2);

    String xmlResponse = getXmlResponse(xmlRequest);

    Response response = parseResponse(xmlResponse);

    return response;

}

Or the concatenated version? Why is that?


Response execute(String para1,String para2){

return parseResponse(getXmlResponse(buildRequest(para1, para2)));

}

开发者_开发百科

Thanks, Sarah


In contrast to what others have written, I find the second version much easier to read.

First, there's about half as many symbols for me to parse (12 vs 21). There's no way for me to glance at the first one and understand what it's doing, even though what it's doing is just as simple as the second method.

Second, in the second example, the data flow is obvious: you can just read down the line. In the first one, I had to look carefully to make sure the variable in one is used in the next, for each pair of lines (especially since 2 of the temp variables start with the exact same 5 characters). This is exactly the sort of case that somebody is going to update later, leave an extra variable assignment around by mistake -- I've seen it a million times.

Third, the second one is written in a more functional style, and reasoning about functional code tends to be easier. In this case the benefit is minimal, since the first 2 lines are assignments to an immutable object, but declaring 3 variables shifts me into "something complex is happening here" mode, which really isn't the case here.

Of course, this method is so small you can't go far wrong either way, but I find it useful to apply good habits at any scale.


I like the first version better because it's easier to read, not so many nested parameters. Other than readability there shouldn't be a difference in performance.


First one is better. Because it is more readable. Any programmer can write code that a computer can understand. Good programmers write code that humans can understand.


They are the same, but as mentioned, the first one is more readble, so that would tip the scales in favor for the first one.

When you nest methods like in the second one, the compiler simply does what you do in the first one for you. So there is no win in terms of speed memory use.


I would say it depends. If it's a static utility method that is extremely unlikely to change, I might go with the second one just to keep the number of lines smaller. But it totally depends.


The first one has an advantage while debugging: when you step through this code in a debugger, you have a chance to view the return values of the different methods easily (i.e., with the debugger you can easily see what is in xmlRequest, xmlResponse and response).

However, I'd prefer the second notation, because the first version is overly verbose and I don't find the first version more readable than the second. But ofcourse it's a matter of opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜