views:

104

answers:

4

Better performance:

list($result1,$result2,$result3,$result4)=get_all_result();

The number of total sql queries are fewer,and performance is a bit better.But it's hard to reuse.

Better structure:

$result1=module1_get_result();
$result2=module2_get_result();
$result3=module3_get_result();
$result4=module4_get_result();

It'll need more queries,so performance is a bit worse.But the structure is more clear.

Which do you perfer?

+1  A: 

Well, how important is the performance?

Is it worth it compared to the increased reuse/maintainability of the later?

Dan McGrath
+2  A: 

That depends.

For critical paths of the application you may have to write high performance code that can be hard to read and maintain, but optimizations are best done when actual problems have been identified. For the rest I would generally prefer maintainability over high performance.

Brian Rasmussen
A: 

Since your code looks like PHP code, I'll assume that is PHP.

It's better if you use array, both performance and readable.

$results = get_all_result();
//the above should return an array, so you can access
//$results['result1'], $results['result2'], $results['result3'], $results['result4']

Fewer call is better for performance. Store the result in array or object for better readability.

Also, premature optimization is evil. You should create code that maintainable, because creating is just about 10% of what you do. 90% of the code life cycle is to maintain it, either bug fix, change feature, add new feature, etc. Maintainable code will also have fewer bug, since the way it written, the bug will easier to spotted.

Donny Kurnia
What Hoare actually said was "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." He was not advocating that we ignore all the implications of our design for our code's performance. He certainly wasn't saying we should not optimize our code at all. If an approach is obvious Teh Suck then we should rewrite it. We just don't need to worry about saving milliseconds unless and until those milliseconds become crucial to the success of our application.
APC
Thanks for the info. I read it somewhere but forgot who write it and the exact word. I have encounter the case where the application I build run slow with thousand of data. Having create it with maintainable code, my colleague and myself can optimize the application to run many times faster. That's why I'm point out that having maintainable code is more important than trying to write optimize code but hard to maintained by having cryptic code.
Donny Kurnia
A: 

If get_all_result() returns a numerically indexed array (1, 2, 3, ...) why not:

extract(get_all_result(), EXTR_PREFIX_ALL, 'result');

extract() seems like a perfect candidate for this.

Alix Axel
Using extract() is like using eval(). It's powerful and it does the job alright, but one should think three times before using it.
Michał Rudnicki
@Michał Rudnicki: I wouldn't compare `extract()` with `eval()`, `register_globals = on` is a better candidate for a good comparison **when using `extract()` with only one argument**. For instance `extract($arr, EXTR_SKIP)` is pretty harmless. Just for the record, I see nothing wrong with my answer and I don't understand the downvote.
Alix Axel
Alix, the general idea that the data become the code is nowhere near good programming practices. It makes the code clever. Clever code is very difficult to debug. extract() transforms array keys into variables without any visibility of what is actually going on.
Michał Rudnicki