<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Armando Ota</title>
    <description>The latest articles on DEV Community by Armando Ota (@armando_ota).</description>
    <link>https://dev.to/armando_ota</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1998617%2F617d85ec-40d1-421e-8106-ad6413afbf81.jpg</url>
      <title>DEV Community: Armando Ota</title>
      <link>https://dev.to/armando_ota</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/armando_ota"/>
    <language>en</language>
    <item>
      <title>Don't do this #2</title>
      <dc:creator>Armando Ota</dc:creator>
      <pubDate>Mon, 16 Jun 2025 06:09:29 +0000</pubDate>
      <link>https://dev.to/armando_ota/dont-do-this-2-n8l</link>
      <guid>https://dev.to/armando_ota/dont-do-this-2-n8l</guid>
      <description>&lt;p&gt;Simple iteration tip...&lt;/p&gt;

&lt;p&gt;You are required to load "sh*t loads" of data from the back-end data storage. &lt;br&gt;
And then this data needs to be iterated. &lt;br&gt;
And data contains relation data which needs to be loaded.&lt;br&gt;
And child data needs to be iterated.&lt;br&gt;
And then both parent and child records must be combined into some output&lt;br&gt;
...&lt;br&gt;
And then you figure out it's extremely slow on large data.&lt;/p&gt;

&lt;p&gt;So some pseudo code: &lt;br&gt;
NOTE: if you can't read pseudo code, you're not the developer ... yet ;) #punIntended #comeBackWhenYouAreCapable&lt;/p&gt;

&lt;p&gt;So I've seen this kind of code 1 too many times!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query = '...';  //just some query that loads 100000+ items from storage
result = dataStorage.loadData(query);
fileData = []; //target array we need later for the output
for (idx = 0; idx &amp;lt; result.size(); idx++) {
  item = result[idx];
  for (idxRelated = 0; idxRelated &amp;lt; item.related.size(); idxRelated++) {
     relatedPartialData = item.related[idxRelated];
     fullRelatedData = dataStorage.loadById(relatedPartialData.id);
     prepareDataForExcelFile(item, fullRelatedData, filaData);
  }
}
createExcelFromFileData(fileData);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead do this:&lt;br&gt;
more pseudo code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fileData = []; //target array we need later for the output
query = '...';  //just some query that load 100000+ items from storage
result = dataStorage.loadData(query);

//use maps !!!

//map of items by item.id
itemsMap = {itemId =&amp;gt; item};  

// map of related data by related.id
relatedMap = {relatedId =&amp;gt; related}; 

//map of related.id(s) by item.id.   
//Note: have an array of related ids !!! 
itemRelatedMap = {itemId =&amp;gt; [relatedId]}; 


//iterate items
for (idx = 0; idx &amp;lt; result.size(); idx++) {
  item = result[idx];
  itemsMap[item.id] = item;
  for (idxRelated = 0; idxRelated &amp;lt; item.related.size(); idxRelated++) {
    relatedPartialData = item.related[idxRelated];
    itemRelatedMap[item.id] =&amp;gt; addToArray[relatedPartialData.id]};
    relatedMap[relatedPartialData.id] = null;
  }
}
//load and iterate related
relatedResult = dataStorage.loadById(relatedMap.getKeys());
for (idx = 0; idx &amp;lt; relatedResult.size(); idx++) {
  relatedItem = relatedResult[idx];
  relatedMap[relatedItem.id] = relatedItem;
}

//voila .. 
//with only 2 queries you have all the data in maps and ready for processing.

prepareDataForExcelFile(itemsMap, itemRelatedMap, relatedMap, fileData);
createExcelFromFileData(fileData);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lesson #1: don't iterate and execute queries!&lt;br&gt;
Lesson #2: if you think your result set will always be small, read the lesson #1 again.&lt;br&gt;
Lesson #3: for sorting purposes use proper collections or implement custom sorting function ;) &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Don't do this #1</title>
      <dc:creator>Armando Ota</dc:creator>
      <pubDate>Wed, 11 Jun 2025 10:33:35 +0000</pubDate>
      <link>https://dev.to/armando_ota/dont-do-this-1-h6f</link>
      <guid>https://dev.to/armando_ota/dont-do-this-1-h6f</guid>
      <description>&lt;p&gt;let this series be reminder to all of us what not to do and why not&lt;/p&gt;

&lt;p&gt;#1: simple example&lt;/p&gt;

&lt;p&gt;PHP Code using well known PHPMailer&lt;br&gt;
&lt;code&gt;function sendMail(...) {&lt;br&gt;
  ...&lt;br&gt;
  $mail-&amp;gt;send(); //DOUBLE PRETTY PLEASE WITH CREAM ON TOP GET RETURN VALUE&lt;br&gt;
  return true;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;taken from PHPMailer API doc:&lt;br&gt;
&lt;code&gt;public send() : bool&lt;/code&gt;&lt;br&gt;
Return values&lt;br&gt;
&lt;strong&gt;bool&lt;/strong&gt; — false on error - See the ErrorInfo property for details of the error&lt;/p&gt;

&lt;p&gt;Lesson #1: read the API docs of what methods return &lt;br&gt;
Lesson #2: handle error flows&lt;/p&gt;

</description>
      <category>php</category>
      <category>documentation</category>
      <category>programming</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
