5 ms·
users=db.get("SELECT name FROM users WHERE group='admin'") is more efficient and understandable then: users=[]; tmp=db.get("SELECT name,group FROM use
by itry 12y ago
users=db.get("SELECT name FROM users WHERE group='admin'")
is more efficient and understandable then:
users=[];
tmp=db.get("SELECT name,group FROM users")
for (i=0;i<tmp.count();i++)
{
if (tmp[i]['group']=='admin') users[]=tmp[i][name];
}
- CodeCube 12y agoThis is where a good abstraction layer can pay dividends. Of course, a bad abstraction layer (as evidenced by Spolsky's "leaky abstraction" post (http://www.joelonsoftware.com/articles/LeakyAbstractions.html) http://www.joelonsoftware.com/articles/LeakyAbstractions.htm...) on the opposite hand can add immeasurable cost.
- abritishguy 12y agoBut who in their right mind would do the second one? Obviously you can make code both less efficient and less readable. But starting with code that a competent programmer has written, I find it seldom makes it more readable when you make it more efficient.
- michael_h 12y agoBut who in their right mind would do the second one? Oh abritishguy, I can assure you that the world is awash in such magical code snippets.
- abritishguy 12y ago> competent programmer
- autokad 12y agoi tend to agree. take dijkstra's algorith implemented with a fibonacci heap or something very simple like for i = 1 to 100 if x = this do that and something with i else something else with i easy to read, less efficient than: if x = this for i = 1 to 100 do that and something with i else for i = 1 to 100 do something else with i from my experience, compilers usually dont hoist very well edit: sorry, spaces didnt come out well
- mtdewcmu 12y ago>>But who in their right mind would do the second one? Surely you jest. I don't know who, but their handiwork is everywhere.
- pjc50 12y agoPeople using ORM can end up writing the second one without it being obvious.
- edgyswingset 12y agoNot to mention, you'd never write code like the second one in a .NET ecosystem, unless you were purposefully trying to write bad, unmaintainable code.