<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Hussein on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Hussein on Medium]]></description>
        <link>https://medium.com/@ahmeddhus?source=rss-bda283543de1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p2hcdkDl5gA18NdJ2VPtRQ.jpeg</url>
            <title>Stories by Ahmed Hussein on Medium</title>
            <link>https://medium.com/@ahmeddhus?source=rss-bda283543de1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 30 May 2026 22:37:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmeddhus/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Pride kills good design]]></title>
            <link>https://medium.com/@ahmeddhus/pride-kills-good-design-63a8f0e085e4?source=rss-bda283543de1------2</link>
            <guid isPermaLink="false">https://medium.com/p/63a8f0e085e4</guid>
            <category><![CDATA[software]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[design-process]]></category>
            <dc:creator><![CDATA[Ahmed Hussein]]></dc:creator>
            <pubDate>Wed, 04 Oct 2023 15:17:58 GMT</pubDate>
            <atom:updated>2023-10-04T15:25:02.023Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of creating computer programs, making decisions about how things should work can be tricky. Recently, I read a famous helpful book called “Head First OOA&amp;D,” and it shared something really important with me.</p><blockquote><strong>death of a design (decision)</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DDqV4UVKH_LoCRf-trAo5A.jpeg" /></figure><p>One big challenge for us developers is accepting that we can make mistakes in our designs. It’s easy to point out problems in other people’s code, but true growth happens when we look at our own code and find areas to improve. This self-reflection is where we become better at coding.</p><h3>Being Open to Change</h3><p>The book teaches us that being open to changing our designs is crucial. Making a better-designed program might mean going back to our code and making improvements. It reminds us that designing things is an ongoing process.</p><h3>Designing is a Do-Over</h3><p>Designing, as the book explains, is a step-by-step process. It’s about creating, checking, and fixing things over and over again. This process applies not only to the code we create but also to the designs we get from other programmers. Being flexible and ready for change is key.</p><h3>Watch Out for Pride:</h3><p>The book warns us about feeling too proud to admit our designs might have issues. If we’re not willing to look at and improve our designs, we might miss opportunities to make them better. Being humble and committed to getting better is important.</p><h3>Conclusion:</h3><p>In the world of software design, learning from our mistakes and improving our designs is a sign of a good developer. The book is like a guide, helping us navigate the complexities of coding and reminding us that we get better by always being ready to learn. So, let’s take on the challenges, learn from our mistakes, and create designs that last.</p><h3>References</h3><p><a href="https://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678">Head First OOA&amp;D book</a></p><p>And that’s it, I would love to hear your feedback about this article or discuss any related topics, so feel free to drop a comment or reach out to me on <a href="https://twitter.com/ahmeddhus">Twitter</a> or <a href="https://www.linkedin.com/in/ahmeddhus/">LinkedIn</a>.</p><p>Thanks for your time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63a8f0e085e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t forget what MdeiaQuery really is]]></title>
            <link>https://medium.com/@ahmeddhus/dont-forget-what-mdeiaquery-really-is-3f2a1ca0e024?source=rss-bda283543de1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f2a1ca0e024</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[flutter-app-performance]]></category>
            <dc:creator><![CDATA[Ahmed Hussein]]></dc:creator>
            <pubDate>Mon, 17 Apr 2023 10:29:30 GMT</pubDate>
            <atom:updated>2023-04-17T10:29:30.539Z</atom:updated>
            <content:encoded><![CDATA[<p>As developers, we strive to build high-performance applications that are both efficient and fast. One tool in our arsenal is the <a href="https://api.flutter.dev/flutter/widgets/MediaQuery-class.html">MediaQuery</a> widget, which can provide information about a device&#39;s screen size and orientation.</p><p>However, sometimes we forget what MediaQuery really is.</p><p>but before that, I want to display an experiment by <a href="https://medium.com/u/5047ab0d90a1">Christian Findlay</a> which he shared on a Twitter <a href="https://twitter.com/CFDevelop/status/1621276270930841600">thread</a>.</p><p>He was trying to calculate how many Page intense created and how many time build() method calls while resizing the screen widget, so he added aText widget to display that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*0kHmyFqxO8bb6pDPqXTP9w.gif" /></figure><p>So we can see here that the widget rebuilds many times because the build() method is calling <strong>every time</strong> the screen width changes.</p><p>Then he removes MediaQuery from Text widget and tried the same thing,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*dYOOAESklTWtdc4Mccxm1Q.gif" /></figure><p>But what we find here is the widget is not rebuilding, the build() method only called once in the beginning.</p><p>So why this happens?</p><h3>MediaQuery and rebuilds</h3><p>So, Let’s go back to when we were talking about what we forget about MediaQuery.</p><blockquote><strong>MediaQuery </strong>is a <strong>InheretedWidget.</strong></blockquote><p>Sometimes we forget about that. So they may use MediaQuerycarelessly which can negatively impact the performance of your application.</p><p>MediaQuery is an <a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html">InheretedWidget</a>means that it rebuilds all its child widgets whenever the device screen characteristics change. This is important to keep in mind because if you use MediaQuery frequently, you risk causing unnecessary rebuilds that can slow down your app.</p><p>And that makes sense why the widget is rebuilding many times when we add MediaQuery, isn’t it?</p><p>but why not when we don’t use MediaQuery while the screen size is changing? Is it any change on the screen triggers the build() method?<br>No.</p><h3>Resizing and no rebuilds</h3><p>The reason why there’s no call for the build() method while resizing the screen is, we’re getting confused between the layout phase and the build phase.</p><p><strong>The build phase</strong> is when the Flutter framework calls the build method of a widget. In this phase, the widget is responsible for returning a tree of elements that represent the user interface. This tree can include other widgets, containers, text, images, or anything else that is part of the UI.</p><p>On the other hand, <strong>the layout phase</strong> is when the framework determines the size and position of each element in the tree. The framework takes the tree of elements created during the build phase and traverses it to calculate the position and size of each element, which is then used to render the UI on the screen.</p><h3>In Summary</h3><p>MediaQuery is a powerful widget that can help create responsive and adaptable user interfaces in Flutter. However, it is important to note that it requires some computing power to calculate the device screen characteristics. If you use MediaQuery too frequently, you risk causing unnecessary computational overhead that can slow down your app.</p><p>Besides that, it’s important to understand the difference between the build phase and the layout phase in Flutter. By understanding the widget lifecycle and following best practices, we can write efficient and performant Flutter apps.</p><h3>References</h3><ul><li><a href="https://docs.flutter.dev/resources/inside-flutter#sublinear-layout">Inside Flutter</a></li><li><a href="https://twitter.com/CFDevelop/status/1621276270930841600">Christian Twitter thread</a></li><li><a href="https://www.youtube.com/watch?v=A3WrA4zAaPw&amp;ab_channel=Flutter">MediaQuery (Flutter Widget of the Week)</a></li></ul><p>Thanks to <a href="https://medium.com/u/5047ab0d90a1">Christian Findlay</a> for bringing this great discussion to us and thanks to <a href="https://medium.com/u/b4634982fdd0">Gordon Hayes</a> for sharing this info. with us.</p><p>And that’s it, I would love to hear your feedback about this article or discuss any related topics, so feel free to drop a comment or reach out to me on <a href="https://twitter.com/ahmeddhus">Twitter</a> or <a href="https://www.linkedin.com/in/ahmeddhus/">LinkedIn</a>.</p><p>Thanks for your time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f2a1ca0e024" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Single-Threaded that, Isolates this]]></title>
            <link>https://medium.com/@ahmeddhus/single-threaded-that-isolates-this-43971c7ecf24?source=rss-bda283543de1------2</link>
            <guid isPermaLink="false">https://medium.com/p/43971c7ecf24</guid>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[isolate]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[Ahmed Hussein]]></dc:creator>
            <pubDate>Tue, 24 Jan 2023 15:00:02 GMT</pubDate>
            <atom:updated>2023-01-25T12:06:14.895Z</atom:updated>
            <content:encoded><![CDATA[<p>If you came by Dart or Flutter, definitely you heard about single-threaded and isolates before.</p><p>According to dart <a href="https://dart.dev/guides">doc</a>,</p><blockquote>Dart is a single-threaded language.</blockquote><blockquote>Within an app, all Dart code runs in an <em>isolate.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*EfCBkAIBeTEpcTKAjwgj3Q.png" /></figure><p>You might be wondering that if Dart is a single-threaded language how can we handle heavy tasks or long-running operations that could block the main thread and due to this the app is not responding? How could I deal with that?</p><p>That’s what we will discuss further.</p><h3>What will we discuss?</h3><p>1- What isolates are.</p><p>2- The differences between Thread and Isolates.</p><p>3- What the thread inside the isolate is and how it works.</p><p>4- How isolates communicate with each other.</p><p>5- Benefits of locking access between isolates.</p><p>6- Multi-threading in Dart and examples.</p><h3>What are Isolates</h3><p>An isolate is what all Dart code runs in. It’s like a little space on the machine with its own, private chunk of memory and a single thread running an event loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*J_ksufIwJ5G6wwf8U1hU7Q.png" /><figcaption>An Isolate has its own memory and a thread runs an event loop.</figcaption></figure><p>If you’re working with other languages like C++ for example, It might be confusing about the difference between isolates and threads. So before we jump into any details about isolates, let’s first discuss the difference between Threads and Isolates.</p><h3>Threads vs. Isolates</h3><p>They are both mechanisms that allow you to run code in parallel on separate execution contexts with some key differences:</p><p>Threads: are <strong>low-level</strong> mechanisms that run on the same operating system as the main thread. and they are managed by the operating system. Threads <strong>share the same memory</strong> and can communicate with each other through shared variables, etc.</p><p>Isolates: are <strong>high-level</strong> mechanisms that run independently from the main thread and they are managed by the Dart runtime. Isolates have <strong>their own memory</strong> and communicate with the main thread or each other only through <strong>message passing.</strong></p><p>Okay, we mention above that every isolate has a thread that runs an event loop, and isolates communicate with each other with only message passing. So what are this event loop and message passing how do they work?</p><h3>Event loop in isolate</h3><p>Just like <a href="https://medium.com/@kathyw_39223">Kathy</a> said,</p><blockquote>It’s what makes really asyncronous code possible.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/1*DXbcejE8mNqJOqqfP0swrg.gif" /><figcaption>Event loop waiting for any events</figcaption></figure><p>While the application is running on that single isolate, the<strong> Event loop </strong>inside that isolate keeps running the whole time the app is running.</p><p>The event loop just waits for any events to process just as tapping, downloading, or any other event.</p><p>And of course, we can have more than one isolate if we need it but how they can communicate with each other? by Message Passing…</p><h3>Message Passing</h3><p>Every isolate has its own <strong>isolated</strong> memory, and can only communicate by message passing back and forth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*cS29xdbyWsirPva6NL2fcg.png" /><figcaption>Message passing back and forth between two isolates</figcaption></figure><p>Every isolate sends a message to the other isolate and the other one receives and processes it using the event loop we talked about above.</p><p>If you are common with threads, you might be confused about separating memory and lack of info. between isolates, so what are the benefits of that?</p><h3><strong>Benefits of locking access between isolates</strong></h3><p>First, let’s talk about the benefits of locking access between isolates and only passing messages between them:</p><ol><li><strong>Isolation:</strong> Isolates run in their own memory space means that they can’t directly access each other’s memory, which helps to prevent bugs and data corruption caused by the shared state.</li><li><strong>Concurrency:</strong> Because isolates don’t share a memory, they can work on different tasks concurrently without interfering with each other. This allows you to take full advantage of multicore processors to improve the performance of your application.</li><li><strong>Scalability:</strong> Isolates are lightweight and can run concurrently, so it’s easy to add more of them to your application as needed. This makes it easy to scale your application up to handle more traffic or data.</li><li><strong>Fault Tolerance:</strong> Because isolates are independent and don’t share a memory, it’s easier to build fault-tolerant systems. If an isolate crashes, it doesn’t affect the other isolates.</li><li><strong>Security:</strong> Because isolates are separate and can’t directly access each other’s memory, it’s more difficult for an attacker to exploit a vulnerability in one isolate to gain access to sensitive data in another isolate.</li><li><strong>Simplicity:</strong> By passing messages between isolates instead of sharing memory, you can keep your code simple and avoid many of the complexities of shared-memory concurrency.</li></ol><h3>Multi-threading in Dart</h3><p>We can accomplish Multi-threading in Dart by using isolates. Okay, enough talk we need examples...</p><p>To use isolates in Dart, you can use the Isolate class, which provides methods for creating and controlling isolates. Here is an example of how you can use an isolate to perform a task in parallel with the main thread:</p><pre>import &#39;dart:isolate&#39;;<br><br>void main() {<br>  // Create the isolate<br>  Isolate.spawn(calculateSum, [1, 2, 3, 4, 5]);<br><br>  // Perform other tasks on the main thread<br>  print(&quot;Main thread is doing something else...&quot;);<br>}<br><br>void calculateSum(List&lt;int&gt; numbers) {<br>  var sum = numbers.reduce((a, b) =&gt; a + b);<br>  print(&quot;Sum: $sum&quot;);<br>}</pre><p>In this example, the Isolate.spawn method is used to create a new isolate and run the calculateSum function on it. The main thread continues to run and perform other tasks while the isolate is running. You can also pass data to the isolate using the second parameter of the Isolate.spawn method, as shown in the example.</p><p>You can also use compute functions from dart:ui library to run a function in an isolate, it&#39;s an easy way to run a function in an isolate and get the result back.</p><pre>import &#39;dart:ui&#39;;<br><br>void main() {<br>    compute(longRunningTask, 1000000).then((result){<br>        print(result);<br>    });<br>}<br><br>int longRunningTask(int x) {<br>    int sum = 0;<br>    for (int i = 0; i &lt; x; i++) {<br>        sum += i;<br>    }<br>    return sum;<br>}</pre><p>All in all, isolates provide an effective way to handle concurrency and parallelism in your code and it’s one of the core features of Dart and Flutter, it makes it easy to build high-performance, concurrent, and scalable systems with minimal complexity.</p><p>I have to mention that, I really recommend watching this “Flutter in Focus” <a href="https://youtu.be/vl_AaCgudcY">video</a> or reading this <a href="https://medium.com/dartlang/dart-asynchronous-programming-isolates-and-event-loops-bffc3e296a6a">article</a> about isolates and event loops and of course taking a look at <a href="https://dart.dev/guides/language/concurrency">Concurrency in Dart</a> doc.</p><p>After the #Flutter 3.7 release, I have to mention that developers can use Plugins and Platform Channels from <em>any</em> isolate. read <a href="https://medium.com/flutter/introducing-background-isolate-channels-7a299609cad8">this</a> for more info.</p><p>And that’s it, I would love to hear your feedback about this article or discuss any related topics, so feel free to drop a comment or reach out to me on <a href="https://twitter.com/ahmeddhus">Twitter</a> or <a href="https://www.linkedin.com/in/ahmeddhus/">LinkedIn</a>.</p><p>Thanks for your time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43971c7ecf24" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[‘addPostFrameCallback’ vs. ‘endOfFrame.then’ in WidgetsBinding with Rendering.]]></title>
            <link>https://medium.com/@ahmeddhus/addpostframecallback-vs-endofframe-then-in-widgetsbinding-with-rendering-a8ed5d527669?source=rss-bda283543de1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8ed5d527669</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <category><![CDATA[flutter-ui]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <dc:creator><![CDATA[Ahmed Hussein]]></dc:creator>
            <pubDate>Mon, 09 Jan 2023 08:00:10 GMT</pubDate>
            <atom:updated>2023-01-23T14:37:07.120Z</atom:updated>
            <content:encoded><![CDATA[<p>as a Flutter developer, I think—at least once — you decided to do some work in <a href="https://api.flutter.dev/flutter/widgets/State/initState.html">initState(</a>) which needs <a href="https://api.flutter.dev/flutter/widgets/BuildContext-class.html">BuildContext</a>.</p><p>Therefore, we do our work inside the WidgetsBinding.instance.addPostFrameCallbackcallback or WidgetsBinding.instance.endOfFrame.then so we use <a href="https://api.flutter.dev/flutter/widgets/BuildContext-class.html">BuildContext</a> safely.</p><h3>So what’s the difference?</h3><p>And how does this relate to the rendering process?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D2ZgIXJVbXEtYznE4B45lQ.png" /></figure><h3>WidgetsBinding</h3><p>the <a href="https://api.flutter.dev/flutter/widgets/WidgetsBinding-mixin.html">WidgetsBinding</a> class provides us access to the current state of the widgets system or as the <a href="https://api.flutter.dev/flutter/widgets/WidgetsBinding-mixin.html">flutter docs</a> say,</p><blockquote>The glue between the widgets layer and the Flutter engine.</blockquote><p>through the WidgetsBinding instance, we can access “<a href="https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html">addPostFramCallback</a>” and “<a href="https://api.flutter.dev/flutter/scheduler/SchedulerBinding/endOfFrame.html">endOfFrame</a>.then”.</p><p>“addPostFramCallback” and “endOfFrame.then” both can be used to schedule a callback to be executed after the rendering phase to the current frame ends.</p><p>But before showing the difference between them, we need first to define the “rendering phase”.</p><h3>Rendering Phases</h3><p>in #Flutter, rendering goes through four phases and each one has a specific purpose:</p><ol><li>Layout phase: During the layout phase, the dimensions and positions of the widgets are calculated. it ensures that the widgets are properly positioned and sized on the screen.</li><li>Painting phase: During the painting phase, the widgets are painted on the screen using the dimensions and positions calculated during the layout phase. The painting phase involves traversing the widgets tree and calling the paint method of each widget.</li><li>Compositing phase: The compositing phase takes the painted widgets and combines them into a single image that can be displayed on the screen.</li><li>Rasterizing phase: in this final phase, the engine receives the updated layer tree containing the drawing instructions and converts them into pixels. in other words, the scene is displayed on the screen as a matrix of pixels.</li></ol><p>For more deep info about rendering in Flutter, check this <a href="https://www.youtube.com/watch?v=UUfXWzp0-DU&amp;ab_channel=GoogleTechTalks">talk</a> by <a href="https://www.adambarth.com/"><em>Adam Barth</em></a>.</p><p>Now, let’s jump back to our main topic…</p><h3>‘addPostFrameCallback’ vs. ‘endOfFrame.then’</h3><p>As we said before,</p><blockquote>“both can be used to schedule a callback to be executed after the rendering phase to the current frame ends”</blockquote><p><strong>However, </strong>they differ in terms of when the callback is executed in relation to the rendering phases.</p><ul><li>“addPostFramCallback”: adds a callback that is executed immediately after the “rendering phase” ends but before the engine starts the “layout phase”. This method is useful for tasks that need to be performed after the widgets have been painted, but before the layout is calculated.</li><li>“endOfFrame.then”: returns a future that completes after the “rendering phase” ends, but after the “layout phase” of the first frame has started. This method is useful for tasks that need to be performed after the widgets have been painted and the layout has been calculated.</li></ul><p>Each method has its own purpose to fulfill your needs.</p><p>And that’s it, I would love to hear your feedback about this article or discuss any related topics, so feel free to drop a comment or reach out to me on <a href="https://twitter.com/ahmeddhus">Twitter</a> or <a href="https://www.linkedin.com/in/ahmeddhus/">LinkedIn</a>.</p><h3>References:</h3><ul><li><a href="https://api.flutter.dev/flutter/widgets/WidgetsBinding-mixin.html">https://api.flutter.dev/flutter/widgets/WidgetsBinding-mixin.html</a></li><li><a href="https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html">https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html</a></li><li><a href="https://api.flutter.dev/flutter/scheduler/SchedulerBinding/endOfFrame.html">https://api.flutter.dev/flutter/scheduler/SchedulerBinding/endOfFrame.html</a></li><li><a href="https://www.youtube.com/watch?v=54yoCqkew6g&amp;ab_channel=Flutter">https://www.youtube.com/watch?v=54yoCqkew6g&amp;ab_channel=Flutter</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8ed5d527669" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>