<?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 Asil Ahmad Naji on Medium]]></title>
        <description><![CDATA[Stories by Asil Ahmad Naji on Medium]]></description>
        <link>https://medium.com/@naseel614?source=rss-410f8a0e7f28------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sDRcm4LjYM_9LTD1x0FQfA.jpeg</url>
            <title>Stories by Asil Ahmad Naji on Medium</title>
            <link>https://medium.com/@naseel614?source=rss-410f8a0e7f28------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 04:02:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@naseel614/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[Dart’s error handling is improved with any type.]]></title>
            <link>https://medium.com/@naseel614/darts-error-handling-is-improved-with-any-type-d88a58b5ef4c?source=rss-410f8a0e7f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/d88a58b5ef4c</guid>
            <dc:creator><![CDATA[Asil Ahmad Naji]]></dc:creator>
            <pubDate>Sat, 27 May 2023 16:57:09 GMT</pubDate>
            <atom:updated>2023-05-27T16:57:09.674Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever wondered if a wrapper class existed that could include both positive and negative responses in a single type? Let me explain why it’s a useful feature if you’ve never wondered why.</p><p>Let’s consider we have these two classes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*u7axgACCZI8N9wLtCSHPrA.png" /></figure><p>When an exception occurs, we return the Failure class.<br>When we receive a successful answer, we want to return the Asil class as the model class.</p><p>Let’s say we want to wrap both of them in a single class, how can we do that? Here’s a very naive approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*NGAwEvsCbiIeVushfjshYw.png" /></figure><p>Here’s how we can use it,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Jnv5kirUfyFcAoEVvK87Q.png" /></figure><p>This class is cool. What’s the problem with this🙄? Let us understand</p><p>The Asil and failure properties may both be triggered simultaneously for the response object. Clearly, we do not wish to act in this way. Either an Asil or a failure need to fit within. They shouldn’t both be there at the same time.</p><pre>Response res = Response(failedReponse);<br>res.person = validResponse; //now &#39;res&#39; has both person and failure</pre><ul><li>You may forget to check the error.</li></ul><pre>void main(){<br>    Failure failedResponse = Failure(&#39;something went wrong&#39;);  <br>    Response res = Response(failedReponse);    //still valid, compiler doesn&#39;t complain, but prints &#39;null&#39;<br>    print(&#39;Response : ${res.person?.name}&#39;);<br>}</pre><p>So, something is required.</p><p><strong><em>Ensure that only one kind is accessible at any one moment.<br>to compel error handling (to make the compiler protest).</em></strong></p><p>The Dartz Package<br>Dart lacks a built-in type to accomplish this, but we do have dartz, a functional programming package in Dart. It supports the type EitherLeft, Right&gt;, which is exactly what we need.</p><p>To begin, include the package in your pubspec.yaml file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*-BFIGjinaIX84NitEjkoaw.png" /></figure><p>dependencies:<br> dartz: ^0.9.2</p><blockquote>What exactly is the Either Type?<br>The Either type is used to express a value that can be of either of the two kinds given. It is typical to see either used to indicate a success or failure value; however, this is not always the case.</blockquote><p>Either&lt;int, String&gt; response = Right(“Hello, im right”);</p><p>This is how simple it is to use Either(), you declare the Left and right types inside the generic type parameters in the respective order.</p><p>To assign value use Left(value) or Right(value) . In the above code, I specified <em>Right(“Hello, im right”)</em> to put the String as the Right value, in this case, the Left value will be empty (the integer value is empty).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BmRiRlc2Fbn1r0X7t9ikWA.png" /></figure><blockquote><em>The fold method here is responsible for handling both the values, if the left value is available, it will call the first callback method </em><em>print(‘Integer : $integerValue’) , otherwise, it will call the second callback</em><em> print(‘String : $stringValue’)</em></blockquote><p>This is exactly what we wanted, we want to have</p><p>At any one time, only one of the values is available.<br>Corresponding actions should be made only when the availability of both the Left and Right values has been determined.</p><h3>Handling Errors with Either with steps :</h3><p>Step 1: Define an Either type</p><p>First, you need to define an Either type that represents a value that can be either a success (Right) or an error (Left). This type allows you to encapsulate the possibility of errors in your computations.</p><p>Step 2: Define an error type</p><p>Next, define an error type that represents the possible errors that can occur in your application. This could be an enum or a class, depending on your requirements. For example, you might have an AppError enum with different error cases such as networkError, serverError, authenticationError, etc.</p><p>Step 3: Wrap functions returning Either</p><p>Wrap functions that can potentially throw errors in a function that returns Either with the error type and the desired success type. This allows you to handle errors explicitly and separate error flows from the normal program execution flow. Typically, these functions will perform operations such as making API requests, accessing databases, or performing complex computations.</p><p>Step 4: Process the result in your UI</p><p>When you call a function that returns Either, you can pattern match on the result to handle the success and error cases separately. This allows you to handle errors gracefully and provide appropriate feedback to the user. In the error case, you can display an error message, retry the operation, or take any other necessary action. In the success case, you can process the returned data or perform any other required actions.</p><p>By following these steps, you can implement error handling using Either in a functional programming style in your Flutter application. This approach helps you explicitly handle errors, separate error flows from normal flows, and provide a more robust and maintainable codebase.</p><h3>Using Either for error handling in Flutter offers several benefits:</h3><ol><li>Explicit handling of errors: Either forces you to explicitly handle both the success and error cases. By pattern matching on the result, you ensure that errors are not overlooked or ignored, promoting robustness in your code.</li><li>Separation of error flows: With Either, you can separate the error flows from the normal program execution flow. This helps in keeping your code organized and allows you to handle errors in a focused and controlled manner.</li><li>Improved code readability: By using Either, your code becomes more readable and self-explanatory. The type signature of functions returning Either clearly indicates the possibility of errors, making it easier for other developers to understand and reason about the codebase.</li><li>Forced error handling discipline: Either enforces a disciplined approach to error handling. It encourages you to handle errors explicitly, making it less likely for errors to go unnoticed or lead to unexpected program behavior.</li><li>Clear error type definition: By defining an error type, you have a centralized place to represent and categorize different types of errors that can occur in your application. This promotes consistency and allows for better error handling strategies.</li><li>Testing and refactoring: Either makes testing and refactoring easier. Since errors are explicitly handled, you can write targeted tests for error cases and refactor code without worrying about hidden error paths.</li><li>Composability and functional programming benefits: Either is a monadic type that offers composability and works well with other functional programming concepts. You can chain operations using functional combinators like map, flatMap, and fold, enabling you to handle complex error scenarios with ease.</li></ol><h3>conclusion:</h3><p>using Either for error handling in Flutter provides several advantages. By leveraging this functional programming technique, you achieve explicit handling of errors, separate error flows from normal program execution, improve code readability, enforce error handling discipline, and promote testing and refactoring. Additionally, Either enables clear error type definition, offers composability and functional programming benefits, and ultimately leads to more robust and maintainable code. Overall, using Either enhances error handling in your Flutter application, resulting in a better user experience and a more reliable software system.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d88a58b5ef4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Code | Chapter(3) | Functions]]></title>
            <link>https://medium.com/@naseel614/clean-code-chapter-3-functions-4df262fb36b3?source=rss-410f8a0e7f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/4df262fb36b3</guid>
            <dc:creator><![CDATA[Asil Ahmad Naji]]></dc:creator>
            <pubDate>Sat, 18 Mar 2023 11:00:54 GMT</pubDate>
            <atom:updated>2023-03-18T11:00:54.644Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ELXCcHoN_BywnO7E6br5jQ.png" /></figure><p>The guidelines that follow are about the mechanics of writing properly. If you adhere to these guidelines, your functions will be brief, well-named, and well-organized.</p><h3>1)Fnctions should be small</h3><ul><li>Should be at maximum 20 lines long.</li><li>The blocks within if, else, while, and so on should be one line long. Probably that line should be a function call.</li><li>The nested structures (blocks) should be one or two at maximum.</li><li>See Listing 3–1 how it’s refactored to be Listing 3–2, and then refactored again to be shorten to Listing 3–3.</li></ul><p>Listing 3–1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_HT09X_zq0aExBndLwllwg.png" /></figure><p>Listing 3–2 (refactored)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-SwXGxoFQbqdQeof6kYrQ.png" /></figure><p>Listing 3–3 (refactored)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Vlx_mnKuo3mX8qWpIXoNg.png" /></figure><p>In Listing 3–2, the if/else block has been refactored to a ternary operator, and the repeated print statements have been replaced with a single print statement that uses string interpolation to include the address.</p><p>In Listing 3–3, the function has been further shortened to a one-line arrow function that uses string interpolation and a null-coalescing operator to handle the case where the address is null.</p><h3>2)Functions should do one thing</h3><ul><li>Functions should do one thing; they should do it well; they should do it only.</li><li>Extract sub functions and classes until it’s not possible.</li></ul><h3>3) One level of abstraction per function:</h3><ul><li>Each function or method should have a clear and well-defined purpose. It should do one thing and do it well. This means that the function should operate at a single level of abstraction.</li><li>Functions that operate at a high level of abstraction should have descriptive names that convey their purpose. For example, a function that performs a complex operation on a set of data should have a name that reflects its purpose, such as processData() or computeResults().</li><li>Functions that operate at a low level of abstraction should also have descriptive names that reflect their purpose. For example, a function that converts a string to a number should have a name like parseNumber() or convertToInt().</li><li>Functions that operate at a higher level of abstraction should call other functions that operate at a lower level of abstraction. This helps to keep the code modular and easy to read, as well as reducing the overall complexity of the codebase.</li><li>Functions should be written in a way that is easy to understand and maintain. This means using clear and concise code, avoiding unnecessary complexity, and following best practices for code organization and formatting.</li></ul><h3>4) Switch Statements:</h3><p>Switch statements are often used to perform different actions based on the value of a variable or expression. While they can be useful in some cases, they can also lead to code that is difficult to understand and modify</p><ul><li>One issue with switch statements is that they tend to be verbose and repetitive, especially when there are many cases to handle. This can make code harder to read and understand, especially if the cases are not well organized or clearly defined.</li><li>Another issue with switch statements is that they can be difficult to modify if new cases need to be added or existing cases need to be changed. This can result in code that is brittle and hard to maintain over time.</li><li>Instead of using switch statements, it is often better to use polymorphism or other object-oriented techniques to handle different cases. This can lead to cleaner and more modular code that is easier to understand and modify.</li><li>Use them only when necessary.</li><li>Do not repeat them =&gt; use <a href="https://stackify.com/oop-concept-polymorphism/">Polymorphism</a> and the <a href="https://refactoring.guru/design-patterns/abstract-factory">Abstract Factory</a> pattern</li></ul><h3>5) Use Descriptive Names:</h3><ul><li>A long descriptive name is better than a short enigmatic name.</li><li>Don’t be afraid to spend time choosing a name.</li><li>Hunting for a good name may result in a favorable restructuring of the code.</li></ul><h3>6) Function Arguments:</h3><p>Functions should have a small number of arguments. Too many arguments can make functions difficult to read and understand, and can also make them harder to modify and maintain over time.</p><ul><li>The ideal number of arguments for a function is zero.</li><li>Three arguments should be avoided as possible.</li><li>More than three requires a special justification.. and shouldn’t be used anyway.</li></ul><h3>7)Have No Side Effects:</h3><ul><li>Function should do one thing, but it also does other hidden things =&gt; bugs!</li></ul><h3>8) Command Query Separation:</h3><p>CQS states that functions should be either commands or queries, but not both. A command changes the state of the system but does not return a value, while a query returns a value but does not change the state of the system.</p><ul><li>By separating commands and queries, you can make your code easier to understand and modify. It also makes it easier to test your code, since you can test commands and queries separately.</li><li>To implement CQS, you should avoid functions that have side effects, since these can change the state of the system. Instead, side effects should be encapsulated in separate functions or methods.</li><li>You should also avoid functions that return values and change the state of the system at the same time, since this can lead to unexpected behavior and bugs.</li></ul><h3>9) Prefer Exceptions over Returning Error Codes:</h3><p>Exceptions are a mechanism for handling errors that occur during the execution of a program. They allow you to handle errors in a structured and consistent way, and can help make your code more robust and reliable.</p><ul><li>Returning error codes from functions can make your code harder to read and understand, since you need to check for error codes after every function call. This can also lead to more error-prone code, since it’s easy to forget to check for error codes or to mishandle them.</li><li>In Dart, you can use the throw keyword to throw an exception when an error occurs. You can also use the try-catch block to handle exceptions and take appropriate actions based on the type of exception that was thrown.</li><li>By using exceptions instead of error codes, you can simplify your code and make it easier to read and understand. Exceptions allow you to handle errors in a centralized and consistent way, which can help reduce errors and improve the quality of your codebase.</li></ul><p>However, it’s important to use exceptions judiciously, and not to overuse them. Exceptions can be expensive in terms of performance, so you should avoid using them in situations where they are not necessary or where other error-handling mechanisms would be more appropriate.</p><h3>10) Don’t Repeat Yourself:</h3><ul><li>Eliminate duplication to have only one source of truth and one place to change.</li></ul><h3>11) Structure Programming:</h3><ul><li>Structured programming is based on the idea of breaking down a program into smaller, simpler parts, each of which performs a specific task. This makes the program easier to understand, test, and maintain.</li><li>In Dart, you can use structured control flow constructs such as if-else statements, for and while loops, and switch statements to write structured code. These constructs help to create code that is easier to read and understand.</li><li>Another key aspect of structured programming is the use of structured error handling. In Dart, you can use the try-catch statement to handle exceptions in a structured way, and to prevent errors from propagating up the call stack.</li></ul><blockquote><em>Programmers think of systems as stories to be told rather than programs to be written.</em></blockquote><blockquote><em>The class stresses the importance of writing jobs that are small, focused, and have one responsibility. It also highlights the importance of using meaningful names, minimizing arguments, and separating commands and queries.In addition, the chapter discusses the importance of properly handling errors and exceptions, as well as the use of structured programming to write clean and maintainable code. The chapter also provides several techniques for testing and refactoring functions to improve their quality and maintainability.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4df262fb36b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Chapter 2 of Clean Code: A Handbook of Agile Software Craftsmanship]]></title>
            <link>https://medium.com/@naseel614/chapter-2-of-clean-code-a-handbook-of-agile-software-craftsmanship-bc2c5c9e694b?source=rss-410f8a0e7f28------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc2c5c9e694b</guid>
            <dc:creator><![CDATA[Asil Ahmad Naji]]></dc:creator>
            <pubDate>Fri, 17 Mar 2023 14:25:53 GMT</pubDate>
            <atom:updated>2023-03-17T14:25:53.437Z</atom:updated>
            <content:encoded><![CDATA[<p>The following 15 guidelines can help you come up with appropriate names for any object in your software, including variables, functions, arguments, classes, files, and directories.</p><h3>1) Use Intention-Revealing Names:</h3><p>Names should reveal the intent of the code: The name of a variable or function should describe what it does or represents. It should be clear and concise, so that other programmers can quickly understand its purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GQVClXGekdt1QytGYGnd1w.png" /></figure><h3>2) Avoid Dis-information:</h3><p>Avoid using meaningless names, such as single letters or numbers, or names that are too generic, such as “qty” or “info.” These names don’t provide any context or clues about what the code is doing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8kn1jt-Yx2kBAHQjVw3z_g.png" /></figure><h3>3) Make Meaningful Distinctions:</h3><p>In this code, the original implementation of processNumbers used number series naming (a1, a2, a3) to refer to the values in the input list. These names are non-informative and provide no meaning, which can make the code harder to read and understand.</p><p>To make the variable names more meaningful, the code was revised to use descriptive names based on the position of the numbers in the list (firstNumber, secondNumber, thirdNumber). This makes the code more readable and easier to understand, which is especially important if someone else needs to work with the code in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jGv6yVVjVeFdjpnRtsKt7A.png" /></figure><h3>4) Use Pronounceable Names:</h3><p>In this example, the variables with pronounceable names are customerName, generationTimestamp, and modifyTimestamp. The names of these variables consist of pronounceable words, making them easier to read and understand.</p><p>On the other hand, the variables with non-pronounceable names are dtarcrd102, genymdhms, and modymdhms. These names are difficult to pronounce and don’t convey any meaning about what the variables represent.</p><p>by using pronounceable names, the code becomes more readable and maintainable, making it easier for developers to understand and modify the code in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jGv6yVVjVeFdjpnRtsKt7A.png" /></figure><h3>5) Use Searchable Names:</h3><p>In this example, the variables with searchable names are userName, maxAttempts, and accountBalance. These names are descriptive and make it easy to understand what the variables represent. They are also searchable, making it easy to locate the variables within the codebase.</p><p>On the other hand, the variables with non-searchable names are a, b, and c. These names are not descriptive and do not provide any information about what the variables represent. They are also not searchable, which can make it difficult to locate the variables within the codebase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*echFgt0NZBWmz8aSdjgj0A.png" /></figure><h3>6) Method Names:</h3><ul><li>Should be verb or verb phrase like postPayment, deletePage, or save.</li></ul><h3>7) Class Names:</h3><p>Classes and objects should have noun phrase names like Customer, WikiPage, Account, AddressParser.</p><p>-Avoid names like Manager, Processor, Data, Info in the class name.</p><ul><li>A class name should not be a verb.</li></ul><h3>8) Avoid Encoding:</h3><p>In this example, the variable names firstName, lastName, and age are examples of names that do not use encoding. They are clear and concise, and they describe what the variable represents.</p><ul><li>The non-avoid encoding examples are firstName_v1, lastName_v1, and age_v1 (Member Prefix:- avoid prefix member variables with V1_ anymore.)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DITZOfR6EEekZS03-nWFcQ.png" /></figure><h3>9) Don’t be Cute:</h3><h3>10) Pick One Word per Concept:</h3><p>The principle states that for each concept in our code, we should choose a single word to represent that concept and use it consistently throughout our codebase. This means that we should avoid using different words to represent the same concept and using the same word to represent different concepts.</p><h3>11) Don’t Pun:</h3><p>The principle states that we should choose words that are unambiguous and clearly convey the intended meaning of the code. This means that we should avoid using puns, jokes, or clever wordplay that could be confusing or misleading to other developers who work with the code.</p><p>For example, using a variable named i to represent the index of an array might be common, but it’s not clear what i means without additional context. A better name would be something like index or arrayIndex, which more clearly convey the purpose of the variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WIpS_7TioJQBRwBr19oGsA.png" /></figure><h3>12) Use Solution Domain Names:</h3><p>refers to using names that reflect the problem domain, rather than the technical implementation details, in the code.</p><p>In other words, the names used for variables, classes, functions, and other code constructs should be chosen based on the language and concepts of the problem domain that the code is trying to solve rather than the programming language or technical implementation details.</p><p>Remember that the people who read your code will be programmers. So it’s valid to use computer science (CS) terms, algorithm names, pattern names, math terms, and so on. It is not practical to generate every name from the problem domain because we don’t want our programmers to have to run back and forth to the customer asking what every name means while they already know the concept by a different name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qz44EQrSvlb3PVfO20bRrg.png" /></figure><h4>In this code, we have used short, cryptic variable names like ’n’, ‘q’, and ‘p’ to represent the name, quantity, and price of each item in the cart. These names do not provide any context or meaning to someone who is not familiar with the technical details of the implementation.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*-MZevP1I8g_Symigo-F-jw.png" /></figure><p>In this code, we define a ShoppingCartItem class with three properties: name, quantity, and price. We’ve also used these same names in the cart list to represent the items in the cart. This code uses solution domain names because the variable names provide context and meaning that goes along with the problem scope (ie the shopping cart items. This makes the code more readable, understandable, and maintainable, especially for people who aren’t familiar with the technical details of the implementation.</p><h3>13) Use Problem Domain Names:</h3><p>Create the name from the issue domain when there are no programming terminology for what you’re doing. The programmer who looks after your code can at least ask a subject-matter specialist what it means.</p><h3>14) Add Meaningful Context:</h3><p>refers to the idea that code should be written in a way that provides clear and concise information about its purpose and usage, without requiring the reader to look elsewhere for context.</p><p>The goal of adding meaningful context is to make the code more readable and easier to understand, especially for other developers who may need to work on the code in the future. Good code should be self-explanatory and require minimal additional documentation to understand.</p><h3>15) Don’t Add Gratuitous Context:</h3><p>meaning that code should be written in a way that is clear and concise, without adding extra or unnecessary context that can lead to confusion or clutter.</p><p>Adding unnecessary context can make code harder to read and understand, and can also increase the risk of errors if the additional context is misleading or incorrect.</p><p>It is pretty easy to add context to things that are already clear. For example, in a class called “Car” you don’t need to name the color field of the class “carColor” or “colorCar.” You are already in the car class, and all information from this class is related to “Car”.</p><h3>Conclusion</h3><p>Finding good names inside an application can be a hard and time consuming thing. It is not simple to find good names. Because of this, you should invest time in finding and using good names.</p><p>Last words</p><p>A clever programmer and a professional programmer differ in that the latter is aware that simplicity is paramount. Professionals produce easy-to-understand code and put their abilities to good use.</p><p><em>Thank you for reading my essay, but I’m working hard to share my findings from this book (you must be a programmer who cares about his health because I’m writing this for you). I swapped 5 cups of coffee for 1 cup of coffee and the remainder is wat</em>er.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc2c5c9e694b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>