<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Newsun]]></title><description><![CDATA[Just Engineering Things....]]></description><link>https://newsun.com.np</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1761458835544/2637d03a-8930-43b5-9733-443317f33dbc.png</url><title>Newsun</title><link>https://newsun.com.np</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 06:24:24 GMT</lastBuildDate><atom:link href="https://newsun.com.np/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Padding Bytes, Why Should You Care About It?]]></title><description><![CDATA[This is part of Gopher UnderCover Series.

Before discussing about padding bytes let’s first investigate why we need it, in the first place.
For this i am assuming we are having 64 bit architecture machine.
type Foo struct{
 isValid bool
 id int16
 v...]]></description><link>https://newsun.com.np/padding-bytes-why-should-you-care-about-it</link><guid isPermaLink="true">https://newsun.com.np/padding-bytes-why-should-you-care-about-it</guid><category><![CDATA[Go Language]]></category><dc:creator><![CDATA[Newsun]]></dc:creator><pubDate>Thu, 15 May 2025 15:25:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1746889218687/c2d97970-ca21-41bf-805d-c9dc378fb98b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>This is part of Gopher UnderCover Series.</p>
</blockquote>
<p>Before discussing about padding bytes let’s first investigate why we need it, in the first place.</p>
<p>For this i am assuming we are having 64 bit architecture machine.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Foo <span class="hljs-keyword">struct</span>{
 isValid <span class="hljs-keyword">bool</span>
 id <span class="hljs-keyword">int16</span>
 value <span class="hljs-keyword">int32</span>
}
</code></pre>
<p>So, How Much Memory Will this struct will take?</p>
<p>bool → 1 bytes, int16 → 2 bytes , int32 → 4 bytes =&gt; 7 bytes.</p>
<p>No, this will take 8 bytes memory. But why? it takes extra 1 bytes.</p>
<p>Because there is memory alignment thing, which compiler has to take care about.</p>
<p>To make 8 bytes memory block filled, compiler adds extra padding bytes.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747319609834/7125b7ed-5d80-466f-a8e9-2fea3d94eaa8.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-why-this-happen">Why This Happen?</h2>
<p>From a hardware perspective, have to make sure memory always align in certain boundaries. To minimize amount of operation to get data from hardware.</p>
<h3 id="heading-lets-dont-follow-memory-alignment">Let’s don’t follow memory alignment.</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747320085201/ee351997-37d6-40e1-9a1d-0c45bd1cafbb.png" alt class="image--center mx-auto" /></p>
<p>In above, Diagram 2bytes of integer is in Block A where rest of 2 in Block B. So, to get data hardware have to perform 2 operation just to get that data, which is waste of 1 cpu cycle. we can have done in 1.</p>
<p>For 1 byte data, it can be place anywhere, when data is more than 1 byte, it have to follow alignment, to make sure we don’t increase operation of hardware to get data.</p>
<p>Alignment make sure, that data store in sequential manner in memory and store in same memory block.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747320556741/300f5cec-0c38-4901-a2b2-5f2f98c21bb3.png" alt class="image--center mx-auto" /></p>
<p>As per size of data type, alignment is assigned by compiler.</p>
<p>int 16 → 2 bytes alignment.</p>
<h2 id="heading-why-should-you-care-about">Why Should You Care About?</h2>
<p>In most of the cases, you don’t need to care about how this form. But in profiling program some performance issue comes, there you need to care.Some time there may form too much of extra padding bytes.</p>
<p>Let Change our Struct.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Foo <span class="hljs-keyword">struct</span>{
 isValid <span class="hljs-keyword">bool</span>
 id <span class="hljs-keyword">int64</span>
 value <span class="hljs-keyword">int64</span>
 anotherVal <span class="hljs-keyword">float32</span>
}
</code></pre>
<p>There is rule, whichever is larger datatype in struct, alignment will follow that. So, here 8 byte alignment will be allocated.</p>
<p>here, 1 byte + 7 padding bytes + 8 bytes+8 bytes+4 bytes =&gt; 28 bytes. we have to take data in 8 bytes but 28 is not divisible by 8. So, we have add extra 4 padding bytes. which makes → 32 bytes.</p>
<p>Which visual representation is something like:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747322040218/12c209a5-7422-418c-a537-c800de49ae32.png" alt class="image--center mx-auto" /></p>
<p>So, we have 11 bytes of padding byte.</p>
<p>There is trick, write struct as largest value at first.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Foo <span class="hljs-keyword">struct</span>{
 id <span class="hljs-keyword">int64</span>
 value <span class="hljs-keyword">int64</span>
 anotherVal <span class="hljs-keyword">float32</span>
 isValid <span class="hljs-keyword">bool</span>
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747322396436/8b1cb7c9-935a-488c-87f4-ba882e387f98.png" alt class="image--center mx-auto" /></p>
<p>Now, 8 bytes + 8 bytes + 4 bytes + 1 bytes = 21 bytes (which is not divisible by 8) so we add , 21 bytes + 3 bytes = 24 bytes. So we save 6 bytes. By just changing way of defining struct.</p>
<blockquote>
<p>Do this if only if it making impact on performance of your program, otherwise you don’t need to do premature optimization.</p>
</blockquote>
<p><img src="https://y.yarn.co/9cc3149c-91fb-48fc-9d26-145e2c05a75c_text.gif" alt="YARN | Okay, bye-bye. Love you. | The Office (2005) - S09E11 Suit Warehouse  | Video gifs by quotes | 9cc3149c | 紗" class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Mastering Chaos: The Art of Embracing Anti-Patterns]]></title><description><![CDATA[As a software engineer/developer we have to give build our applications/programs faster as possible because so the users can use the applications fast as possible. Usually in this hurry we anyhow make the application working for that for current time...]]></description><link>https://newsun.com.np/mastering-chaos-the-art-of-embracing-anti-patterns</link><guid isPermaLink="true">https://newsun.com.np/mastering-chaos-the-art-of-embracing-anti-patterns</guid><category><![CDATA[patterns]]></category><category><![CDATA[Programming Tips]]></category><dc:creator><![CDATA[Newsun]]></dc:creator><pubDate>Wed, 03 Jan 2024 16:47:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1704297698351/597d7e4d-be25-478c-bf31-9925a0027db8.avif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a software engineer/developer we have to give build our applications/programs faster as possible because so the users can use the applications fast as possible. Usually in this hurry we anyhow make the application working for that for current time but in this rush we write some code that fucks our mind later and makes difficult to make project scalable for long-term. It creates more problem than it solves.</p>
<blockquote>
<p>An antipattern is just like a pattern, except that instead of a solution, it gives something that looks superficially like a solution but isn’t one. - Andrew Koenig</p>
</blockquote>
<p>In this article, we will discuss some of anti-pattern practice which makes chaos everywhere in project. Later Project Become difficulty to maintain and other developer might find code difficult to understand.</p>
<h2 id="heading-spaghetti">Spaghetti</h2>
<p>Spaghetti is fancier term when the code is unstructured and difficult to understand.This wastes time and energy in finding bugs, and fix them is harder because code has not proper defined structure.</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() {
  <span class="hljs-built_in">String</span> name = getName();
  greetUser(name);

  <span class="hljs-built_in">int</span> num1 = getNumber(<span class="hljs-string">"first"</span>);
  <span class="hljs-built_in">int</span> num2 = getNumber(<span class="hljs-string">"second"</span>);

  <span class="hljs-built_in">int</span> sum = addNumbers(num1, num2);
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"The sum of <span class="hljs-subst">$num1</span> and <span class="hljs-subst">$num2</span> is: <span class="hljs-subst">$sum</span>"</span>);

  <span class="hljs-built_in">int</span> product = multiplyNumbers(num1, num2);
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"The product of <span class="hljs-subst">$num1</span> and <span class="hljs-subst">$num2</span> is: <span class="hljs-subst">$product</span>"</span>);
}

<span class="hljs-built_in">String</span> getName() {
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Enter your name:"</span>);
  <span class="hljs-keyword">return</span> readInput();
}

<span class="hljs-keyword">void</span> greetUser(<span class="hljs-built_in">String</span> name) {
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Hello, <span class="hljs-subst">$name</span>!"</span>);
}

<span class="hljs-built_in">int</span> getNumber(<span class="hljs-built_in">String</span> ordinal) {
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Enter the <span class="hljs-subst">$ordinal</span> number:"</span>);
  <span class="hljs-keyword">return</span> <span class="hljs-built_in">int</span>.parse(readInput());
}

<span class="hljs-built_in">int</span> addNumbers(<span class="hljs-built_in">int</span> a, <span class="hljs-built_in">int</span> b) {
  <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-built_in">int</span> multiplyNumbers(<span class="hljs-built_in">int</span> a, <span class="hljs-built_in">int</span> b) {
  <span class="hljs-keyword">return</span> a * b;
}

<span class="hljs-built_in">String</span> readInput() {
  <span class="hljs-keyword">return</span> stdin.readLineSync()!;
}
</code></pre>
<p>This how you can follow Spaghetti Pattern in Your Code.</p>
<h2 id="heading-golden-hammer">Golden Hammer</h2>
<p>Example You have a toolbox where you have hammer, screwdriver, wrench. Each Tool is best for it's own specific work.Now, the hammer is gold platted, it so shinny so you want to use hammer more, like you're attach to hammer.So for everything you hammer because it looks cool.</p>
<p>Each tool perform best in own area. Not using suitable tool for specific task and using tool which you like no matter it perform well or not. It is Golden Hammer Pattern.</p>
<p>It is like using Javascript for Everything like Frontend, Backend, Mobile Development,AI and embedded systems.</p>
<h2 id="heading-lava-flow">Lava Flow</h2>
<p>This occurs when the code that is no longer use is present in source code. This leads to unnecessary complexity because, developer don't know whether code is used some where else in codebase. Refactoring code can reduce this. It also Leads to performance issue in application.</p>
<h2 id="heading-unnecessary-complexity">Unnecessary Complexity</h2>
<p>When Solution for Problem is simple but developer want to flex his skills with writing complexly (Over Engineering). Usually this occur because of lack of experience and not focus on simplicity.</p>
<p><img src="https://i.imgflip.com/1q58sf.jpg" alt="Captain Picard Facepalm Meme - Imgflip" /></p>
<h2 id="heading-god-class">God Class</h2>
<p>It occurs when a class does multiple things(Like God), resulting tight coupling and decrease maintainability. It has too many responsibilities and violates single responsibility principle of object oriented programming.</p>
<p>For example a single class , handling authentication of user and handling user blogs, profiles etc.</p>
<p><img src="https://media.makeameme.org/created/god-god-everywhere.jpg" alt="GOD GOD EVERYWHERE - Buzz and Woody (Toy Story) Meme | Make a Meme" /></p>
<p>These are few anti-patterns which developer do in their codebase and this should avoid or face the consequences of it.</p>
]]></content:encoded></item><item><title><![CDATA[Why Async/Await Don't Work With Array Methods?]]></title><description><![CDATA[Recently, While I was working on the project. I have to make multiple local database calls. I used forEach Loop For that purpose.
My Code was Something like this:
void main() {
    final List<int> ids = [1,5,3,7];
    print("Loop Started");
   ids.fo...]]></description><link>https://newsun.com.np/why-asyncawait-dont-work-with-array-methods</link><guid isPermaLink="true">https://newsun.com.np/why-asyncawait-dont-work-with-array-methods</guid><category><![CDATA[array methods]]></category><category><![CDATA[Dart]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[asynchronous]]></category><dc:creator><![CDATA[Newsun]]></dc:creator><pubDate>Tue, 09 May 2023 16:18:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683645506061/0dae7be9-a749-4061-bfbb-7ef2956c1a1f.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Recently, While I was working on the project. I have to make multiple local database calls. I used forEach Loop For that purpose.</p>
<p>My Code was Something like this:</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() {
    <span class="hljs-keyword">final</span> <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; ids = [<span class="hljs-number">1</span>,<span class="hljs-number">5</span>,<span class="hljs-number">3</span>,<span class="hljs-number">7</span>];
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Started"</span>);
   ids.forEach((element)<span class="hljs-keyword">async</span>{
     <span class="hljs-keyword">await</span> getDataAsId(element);
   });
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Ended"</span>);
}

Future&lt;<span class="hljs-keyword">void</span>&gt; getDataAsId(<span class="hljs-built_in">int</span> id)<span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">await</span> Future.delayed(<span class="hljs-built_in">Duration</span>(milliseconds:<span class="hljs-number">400</span>),(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">${id*<span class="hljs-number">2</span>}</span>"</span>);
  });
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683646115714/b3e3641b-9987-4c66-b72b-4586926468b6.png" alt class="image--center mx-auto" /></p>
<p>My Code Inside forEach was executed after all code runs, What the heck?</p>
<h2 id="heading-why-this-happened-lets-see">Why This Happened? Let's See</h2>
<p>After a Bit of Research, the block of forEach wasn't run at last. forEach was executed at its time not later. What happened was with my Future function.</p>
<p>Array Methods such as forEach execute codes synchronously. That means each time Future is registered in Queue not waited. You cannot wait for Future(promises) inside of arrayMethods.</p>
<p>Behind the scene, it does something like this.</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main(){

<span class="hljs-keyword">final</span> <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; ids = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>];  
 <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Started"</span>);
  <span class="hljs-keyword">for</span>(<span class="hljs-built_in">int</span> element <span class="hljs-keyword">in</span> ids){
    callback();
  }
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Ended"</span>);
}
<span class="hljs-keyword">void</span> callback()<span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">await</span> myDatabase();
}
Future&lt;<span class="hljs-keyword">void</span>&gt; myDatabase()<span class="hljs-keyword">async</span>{
  Future.delayed(<span class="hljs-built_in">Duration</span>(milliseconds:<span class="hljs-number">500</span>),(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"HI"</span>);
  });
}
</code></pre>
<p>So, What's the Solution? Let's Discuss It.</p>
<h1 id="heading-solutions">Solutions</h1>
<h2 id="heading-1-using-basic-loops">1# Using Basic Loops</h2>
<p>Just use forEach, for, while or do while loop when you need to call Future which you want to wait.</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
    <span class="hljs-keyword">final</span> <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; ids = [<span class="hljs-number">1</span>,<span class="hljs-number">5</span>,<span class="hljs-number">3</span>,<span class="hljs-number">7</span>];
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Started"</span>);
    <span class="hljs-keyword">for</span>(<span class="hljs-built_in">int</span> element <span class="hljs-keyword">in</span> ids){
      <span class="hljs-keyword">await</span> getDataAsId(element);
    }
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Ended"</span>);
}

Future&lt;<span class="hljs-keyword">void</span>&gt; getDataAsId(<span class="hljs-built_in">int</span> id)<span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">await</span> Future.delayed(<span class="hljs-built_in">Duration</span>(milliseconds:<span class="hljs-number">400</span>),(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">${id*<span class="hljs-number">2</span>}</span>"</span>);
  });
}
</code></pre>
<h2 id="heading-2-store-future-and-call-all-at-once">#2 Store Future and Call All At Once</h2>
<p>For this solution, we will map all futures. And run all Future at once.</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
    <span class="hljs-keyword">final</span> <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; ids = [<span class="hljs-number">1</span>,<span class="hljs-number">5</span>,<span class="hljs-number">3</span>,<span class="hljs-number">7</span>];
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Started"</span>);
   <span class="hljs-keyword">final</span> <span class="hljs-built_in">List</span>&lt;Future&lt;<span class="hljs-keyword">void</span>&gt;&gt; futures = ids.map((e)=&gt;getDataAsId(e)).toList();
  <span class="hljs-keyword">await</span> Future.wait(futures);
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loop Ended"</span>);
}
Future&lt;<span class="hljs-keyword">void</span>&gt; getDataAsId(<span class="hljs-built_in">int</span> id)<span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">await</span> Future.delayed(<span class="hljs-built_in">Duration</span>(milliseconds:<span class="hljs-number">400</span>),(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">${id*<span class="hljs-number">2</span>}</span>"</span>);
  });
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683649052799/4a4e9e0c-6796-4a35-914a-da6bdb9ce460.png" alt class="image--center mx-auto" /></p>
<p>If you are on Javascript use</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all(futures);
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Flutter State Management: Controversial Topic]]></title><description><![CDATA[Before Jumping into the topic Let's clarify why we need State Management Solution for Our App.
Not Only Mobile Apps and web apps also need State Management. But Webpage Don't. But Why?
In Single Page Applications UI have to change according to certai...]]></description><link>https://newsun.com.np/flutter-state-management-controversial-topic</link><guid isPermaLink="true">https://newsun.com.np/flutter-state-management-controversial-topic</guid><category><![CDATA[State Management ]]></category><category><![CDATA[Flutter]]></category><dc:creator><![CDATA[Newsun]]></dc:creator><pubDate>Sat, 29 Apr 2023 12:20:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1682767927007/bd5fd387-2250-47bb-b410-d21d82b4d612.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before Jumping into the topic Let's clarify why we need State Management Solution for Our App.</p>
<p>Not Only Mobile Apps and web apps also need State Management. But Webpage Don't. But Why?</p>
<p>In Single Page Applications UI have to change according to certain data changes. There all UI changes happen on a single page, but in the case of a webpage, it redirects to another statically created webpage such as .php or .html etc. Single Page Applications are dynamic they change as data changes.</p>
<p>But how State Management Solution comes into the picture for this?</p>
<p>In Simple Terms, State Management is Controller for UI, It handles when UI should rebuild.</p>
<h3 id="heading-what-state-management-solution-do-we-have-in-flutter">What State Management Solution Do We Have in Flutter?</h3>
<ol>
<li><p><strong>setState()</strong></p>
<p> It is provided by Flutter. For Small Projects, it does a great job. When it is called it rebuilds the whole widget.</p>
</li>
<li><p>Provider</p>
<p> The provider is State Management Solution created by Remi Rousselet. By using provider developers have to ability to separate the UI and logic easily. Provider data can be used Anywhere it is not limited to widgets only. It uses an Inherited widget. In short, it depends on the flutter framework.</p>
</li>
<li><p>BloC</p>
<p> Bloc is another State Management Solution for Flutter. It works in Stream, any changes in data immediately rebuild the UI. It is industry standard. Many Developers Prefer Bloc.</p>
</li>
<li><p>Riverpod</p>
<p> Riverpod is Created By the Same Person Who Created Provider. Buy why is need of new package? Because the Provider was dependent on Flutter Framework and it causes issues like the provider not being found. Riverpod is independent from the Flutter framework.</p>
</li>
<li><p>GetX</p>
<p> GetX is widely Popular among beginner Flutter App Developers. It doesn't mean that experienced developer doesn't use GetX. They use it.</p>
</li>
</ol>
<p>So Why there is Fight for Which State Management is Best.</p>
<p>There is no reason to fight over a state management tool. It just an architectural way. You can use any state management tool as you need.</p>
<h2 id="heading-but-dont-use-getx">But Don't Use GetX</h2>
<p>Many Beginner who comes into Flutter most likely use GetX for State Management. Because it makes everything easy to do. You Can Access Context anywhere beginner find it handy but it is bad practice. For Shake for learning it's good but In Production App GetX may cause memory leaks and context issues.</p>
<h2 id="heading-so-what-do-i-prefer">So, What do I prefer</h2>
<p>I prefer Riverpod, It doesn't mean you should also. I recommend you go with Riverpod or BloC. They have good community support. Easy Follow Documentation.</p>
]]></content:encoded></item></channel></rss>