title: Should I always reduce count of database queries even if they do not give significant performance boost?
body: <p>I have read that that premature optimization is the root of evil. Because it takes more time, plus it can make code more complicated.</p>
  
  <p>Sometimes it happens that you need to get some item by id. This is quick query.</p>
  
  <p>And then later in same request you need again information of this item, in another function.</p>
  
  <p>Is it bad to just call query again? Or do I need to reuse the fetched object even if it saves maybe just some milliseconds which end user will not feel. Unless many users are doing same query at the same time.</p>
  
  <p>Ok, here would be an example:</p>
  
  <pre><code>function getItem() 
  {
       $item = null;
       if ($someCondition) {
  
            $item = $this-&gt;repository-&gt;getById(1);
       }
  
       someFunction($item);
  }
  
  function someFunction($item) 
  {
       if ($item == null) { 
  
            $item = $this-&gt;repository-&gt;getById(1);
       }
  
       doSomething($item);
  
  }
  </code></pre>
  
  <p>So in someFunction we add a check - additional logic to know if the item is fetched. In this simple example there is no problem to add more logic.</p>
  
  <p>But in real world applications, it happens that you need to add more logic just to optimize and not fetch the item second time.</p>
  
  <p>More logic - more time to write and read code.</p>
  
  <p>ORMs are example of not caring about optimization - they just lazy load related items for example like Doctrine. So you as developer save time by not writing query which joins other tables.</p>
  
  <p>Some programmers say its ugly code if it does same query twice when it can be avoided.</p>
  
  <p>It might take half hour or 1 hour to optimize in some cases. Add it adds up, productivity can decrease because of this. </p>
  
  <p>How should we do in those cases?</p>