<?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 Drioueche Mohammed on Medium]]></title>
        <description><![CDATA[Stories by Drioueche Mohammed on Medium]]></description>
        <link>https://medium.com/@drioueche.med?source=rss-53aa763d48fa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*w9hnMHNZ1z2lZWqDG5CT0Q.jpeg</url>
            <title>Stories by Drioueche Mohammed on Medium</title>
            <link>https://medium.com/@drioueche.med?source=rss-53aa763d48fa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:46:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@drioueche.med/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[Coupling and Cohesion in Object-Oriented Programming]]></title>
            <link>https://medium.com/better-programming/coupling-cohesion-in-object-oriented-programming-8984da78b70c?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/8984da78b70c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Sat, 15 Jul 2023 19:39:32 GMT</pubDate>
            <atom:updated>2023-07-19T17:04:42.135Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O-xl5A_98u-wvaiv" /><figcaption>Photo by <a href="https://unsplash.com/es/@raddfilms?utm_source=medium&amp;utm_medium=referral">Redd F</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Have you ever found yourself navigating multiple files to determine how a straightforward function behaves? On the other hand, have you ever been in the middle of a good class that does everything while you are struggling to add your minor improvement? If you have been here before, maybe the code you were exploring is tightly coupled and low in cohesion.</p><p>Thus, what exactly is tightly coupled and low in cohesion code? Why is tightly coupled and low in cohesion code bad? And how can you write better code?</p><p>To answer this question, let’s have a look at coupling and cohesion.</p><h3>Coupling</h3><blockquote>In object oriented design, coupling refers to the degree of direct knowledge that one element has of another. In other words, how often do changes in class A force related changes in class B.</blockquote><p>Let’s take a look at this code snippet:</p><pre>public class Order {<br>  private CashPayment payment = new CashPayment();<br>  <br>  public void processPayment() {<br>    payment.process();<br>  }<br>}<br><br>public class CashPayment {<br>  public void process() {<br>    // Process logic<br>  }<br>}</pre><p>The Order class depends directly on the CashPayment class, which makes it difficult to modify the CashPayment class without affecting the Order class. In this case, we say Order class is tightly coupled with CashPayment class.</p><p>To make the code loosely coupled, we can introduce abstractions to the CashPayment class. Here is an updated version of the code:</p><pre>public class Order {<br>  private PaymentType payment;<br>  <br>  public Order(PaymentType paymentType) {<br>    payment = paymentType;<br>  }<br><br>  public void processPayment() {<br>    payment.process();<br>  }<br>}<br><br>public interface PaymentType {<br>  public void process();<br>}<br><br>public class CashPayment implements PaymentType {<br>  public void process() {<br>    // Process logic<br>  }<br>}</pre><p>In the updated code, we introduced the PaymentType interface, which defines the process() method. The Order class now depends on the PaymentType interface instead of the concrete CashPayment class. By using an interface, we decouple the Order class from the specific implementation of the CashPayment class.</p><p>This change provides better flexibility. It also allows the Order class to work with any class that implements the PaymentType interface. In our case, we can easily add CreditCardPayement class without modifying the Order class.</p><p>To summarize, good code is loosely coupled because modifications will be made in the associated class.</p><h3>Cohesion</h3><blockquote>In object-oriented design, cohesion refers to how a single class is designed. Cohesion is closely associated with making sure that a class is designed with a single, well-focused purpose. In other words, if the stuff that are grouped in a class tends to be similar in many aspects, then the class is said to have high cohesion.</blockquote><p>Let’s take a look at this code snippet:</p><pre>public class Circle {<br>  private double radius;<br><br>  public Circle(double radius) {<br>    this.radius = radius;<br>  }<br><br>  public double getArea() {<br>    return 3.14 * radius * radius;<br>  }<br><br>  public double getPerimeter() {<br>    return 2 * 3.14 * radius;<br>  }<br><br>  public String toString() {<br>    return &quot;Circle (radius: &quot; + radius + &quot;)&quot;;<br>  }<br><br>  public void printDetails() {<br>    System.out.println(&quot;Shape: &quot; + this.toString());<br>    System.out.println(&quot;Area: &quot; + getArea());<br>    System.out.println(&quot;Perimeter: &quot; + getPerimeter());<br>  }<br>}</pre><p>The Circle class has an attribute radius, as well as methods for calculating the area, perimeter, and printing the details of the circle. However, the printDetails() method violates the principle of single responsibility by combining outputting details with calculating area and perimeter. We say our code is low in cohesion.</p><p>To improve cohesion, we can separate the responsibilities by creating a new class just for displaying the details of the circle:</p><pre>public class Circle {<br>  private double radius;<br><br>  public Circle(double radius) {<br>    this.radius = radius;<br>  }<br><br>  public double getArea() {<br>    return 3.14 * radius * radius;<br>  }<br><br>  public double getPerimeter() {<br>    return 2 * 3.14 * radius;<br>  }<br><br>  public String toString() {<br>    return &quot;Circle (radius: &quot; + radius + &quot;)&quot;;<br>  }<br>}<br><br>public class CircleDetailsPrinter {<br>  private Circle circle;<br><br>  public CircleDetailsPrinter(Circle circle) {<br>    this.circle = circle;<br>  }<br><br>  public void printDetails() {<br>    System.out.println(&quot;Shape: &quot; + circle.toString());<br>    System.out.println(&quot;Area: &quot; + circle.getArea());<br>    System.out.println(&quot;Perimeter: &quot; + circle.getPerimeter());<br>  }<br>}</pre><p>In the updated code, the CircleDetailsPrinter class is introduced to handle the printing of circle details. The Circle class now delegates the responsibility of printing to the CircleDetailsPrinter class.</p><p>By separating the printing functionality into its own class, we say that our code is highly cohesive. Each class has a single responsibility, which makes the code clearer and easier to maintain.</p><p>Note that in this example, cohesion is improved by separating the responsibilities between two classes. However, it’s important to establish a balance and avoid excessive class separation. The goal is to have focused classes with clear responsibilities without unnecessarily splitting them into too many small units.</p><p>Good code is highly cohesive because modifications will be made in the associated class.</p><p>Now that we understand the concept of cohesion, let’s apply what we have learned about coupling to the last example. We see that the CircleDetailsPrinter class is tightly coupled with the Circle class. Let’s decouple the CircleDetailsPrinter class from the specific implementation of the Circle class:</p><pre>public interface Shape {<br>  public double getArea();<br>  public double getPerimeter();<br>}<br><br>public class Circle implements Shape {<br>  // ...<br>}<br><br>public class Square implements Shape {<br>  // ...<br>}<br><br>public class Rectangle implements Shape {<br>  // ...<br>}<br><br>public class ShapeDetailsPrinter {<br>  private Shape shape;<br><br>  public ShapeDetailsPrinter(Shape shape) {<br>    this.shape = shape;<br>  }<br><br>  public void printDetails() {<br>    System.out.println(&quot;Shape: &quot; + shape.toString());<br>    System.out.println(&quot;Area: &quot; + shape.getArea());<br>    System.out.println(&quot;Perimeter: &quot; + shape.getPerimeter());<br>  }<br>}</pre><p>By introducing the Shape interface, we decoupled the CircleDetailsPrinter class from the specific implementation of the Circle class. Additionally, we leverage the new ShapeDetailsPrinter class to serve the Square and Rectangle classes without duplicating code.</p><h3>Conclusion</h3><p>In conclusion, loosely coupled and high-cohesion code is the best. Additionally, we learned how hard it is to maintain tightly coupled and low-cohesion code and how to rework it.</p><p><em>I hope you found this article insightful. Thank you for reading.</em></p><p><em>Please make sure to follow me to get the updates.</em></p><p><em>You can also find me </em><a href="https://linktr.ee/drioueche_med"><em>elsewhere on the internet</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8984da78b70c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/coupling-cohesion-in-object-oriented-programming-8984da78b70c">Coupling and Cohesion in Object-Oriented Programming</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Walk Through Refactoring]]></title>
            <link>https://medium.com/@drioueche.med/a-walk-through-refactoring-a38d81bcd66?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/a38d81bcd66</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Wed, 19 Apr 2023 17:28:43 GMT</pubDate>
            <atom:updated>2023-04-19T17:28:43.518Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iqGahMa_-tPRISYq" /><figcaption>Photo by <a href="https://unsplash.com/pt-br/@norevisions?utm_source=medium&amp;utm_medium=referral">No Revisions</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>In software development, we spent more time reading code rather than actually writing it.</blockquote><p>This is what I was told in the beginning of my career, and upon reflection on my day-to-day, I realized that I am usually upgrading and maintaining existing projects rather than starting new ones from scratch. This prompted me to believe that developers should prioritize the quality of their code, just as they prioritize getting it to work.</p><p>As the quality matter, this article will provide some advice on how to write a clean and readable code by refactoring step-by-step an existing code.</p><p>But first, <strong>what’s code refactoring?</strong></p><p>Code refactoring is the process of restructuring and improving existing source code without changing its external behavior or functionality. It involves making changes to the code to make it easier to understand, modify, and maintain, while also improving its overall quality, readability, and performance.</p><p>After defining refactoring, let’s examine this code snippet and try to identify all the code smells and figure out how to fix them.</p><pre>public class Processor {<br><br>// process Api response<br>public static List&lt;List&lt;Integer&gt;&gt; process(List&lt;List&lt;Integer&gt;&gt; d) {<br><br>boolean isFirst = true;<br><br>for (int i = 0; i &lt; d.size(); i++) {<br>    // Category A<br>    if (d.get(i).get(0) == 1) {<br>      for (int j =1; j &lt; d.get(i).size(); j++){<br>            if (d.get(i).get(j) &lt; 0)<br>                    d.get(i)<br>                    .set(j, -1 * d.get(i).get(j));<br>    }<br>    }<br>        // Category B<br>        if (d.get(i).get(0) == -1){<br>        for (int j = 1;j &lt; d.get(i).size();j++) {<br>            if(d.get(i).get(j) &gt; 0) d.get(i).set(j, -1 * d.get(i).get(j));<br>        }}}<br>        return d;<br>    }<br>}</pre><h4>Code formatting</h4><p>It’s difficult to figure out the hierarchy of the nested statements in the snippet. The snippet demonstrates the value of proper code formatting.</p><p>This code smell is unacceptable to have, since all modern IDEs have shortcut to format code, besides, you can customize the formatting rules.</p><h4>Dead code</h4><p>In the line before the first for loop, the variable <em>‘isFirst’</em> is declared but not used . This is referred to as dead code.</p><p>Unused variables, unused methods, unused imports, and if statements that are never true are all examples of dead code. This code smell is also unacceptable to have dead code, since all modern IDEs warn developers when a program contains dead code.</p><p>Sometimes our laziness drives us to tolerate dead code under the justification that “we will use it later”, but by the time we need to use this dead code, we will probably forget about it. Which lead not only to have unnecessary and forgotten lines of code, but also to have a duplicated code that can make the program more complex and difficult to maintain.</p><p>After fixing those two code smells, our code will look like this:</p><pre>public class Processor {<br><br>    // process Api response<br>    public static List&lt;List&lt;Integer&gt;&gt; process(List&lt;List&lt;Integer&gt;&gt; d) {<br>        <br>        for (int i = 0; i &lt; d.size(); i++) {<br>            // Category A<br>            if (d.get(i).get(0) == 1) {<br>                for (int j = 1; j &lt; d.get(i).size(); j++) {<br>                    if (d.get(i).get(j) &lt; 0)<br>                        d.get(i).set(j, -1 * d.get(i).get(j));<br>                }<br>            }<br>            // Category B<br>            if (d.get(i).get(0) == -1) {<br>                for (int j = 1; j &lt; d.get(i).size(); j++) {<br>                    if(d.get(i).get(j) &gt; 0)<br>                        d.get(i).set(j, -1 * d.get(i).get(j));<br>                }<br>            }<br>        }<br>        <br>        return d;<br>    }<br>}</pre><h4>Meaningful naming</h4><p>Even the code’s functionality is now clear, but it’s hard to tell the purpose of this process method. The reason behind this is because there is a lack of context, and the way to contextualize your code is by giving your variables and methods meaningful names.</p><p>To prove the importance of meaningful naming, let’s imagine we want to create a variable that represents an accordion element, we can name it <em>‘var a’ </em>or <em>‘var acrd’</em> or <em>‘var accordion’</em>, it’s clear that the last one is more representative. However, it’s better to add some context to the variable, for example <em>‘var loginAccordion’</em>, by doing so it becomes easier to understand what the variable does and why it’s used.</p><p>Just to clarify, the more longest names the worse it is; the ideal names are those that are short and clear.</p><p>Now that we are aware of the importance of a good naming, let’s apply it on our code snippet.</p><pre>public class Processor {<br><br>    // process Api response<br>    public static List&lt;List&lt;Integer&gt;&gt; processApiResponse(<br>        List&lt;List&lt;Integer&gt;&gt; apiResponseList<br>        ) { <br>        for (List&lt;Integer&gt; apiResponse : apiResponseList) {<br>            // Category A<br>            if (apiResponse.get(0) == 1) {<br>                for (int i = 1; i &lt; apiResponse.size(); i++) {<br>                    if (apiResponse.get(i) &lt; 0)<br>                        apiResponse.set(i, -1 * apiResponse.get(i));<br>                }<br>            }<br>            // Category B<br>            if (apiResponse.get(0) == -1) {<br>                for (int i = 1; i &lt; apiResponse.size(); i++) {<br>                    if(apiResponse.get(i) &gt; 0)<br>                        apiResponse.set(i, -1 * apiResponse.get(i));<br>                }<br>            }<br>        }<br>        <br>        return apiResponseList;<br>    }<br>}</pre><h4>Magic numbers &amp; strings</h4><p>Magic numbers &amp; strings are values used directly in code that has a special meaning or behavior, but are not obvious to someone reading the code. These values are called ‘<strong>magic</strong>’ because they seem to have no explanation for why they were used.</p><p>To avoid this code smell, it’s advised to use constants or variables with descriptive names.</p><p>In our case, our magic numbers will be represented as:</p><pre>final int CATEGORY_A = 1;<br>final int CATEGORY_B = -1;</pre><h4>Comments</h4><p>There is two types of comments, comments that answer to <strong>‘What?’</strong> question and the ones that answer to <strong>‘Why?’ </strong>question.</p><p>‘What’ comments are bad, because they result in duplication of knowledge, which can lead to two different sources of information simply by updating the code and forgetting comments. Besides, considering adding ‘What’ comments indicates the code’s readability difficulty, which should be resolved by rewriting the code rather than adding comments.</p><p>On the other side, ‘Why’ comments are useful since they explain why the code was implemented in a specific way, and it doesn’t lead to duplication of knowledge.</p><p>In our code snippet, there are three ‘What’ comments that we must remove.</p><h4>Functions</h4><p>Functions are the basic reusable blocks of any software code that accomplish a single task. A good function is the one that meets these criteria:</p><ul><li><strong>Short</strong>: No one of us wants to find a function with a hundred of lines, so we all agree that functions should be short, but how short they should be? According to the literature, you can find that each function should be no longer than 5 lines, but for me, I found that they should be short to the point that each function should perform a single elementary task regardless the number of lines, which lead to our next criterion.</li><li><strong>Perform Single Task</strong>: Each function should only be responsible for one thing, as this makes them simpler to understand, test, and reuse.</li><li><strong>With No Flag argument:</strong> Flag arguments are parameters that indicate whether a particular option should be enabled or disabled. The flag argument usually reveals that the function performs two tasks based on the option, which is contrary to the previous criterion. The solution is to create a single function for each option.</li><li><strong>With less parameters</strong>: Nobody wants to find a function with a gigantic list of a parameter since it makes the function hard to understand and test given all possible parameter combinations. In this case, grouping related parameters into an object or struct is the best decision. For example: instead of having three parameters (x, y, and z) that indicate coordinates, create a <em>‘Coordinate’</em> object.</li><li><strong>Pure</strong>: Functions are designed to accept input from parameters and return the output when necessary. Returning the result through parameters in not expected and makes debugging hard because the function changes variables outside their scope. As a solution, it’s advised to use pure functions. Pure function is a function that always returns the same output for a given input, and has no side effects.</li></ul><p>This is how our code will look if we implement these criteria:</p><pre>public class Processor {<br><br>    public static List&lt;List&lt;Integer&gt;&gt; processApiResponse(<br>        List&lt;List&lt;Integer&gt;&gt; apiResponseList<br>        ) {<br><br>        List&lt;List&lt;Integer&gt;&gt; apiResponseListClone = cloneApiResponse(<br>              apiResponseList<br>            );<br>        for (List&lt;Integer&gt; apiResponse : apiResponseListClone) <br>            processResponseItem(apiResponse);<br>        <br>        return apiResponseListClone;<br>    }<br><br>    private static List&lt;List&lt;Integer&gt;&gt; cloneApiResponse(<br>        List&lt;List&lt;Integer&gt;&gt; apiResponseList<br>        ) {<br>        // code that return a clone of List&lt;List&lt;Integer&gt;&gt;<br>    }<br><br>    private static void processResponseItem(List&lt;Integer&gt; apiResponse) {<br>        final int CATEGORY_A = 1;<br>        final int CATEGORY_B = -1;  <br>      <br>        if (apiResponse.get(0) == CATEGORY_A) {<br>            processCategoryA(apiResponse);<br>        }<br>        if (apiResponse.get(0) == CATEGORY_B) {<br>            processCategoryB(apiResponse);<br>        }<br>    }<br><br>    private static void processCategoryA(List&lt;Integer&gt; apiResponse) {<br>        for (int i = 1; i &lt; apiResponse.size(); i++) {<br>            if (apiResponse.get(i) &lt; 0)<br>                apiResponse.set(i, -1 * apiResponse.get(i));<br>        }<br>    }<br><br>    private static void processCategoryB(List&lt;Integer&gt; apiResponse) {<br>        for (int i = 1; i &lt; apiResponse.size(); i++) {<br>            if (apiResponse.get(i) &gt; 0)<br>                apiResponse.set(i, -1 * apiResponse.get(i));<br>        }<br>    }<br>}</pre><h4>Primitive Obsession</h4><p>Primitive obsession is the overuse of primitive data types (such as integers, strings, etc..) to represent domain concepts and data structures, which lead to reduce readability and flexibility and increase risk of errors.</p><p>Imagine that a country code is represented by a string. This doesn’t give the developer so much control over the country code because string can be any value and not just a country code. To solve this, you can wrap the string in an object called <em>‘CountryCode’</em> and possibly add some constraints to ensure that the string truly is a country code.</p><p>In our code snippet, we are presenting an API response with a list of integers. Let’s refactor this code smell.</p><pre>public class Processor {<br><br>    public static List&lt;ApiResponse&gt; processApiResponse(<br>        List&lt;ApiResponse&gt; apiResponseList<br>        ) {<br><br>        List&lt;ApiResponse&gt; apiResponseListClone = cloneApiResponse(<br>              apiResponseList<br>            );<br><br>        for (ApiResponse apiResponse : apiResponseListClone) <br>            processApiResponse(apiResponse);<br>        <br>        return apiResponseListClone;<br>    }<br><br>    private static List&lt;ApiResponse&gt; cloneApiResponse(<br>        List&lt;ApiResponse&gt; apiResponseList<br>        ) {<br>        // code that return a clone of List&lt;ApiResponse&gt;<br>    }<br><br>    private static void processApiResponse(ApiResponse apiResponse) {<br>        if (apiResponse.getCategoryFlag() == ApiResponse.CATEGORY_A) {<br>            processCategoryA(apiResponse);<br>        }<br>        if (apiResponse.getCategoryFlag() == ApiResponse.CATEGORY_B) {<br>            processCategoryB(apiResponse);<br>        }<br>    }<br><br>    private static void processCategoryA(ApiResponse apiResponse) {<br>        for (int i = 1; i &lt; apiResponse.size(); i++) {<br>            if (apiResponse.getResponseElement(i) &lt; 0)<br>                apiResponse.setResponseElement(i, <br>                    -1 * apiResponse.getResponseElement(i)<br>                );<br>        }<br>    }<br><br>    private static void processCategoryB(ApiResponse apiResponse) {<br>        for (int i = 1; i &lt; apiResponse.size(); i++) {<br>            if (apiResponse.getResponseElement(i) &gt; 0)<br>                apiResponse.setResponseElement(i, <br>                    -1 * apiResponse.getResponseElement(i)<br>                );<br>        }<br>    }<br>}<br><br>public class ApiResponse {<br>            <br>    public static final int CATEGORY_A = 1;<br>    public static final int CATEGORY_B = -1;<br><br>    private List&lt;Integer&gt; response;<br><br>    public int getCategoryFlag() {<br>        return response.get(0);<br>    }<br><br>    public Integer getResponseElement(int index) {<br>        return response.get(index);<br>    }<br><br>    public void setResponseElement(int index, Integer newValue) {<br>        response.set(index, newValue);<br>    }<br><br>    public int size() {<br>        return response.size();<br>    }<br>}</pre><h3>Conclusion</h3><p>Code is not only meant to be running, but it also to be read; therefore, the readability of the code is important.</p><p><em>Hope You found this article insightful, thank you for reading.</em></p><p><em>Please make sure to follow to get the updates.</em></p><p><em>You can also find me </em><a href="https://linktr.ee/drioueche_med"><em>elsewhere on the internet</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a38d81bcd66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DRY: The Unspoken Parts]]></title>
            <link>https://medium.com/better-programming/dry-the-unspoken-parts-9483056d0306?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/9483056d0306</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[dry-principle]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Sat, 28 Jan 2023 17:57:25 GMT</pubDate>
            <atom:updated>2023-07-19T15:37:40.424Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rT1Zf8wrFjw_KAKt" /><figcaption>Photo by <a href="https://unsplash.com/@laine23?utm_source=medium&amp;utm_medium=referral">Laine Cooper</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>One of the most common terms in software engineering is the DRY principle (Don’t Repeat Yourself), but many engineers associate this principle with “Don’t copy-paste lines of code.” This is a part of DRY; however, DRY is about duplication of knowledge.</p><p>But first, why is duplication evil?</p><ol><li>The apparent issue with duplication is the unhealthy growth of your code. In other words, there are many duplicated lines in your code, so it becomes difficult to browse your code.</li><li>In the software lifecycle, the change specification or the maintenance of the project, e.g., by fixings bugs, is more likely to happen. Then, if your code isn’t DRY, you are going to make changes in different places. This isn’t the problem; the real problem is how many places you are going to forget.</li><li>Once you have read code that isn’t DRY, you are going to find yourself describing it as non-modular or violating SOLID principles, and it doesn’t respect any fancy word used to describe well-written code.</li></ol><p>Now that we are aware that duplication is evil, what does this DRY (Don’t Repeat Yourself) principle say?</p><blockquote>“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” — from <strong>The Pragmatic Programmer</strong> book</blockquote><p>After a brief introduction about DRY, let’s discuss some DRY code smells and find ways to fix it.</p><blockquote><strong>Note</strong>: Attempt to spot code smells from the first code snippet in each section contains code smells, before reading explanations and code suggestion (second snippet).</blockquote><h3>1. Duplication In Code</h3><pre>void printTransaction() {<br>  if (isProduct) {<br>    if (isFromVendor)<br>      System.out<br>        .println(String.format(&quot;Product %s : - %10.2f $&quot;, name, amount));<br>    else<br>      System.out<br>        .println(String.format(&quot;Product %s : + %10.2f $&quot;, name, amount));<br>  } else {<br>    if (isFromVendor)<br>      System.out<br>        .println(String.format(&quot;Service %s : - %10.2f $&quot;, name, amount));<br>    else<br>      System.out<br>        .println(String.format(&quot;Service %s : + %10.2f $&quot;, name, amount));<br>  }<br>}</pre><p>This is a common case of duplication. It’s pretty evident that there is a duplication of formatting and duplication of determining “+/-” signs to represent in/out transactions. Below is a suggestion on how we can improve this code:</p><pre>void printTransaction() {<br>  String transactionType = isProduct ? &quot;Product&quot; : &quot;Service&quot;;<br>  System.out.println(getformattedTransaction(transactionType));<br>}<br><br>String getformattedTransaction(String transactionType) {<br>  String formattedAmount = isFromVendor ? String.format(&quot;- %10.2f&quot;, amount) <br>                              : String.format(&quot;+ %10.2f&quot;, amount);<br>  return String.format(&quot;%s %s : %s $&quot;, transactionType, name, formattedAmount);<br>}</pre><h3>2. Not All Duplications Are DRY</h3><pre>boolean isUsernameValid() {<br>  return username.length() &gt; 5;<br>}<br>boolean isPasswordValid() {<br>  return password.length() &gt; 5;<br>}</pre><p>This is a tricky example. As we saw in the introduction, DRY is about knowledge duplication rather than code. It’s purely a coincidence that the username and password in the example both satisfy the same criteria of validity, but nothing prevents modifying the criteria of validating the password, for example, without affecting the criteria of validating the username, simply because password and username are independent entities.</p><p>So, before grouping a duplicate code, consider this question: “Is it a duplication or just a coincidence?”</p><h3>3. Duplication in Documentation</h3><pre>// This method calculates the final price of a product<br>// By adding to the product&#39;s cost the taxes and the transportation cost<br>// Tax percentage is 20%<br>// &amp; Average transportation cost is 200 $<br>double finalPrice(double productCost) {<br>  return productCost + productCost * 0.2 + 200;<br>}</pre><p>As a student, I fell into this mistake, believing that adding comments would make my code clearer. However, most of the time, like in the example above, it leads to duplication of information.</p><p>Let’s suppose that the product price formula changes regularly due to some external factors (e.g., regulations, changes in transportation cost, etc.). To keep the code coherent, the developer needs to update the formula twice (code and comment), but from my experience, it’s more likely to update the code and forget about the comment, which can lead -in our case- to two different formulas.</p><p>As a refactor to the code above, it’s preferable to get rid of comments and move magic numbers with a well-named constant.</p><pre>static final double TAX_PERCENTAGE = 0.2;<br>static final double AVERAGE_TRANSPORTATION_COST = 200;<br>double finalPrice(double productCost) {<br>  return productCost + productCost * TAX_PERCENTAGE <br>          + AVERAGE_TRANSPORTATION_COST;<br>}</pre><p>This section is not about getting rid of all comments in your code. But keep in mind that comments are helpful when used to answer the “Why” question.</p><p>Finally, documentation is not restricted to comments but includes all forms of documentation. For example, if you are developing a public API or a library and you need to provide documentation, think of using a documentation generator such as Sphinx for Python.</p><h3>4. Duplication in Data</h3><pre>class StopWatch {<br>  private LocalDateTime startDateTime;<br>  private LocalDateTime endDateTime;<br>  private Long durationInMilliSecond;<br>  // Constructor <br>  // getters &amp; setters<br>}</pre><p>The example above seems to be fine, but it’s very likely to have bugs as this design allows developers to modify start/end DateTime and forget to modify duration. Therefore, the solution for this kind of duplication is to make the duration calculated when it’s needed.</p><pre>class StopWatch {<br>  private LocalDateTime startDateTime;<br>  private LocalDateTime endDateTime;<br>  // Constructor <br>  // getters &amp; setters<br>  public Long getDurationInMilliSecond() {<br>    return ChronoUnit.MILLIS.between(startDateTime, endDateTime);<br>  }<br>}</pre><h3>Conclusion</h3><p>As we’ve seen, DRY is not limited to copy-paste code, but it’s about duplication of knowledge.</p><p><em>I hope you found this article insightful. Thank you for reading.</em></p><p><em>Please make sure to follow up to get the updates.</em></p><p><em>You can also find me </em><a href="https://linktr.ee/drioueche_med"><em>elsewhere on the internet</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9483056d0306" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/dry-the-unspoken-parts-9483056d0306">DRY: The Unspoken Parts</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Does a Quad-copter Fly?]]></title>
            <link>https://medium.com/@drioueche.med/how-does-a-quad-copter-fly-a133f6d49749?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/a133f6d49749</guid>
            <category><![CDATA[uav]]></category>
            <category><![CDATA[flight]]></category>
            <category><![CDATA[research]]></category>
            <category><![CDATA[drones]]></category>
            <category><![CDATA[mechanical-engineering]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Sun, 16 Jan 2022 19:32:12 GMT</pubDate>
            <atom:updated>2022-01-16T20:42:14.547Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*54lHd0LVomjCk_ON" /><figcaption>Photo by <a href="https://unsplash.com/@mahlkornel?utm_source=medium&amp;utm_medium=referral">Kornél Máhl</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the past few years, the number of research publications about <strong>Unmanned Aerial Vehicle (UAV a.k.a. Drones)</strong> have grown exponentially. Nowadays, UAVs are becoming democratized and more used not only in military applications but also in civil applications such as photography, agriculture, etc…</p><p>To understand this trends, and having a full understanding of UAVs, it’s recommended to start with the basics. For this topic one of the basics is: “<strong>how does an UAV fly ?”</strong></p><p>To answer this question, we are going to focus on quad-rotors <em>(UAV with 4 motors)</em>, Using a X configuration presentation and Euler angles <em>(pitch, Yaw, Roll)</em> as it shown in Fig 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*yae7uaWVA6ee_yI_Hc8x-A.png" /><figcaption>Fig 1. Photo by Shady Zahran, Adel Mousa, NAser El-Sheimy on mdpi</figcaption></figure><h3>I. Vertical Motion:</h3><p>By controlling the rotation of the propeller a lift force’s is produced (this lift is referred to as <strong>thrust</strong>) which moves the UAV vertically <em>(as shown in Fig 2.)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*JZOV5LvuiP3quQJVNQUlsg.png" /><figcaption>Fig 2. Photo by Sharad Pachpute on cfdflowengineering</figcaption></figure><p>For studying the vertical fly of UAVs, we should first define the forces applied on it.</p><p>There is two forces:</p><ul><li>Weight that pull the UAV down.</li><li>Thrust that lift the UAV up.</li></ul><p><em>(Fig 2)</em></p><p>Three scenarios are possible:</p><ul><li><strong>Upward motion</strong>: occurs when the thrust is bigger than the weight, then the UAV will move upward.</li><li><strong>Downward motion</strong>: occurs when the thrust is smaller than the weight, then the UAV will move downward.</li><li><strong>Hover mode:</strong> <em>(To </em><strong><em>hover</em></strong><em> means to stay in the same position in the air without moving)</em> occurs when the thrust is equal with the weight, then the UAV will remain in the same altitude.</li></ul><blockquote>To sum up, for achieving a vertical motion, the propellers should rotate at the same speed, and comparing the thrust force’s with the weight a vertical motion occurs.</blockquote><h3>II. Horizontal Motion:</h3><h4><strong>a. Forward/backward motion:</strong></h4><p>By controlling the rotation speed of the rear or the font propellers a pitch motion is produced which leads to a forward or backward motion of the UAV.</p><ul><li>If two rear propellers rotate at high speed, then the drone will move in a <strong>forwarding direction </strong><em>(as shown in Fig 3.).</em></li><li>If two front propellers rotate high speed, then the drone will move in the <strong>backward direction</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*AiikUQhwbyx15uno2zxLWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*99asbE89cyRt7P9Au0z9Gg.png" /><figcaption>Fig 3 . Photos by Lesics on YouTube</figcaption></figure><blockquote>To sum up, the two front propellers (rear propellers) should rotate with higher speed than the rear propellers (front propellers) to achieve a backward (forward) motion.</blockquote><h4>b. Sideways (Left/Right) Horizontal Motion:</h4><p>By controlling the rotation speed of the left or the right propellers a roll motion is produced which leads to a left or right horizontal motion of the UAV.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*3-lnjEQd3vSJOjbm.jpg" /><figcaption>Fig 4. Photo by Namrata Gupta, Mangal Kothari, and Abhishek on researchgate</figcaption></figure><ul><li>If the two right propellers rotate at high speed, then the drone will move in the left direction.</li><li>If the two left propellers rotate at high speed, then the drone will move in the right direction <em>(as shown in Fig 4.)</em>.</li></ul><p><em>(Fig 4)</em></p><blockquote>To sum up, the two right propellers (left propellers) should rotate with higher speed than the left propellers (right propellers) to achieve a horizontal left motion (right motion).</blockquote><h3>III. Turn (Left/Right) Motion:</h3><p>By controlling the rotation speed of the diagonal propellers a yaw motion is produced which leads to a rotation motion of UAV (either left or right) <em>(as shown in Fig 5.)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*bVOzVJD8-qBrAP3-CapKIw.png" /><figcaption>Fig 5. Photo by Namrata Gupta, Mangal Kothari, and Abhishek on researchgate</figcaption></figure><blockquote>To sum up, one of the diagonal propellers should rotate with higher speed to produce a rotation motion.</blockquote><h3>Summary:</h3><p>Finally, I would to love share the fig. below that summarize the possible combinations to achieve different motions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/0*Xp7m2e-C0RN7yyOT.jpg" /><figcaption>Fig 6. Photo by socialledge</figcaption></figure><p><em>Please make sure to follow to get the updates.</em></p><p><em>You can also join me on </em><a href="https://www.linkedin.com/in/driouechemed/"><em>LinkedIn</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a133f6d49749" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading Values in a Competitive Programming (Beginner): List Comprehension, NZEC error]]></title>
            <link>https://medium.com/@drioueche.med/reading-values-in-a-competitive-programming-beginner-list-comprehension-nzec-error-1a95988294bc?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a95988294bc</guid>
            <category><![CDATA[error]]></category>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[reading]]></category>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Fri, 09 Apr 2021 11:44:59 GMT</pubDate>
            <atom:updated>2023-01-21T17:04:14.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2O_PP3P_DkQQ8-eI" /><figcaption>Photo by <a href="https://unsplash.com/@hiteshchoudhary?utm_source=medium&amp;utm_medium=referral">Hitesh Choudhary</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We all participated in a competitive programming, and we all know that reading inputs in a competitive programming is the key to solve a problem, a misread input implies a wrong output (or even an error).</p><p>And with experience, we all struggled with reading value, so that what this article is all about. We will discuss <strong>List Comprehension &amp; NZEC error in Python.</strong></p><h3>1. List Comprehension</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dl%252520%25253D%252520%25255B%25255D%25250Afor%252520i%252520in%252520range%252810%2529%25253A%25250A%252520%252520l.append%2528int%2528input%2528%2529%2529%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dl%25252520%2525253D%25252520%2525255B%2525255D%2525250Afor%25252520i%25252520in%25252520range%25252810%252529%2525253A%2525250A%25252520%25252520l.append%252528int%252528input%252528%252529%252529%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cc5ecf0961be2eddc6af1872006b06c/href">https://medium.com/media/2cc5ecf0961be2eddc6af1872006b06c/href</a></iframe><p>The code in the left is a simple code for reading 10 integer values.</p><p>For who is used to program with C++ or other languages this code is excellent, or maybe they will think that this is the only way to read an array/list. However, in python there is other way, in just a single line and in a more easy readable way, that way is called<em> ‘List Comprehension’.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dl%252520%25253D%252520%25255Bint%2528input%2528%2529%2529%252520for%252520i%252520in%252520range%252810%2529%25255D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dl%25252520%2525253D%25252520%2525255Bint%252528input%252528%252529%252529%25252520for%25252520i%25252520in%25252520range%25252810%252529%2525255D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/473dc6d507b7c033faa77b61f35b77f9/href">https://medium.com/media/473dc6d507b7c033faa77b61f35b77f9/href</a></iframe><p>Here is what the previous code will look like in python using list comprehension.</p><p>So in short, List comprehension is an elegant and more readable way to define and create a list in python.</p><p>To make it clear, here is more few examples:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523%252520A%252520list%252520that%252520contains%252520numbers%252520from%2525201%252520to%25252019%25250Al%252520%25253D%252520%25255Bx%252520for%252520x%252520in%252520range%25281%25252C%25252020%2529%25255D%25250A%25250A%252523%252520A%252520list%252520that%252520contains%252520even%252520numbers%252520from%2525201%252520to%25252019%25250Al%252520%25253D%252520%25255Bx%252520for%252520x%252520in%252520range%25281%25252C%25252020%2529%252520if%252520x%252520%252525%2525202%252520%25253D%25253D%2525200%25255D%25250A%25250A%252523%252520A%252520list%252520that%252520contains%252520square%252520of%252520all%252520even%252520numbers%252520from%2525201%252520to%25252019%25250Al%252520%25253D%252520%25255Bx%252520%252A%252A%2525202%252520for%252520x%252520in%252520range%25281%25252C%25252020%2529%252520if%252520x%252520%252525%2525202%252520%25253D%25253D%2525200%25255D%25250A%25250A%252523%252520In%252520the%252520same%252520way%252520there%252520is%252520%252522Dictionary%252520Comprehension%252522%25250A%252523%252520A%252520dictionary%252520with%252520numbers%252520from%2525200%252520to%2525209%252520as%252520keys%252520and%252520their%252520square%252520as%252520value%25250Ad%252520%25253D%252520%25257Bx%25253Ax%252A%252A2%252520for%252520x%252520in%252520range%252810%2529%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523%25252520A%25252520list%25252520that%25252520contains%25252520numbers%25252520from%252525201%25252520to%2525252019%2525250Al%25252520%2525253D%25252520%2525255Bx%25252520for%25252520x%25252520in%25252520range%2525281%2525252C%2525252020%252529%2525255D%2525250A%2525250A%25252523%25252520A%25252520list%25252520that%25252520contains%25252520even%25252520numbers%25252520from%252525201%25252520to%2525252019%2525250Al%25252520%2525253D%25252520%2525255Bx%25252520for%25252520x%25252520in%25252520range%2525281%2525252C%2525252020%252529%25252520if%25252520x%25252520%25252525%252525202%25252520%2525253D%2525253D%252525200%2525255D%2525250A%2525250A%25252523%25252520A%25252520list%25252520that%25252520contains%25252520square%25252520of%25252520all%25252520even%25252520numbers%25252520from%252525201%25252520to%2525252019%2525250Al%25252520%2525253D%25252520%2525255Bx%25252520%25252A%25252A%252525202%25252520for%25252520x%25252520in%25252520range%2525281%2525252C%2525252020%252529%25252520if%25252520x%25252520%25252525%252525202%25252520%2525253D%2525253D%252525200%2525255D%2525250A%2525250A%25252523%25252520In%25252520the%25252520same%25252520way%25252520there%25252520is%25252520%25252522Dictionary%25252520Comprehension%25252522%2525250A%25252523%25252520A%25252520dictionary%25252520with%25252520numbers%25252520from%252525200%25252520to%252525209%25252520as%25252520keys%25252520and%25252520their%25252520square%25252520as%25252520value%2525250Ad%25252520%2525253D%25252520%2525257Bx%2525253Ax%25252A%25252A2%25252520for%25252520x%25252520in%25252520range%25252810%252529%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef198ac82e026cf37b8716255d244d76/href">https://medium.com/media/ef198ac82e026cf37b8716255d244d76/href</a></iframe><p>And as I always say, to get more used to list comprehension practice is the key.</p><h3>2. NZEC error in Python</h3><p>In this chapter we will discuss the famous NZEC error that everyone encountered. NZEC (Non Zero Exit Code) as the name suggests occurs when your code is failed to return 0. When a code returns 0 it means it is successfully executed otherwise it will return some other number depending on the type of error.</p><p><strong>Why does NZEC occur?</strong></p><p>This error is <strong>mainly </strong>caused by the fact that most of the coding platforms gives input separated by space, however int(input()) is not able to read such an input properly and shows error like NZEC.</p><p>Here is a more concrete example to understand, for example the input is two integers separated with space 5 6 , let’s try to apply a, b = int(input()) on it and see what it produce. First, input() will try to read input 5 6 , and we all know that input()return a string, so it will produce &#39;5 6&#39; , now int() try to cast the input return as following int(&#39;5 6&#39;) , and this obviously will case an error because &#39;5 6&#39; is not a number. And even without casting, we shouldn’t forget that the return of input() is a single string &#39;5 6&#39; however we try to assign it to two variable a and b .</p><p><strong>How to resolve?</strong></p><p>The overcome the problem, we will use split() . The split() method breaks up a string at the specified separator and returns a list of strings.</p><p>So using the previous example of 5 6 as an input, we will try to apply a, b = input().split() instead, so as a result of this line of code we will have a= &#39;5&#39; &amp; b= &#39;6&#39; , and casting in the next lines a &amp; b will not cause problem, cause the strings are completely made of digits and no space in it.</p><p>To make it easy and quick to learn, I’ll try to compress everything in the following code:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523%252520input%25253A%2525205%2525205%25250A%252523%252520wrong%252520code%25250Aa%252520%25253D%252520int%2528input%2528%2529%2529%25250Ab%252520%25253D%252520int%2528input%2528%2529%2529%25250A%25250A%252523%252520correct%252520code%25250Aa%25252C%252520b%252520%25253D%252520input%2528%2529.split%2528%2529%252520%252520%252523%252520or%252520a%25252C%252520b%252520%25253D%252520input%2528%2529.split%2528%2527%252520%2527%2529%25250Aa%252520%25253D%252520int%2528a%2529%25250Ab%252520%25253D%252520int%2528b%2529%25250A%25250A%252523%252520Array%252520input%25250A%252523%252520input%25253A%2525201%2525202%2525203%2525204%2525205%2525206%2525207%2525208%2525209%25252010%25250A%252523%252520using%25253A%252520List%252520Comprehension%25250Al%252520%25253D%252520%25255Bint%2528x%2529%252520for%252520x%252520in%252520input%2528%2529.split%2528%2529%25255D%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dnight-owl%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523%25252520input%2525253A%252525205%252525205%2525250A%25252523%25252520wrong%25252520code%2525250Aa%25252520%2525253D%25252520int%252528input%252528%252529%252529%2525250Ab%25252520%2525253D%25252520int%252528input%252528%252529%252529%2525250A%2525250A%25252523%25252520correct%25252520code%2525250Aa%2525252C%25252520b%25252520%2525253D%25252520input%252528%252529.split%252528%252529%25252520%25252520%25252523%25252520or%25252520a%2525252C%25252520b%25252520%2525253D%25252520input%252528%252529.split%252528%252527%25252520%252527%252529%2525250Aa%25252520%2525253D%25252520int%252528a%252529%2525250Ab%25252520%2525253D%25252520int%252528b%252529%2525250A%2525250A%25252523%25252520Array%25252520input%2525250A%25252523%25252520input%2525253A%252525201%252525202%252525203%252525204%252525205%252525206%252525207%252525208%252525209%2525252010%2525250A%25252523%25252520using%2525253A%25252520List%25252520Comprehension%2525250Al%25252520%2525253D%25252520%2525255Bint%252528x%252529%25252520for%25252520x%25252520in%25252520input%252528%252529.split%252528%252529%2525255D%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/51aaf2b7e240105c73127c33259a97e3/href">https://medium.com/media/51aaf2b7e240105c73127c33259a97e3/href</a></iframe><p>That was a short overview on List Comprehension, NZEC error, I hope it was clear and you like it.</p><p><em>Please make sure to follow to get the updates.</em></p><p><em>You can also join me on </em><a href="https://www.linkedin.com/in/driouechemed/"><em>LinkedIn</em></a><em>.</em></p><p><em>Code written using: </em><a href="https://carbon.now.sh/">carbon</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a95988294bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Programmable Logic Device (PLD)]]></title>
            <link>https://medium.com/@drioueche.med/programmable-logic-device-pld-b15a6474276e?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/b15a6474276e</guid>
            <category><![CDATA[fpga]]></category>
            <category><![CDATA[programmable-logic]]></category>
            <category><![CDATA[pld]]></category>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Tue, 29 Sep 2020 17:42:21 GMT</pubDate>
            <atom:updated>2023-01-21T17:03:33.187Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hk3Z9BaZW4oZHEDn" /><figcaption>Photo by <a href="https://unsplash.com/@infralist?utm_source=medium&amp;utm_medium=referral">Infralist.com</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Everyone is annoyed with Windows OS updates and other software updates<em> (I get annoyed too)</em>, but imagine if there is a hardware update!</p><p>But wait, what’s a hardware update?</p><p>I will go easy on you, for example imagine that you own a laptop with Intel i3 processor and instead of buying a new laptop with i7 or i9 processor, you can simply download an update <em>(hardware update)</em> from Intel website and without changing the hardware you move from an i3 to i7 or i9.</p><p>Sound like science-fiction? Yeah, I exaggerated a little bit, but this is <em>(the hardware updates)</em> possible due to <strong>Programmable Logic Devices (PLDs)</strong>.</p><p>So what are those PLDs?</p><p>we are going to response to this question and present the PLDs variants in the rest of this article.</p><h3>PLD</h3><p>A logic device is an electronic component which is customized for a particular use decided at the time of manufacture. For example, a not gate always inverts the logic level of the input signal and can’t do anything else.</p><p>However, <strong>Programmable Logic Devices</strong> (PLDs) are the components which do not have a specific function associated with them. Before the PLD can be used in a circuit it must be programmed (reconfigured) to perform a certain function on a need basis and can further be changed to perform some other function at the later point of time.</p><p>There are many variants of PLDs :</p><ul><li><strong>PLA</strong> (<em>Programmable Logic Array</em>);</li><li><strong>PAL</strong> (<em>Programmable Array Logic</em>) ;</li><li><strong>GAL</strong> (<em>Generic Logic Array</em>);</li><li><strong>CPLD</strong> (<em>Complex Programmable Logic Device</em>) ;</li><li><strong>FPGA</strong> (<em>field-programmable gate array</em>) .</li></ul><p>Let’s discuss each one of the PLDs variants.</p><h3>PLA</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*Z5iBZEyJCo318FNtWaUedA.png" /><figcaption>PLA schematic example</figcaption></figure><p>A <strong>programmable logic array</strong> (<strong>PLA</strong>) introduced by <em>Texas Instruments</em>, it is a kind of programmable logic device used to implement combinational logic circuits. The PLA has a set of programmable AND gate planes, which link to a set of programmable OR gate planes, which can then be conditionally complemented to produce an output. This layout allows for many logic functions to be synthesized in the sum of products canonical forms.</p><h3>PAL</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*bVXhP6W5hgCfJIMUD-RGrA.jpeg" /><figcaption>MMI PAL 16R6 in 20-pin DIP</figcaption></figure><p><strong>Programmable Array Logic</strong> (<strong>PAL</strong>) devices introduced by <em>Monolithic Memories, Inc. (MMI)</em>, consisted of a small PROM (programmable read-only memory) core and additional output logic used to implement particular desired logic functions with few components.</p><p>PAL devices have arrays of transistor cells arranged in a “fixed-OR, programmable-AND” plane used to implement “sum-of-products” binary logic equations for each of the outputs in terms of the inputs and either synchronous or asynchronous feedback from the outputs.</p><h3>GAL</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*OCRuzkWXwe_iqWry6Gt65g.jpeg" /><figcaption>Lattice GAL16V8D-15LJ</figcaption></figure><p>The <strong>Generic Array Logic</strong> (<strong>GAL</strong>) device was an innovation of the PAL and was invented by <em>Lattice Semiconductor</em>. The GAL was an improvement on the PAL because one device type was able to take the place of many PAL device types or could even have functionality not covered by the original range of PAL devices. Its primary benefit, however, was that it was eraseable and re-programmable, making prototyping and design changes easier for engineers.</p><h3><strong>CPLD</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*i56JyGoZnzMRr5YbY-1tvQ.jpeg" /><figcaption>An Altera MAX 7000-series CPLD with 2500 gates.</figcaption></figure><p>PALs and GALs are available only in small sizes, equivalent to a few hundred logic gates. For bigger logic circuits, <strong>complex programmable logic device</strong> (<strong>CPLD</strong>) can be used. These contain the equivalent of several PALs linked by programmable interconnections, all in one integrated circuit. CPLDs can replace thousands, or even hundreds of thousands, of logic gates.</p><h3>FPGA</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*GdFLUpIdMJOCAMKibDSbYw.jpeg" /><figcaption>A Stratix IV FPGA from Altera</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*bZ93-hTrCj63vMDU7Y8Slw.jpeg" /><figcaption>A Spartan FPGA from Xilinx</figcaption></figure><p>While PALs were being developed into GALs and CPLDs, a separate stream of development was happening. This type of device is based on gate array technology and is called the <strong>Field-programmable Gate Array (FPGA)</strong>. Introduced by <em>Signetics</em>.</p><p>FPGAs use a grid of logic gates, the term “field-programmable” means the device is programmed by the customer, not the manufacturer.</p><p>The difference between FPGAs and CPLDs is that FPGAs are internally based on look-up tables (LUTs) whereas CPLDs form the logic functions with sea-of-gates (e.g. sum of products). CPLDs are meant for simpler designs while FPGAs are meant for more complex designs. In general, CPLDs are a good choice for wide combinational logic applications, whereas FPGAs are more suitable for large state machines such as microprocessors.</p><p><em>That’s it! :)</em></p><p><em>I Hope You Like It. Please make sure to follow to get the updates.</em></p><p><em>You can also join me on </em><a href="https://www.linkedin.com/in/driouechemed/"><em>LinkedIn</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b15a6474276e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Terms You Must Know Before Starting Any Android Project]]></title>
            <link>https://medium.com/@drioueche.med/terms-you-must-know-before-starting-any-android-project-af891cb21a36?source=rss-53aa763d48fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/af891cb21a36</guid>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Drioueche Mohammed]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 20:00:17 GMT</pubDate>
            <atom:updated>2020-01-26T14:21:37.171Z</atom:updated>
            <content:encoded><![CDATA[<h3>Terms You Must Know Before Starting Any Android Project Using Android Studio</h3><h4>Introduction To Android Studio Project Tree</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Lb20H-oZgQ5H7v_V" /><figcaption>Photo by <a href="https://unsplash.com/@robin_rednine?utm_source=medium&amp;utm_medium=referral">ROBIN WORRALL</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>With the age of digitization, the need for more mobile apps become higher and higher. Even the number of Android developers and the applications released, the market isn’t satisfied yet. So if you want to start your journey as an Android developer, THIS IS YOUR <strong>CHANCE</strong>!</p><p><em>This article is based on my own gaps that I faced during my starting journey in Android development. By the end of this article you are going to have the basic knowledge to start your first Android application. </em><strong><em>So let’s get started.</em></strong></p><p>You want to start your Android project, so you have just set up your computer, by installing your Android Studio IDE, creating your project, and picking JAVA as the language you are going to use, then you find yourself in front of a project tree which looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*wqR0klLTD2jx57n2tBTK9w.png" /><figcaption>Android Project Tree</figcaption></figure><p>then your curiosity starts asking about what is the purpose of every folder. The next lines will answer this question, so let’s define the basic terms given in this project tree.</p><h4>Activity</h4><p>It’s the first term you are going to face in each project you start, so if you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, the Android system initiates its program within an <strong>Activity</strong> starting with a call on onCreate() callback method.</p><p>An activity is a single, focused thing that the user can do. Almost all activities interact with the user.</p><h4>Manifest</h4><p>Every app project must have an AndroidManifest.xml file (with precisely that name <em>-3rd line of the project tree-</em>).</p><p>The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.</p><p>NB. Each activity class should also be declared in the Manifest file.</p><h4>Folder JAVA &amp; activity.java</h4><p>In this folder, you can write your activity classes and java classes.</p><p>For the <strong>activity class</strong>, it should extend AppCompatActivity() to mention into the compiler that is an activity, override void onCreate(Bundle savedInstanceState)which is the method called when an activity starting. In this method, you can connect to activity with her own view by setContentView(R.layout.&lt;activity_main&gt;);, add listeners for User Interface (UI) components, collect data form a UI component, generate UI Components dynamically, etc...</p><p><strong>Bundle</strong> is generally used for passing data between various Activities. Bundle can hold all types of values and pass to the new activity.</p><p><strong>Intent</strong> is a simple message object that is used to communicate between Android components. Intents are also used to transfer data between activities.</p><p><strong>R Class</strong> is the dynamically generated class, created during build process to dynamically identify all assets (from strings to Android widgets to layouts), for usage in java classes in Android app.</p><h4>Resources Folder (res)</h4><p>The<strong> Drawable folder </strong>contains multiple drawables (for example launching icon of the application). And a <strong>Drawable</strong> is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen.</p><p>The <strong>Layout folder </strong>contains files written in XML which defines the structure for a UI in your app, such as in an activity.</p><p>The <strong>values folder</strong> is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions, etc.</p><h4>Gradle</h4><p>Finally, the last term of our list is Gradle. Gradle is an automated build toolkit (open source) that is used to automate building, testing, deployment, etc… Gradle brings a number of powerful features to building Android application projects.</p><p><em>That’s it! :)</em></p><p><em>I Hope You Like It. Please make sure to follow to get the updates.</em></p><p><em>You can also join me on </em><a href="https://www.linkedin.com/in/driouechemed/"><em>Linkedin</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af891cb21a36" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>