<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul AJadi</title>
    <description>The latest articles on DEV Community by Paul AJadi (@ajadi473).</description>
    <link>https://dev.to/ajadi473</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F478126%2F5546e15b-a16c-4c2d-afa8-fb930957d7d0.png</url>
      <title>DEV Community: Paul AJadi</title>
      <link>https://dev.to/ajadi473</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajadi473"/>
    <language>en</language>
    <item>
      <title>Writing Clean, Efficient, and Maintainable Code - Python as use case.</title>
      <dc:creator>Paul AJadi</dc:creator>
      <pubDate>Thu, 26 Sep 2024 13:29:40 +0000</pubDate>
      <link>https://dev.to/ajadi473/writing-clean-efficient-and-maintainable-code-python-as-use-case-47a</link>
      <guid>https://dev.to/ajadi473/writing-clean-efficient-and-maintainable-code-python-as-use-case-47a</guid>
      <description>&lt;p&gt;Writing Clean, Efficient, and Maintainable Code - Python as use case.&lt;br&gt;
If you have been a developer for some time, you may understand the importance of proper coding as against the rush of just writing somthings that works. One of the greatest setback this has for you is when you need to review your code you rarely can make sense of what you were trying to do and for others, they have a hard time reading your code. &lt;/p&gt;

&lt;p&gt;I recently have been reviewing some codes and I think it is important to stress the need to clean, efficien code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Some history to guidelines and styles in coding
&lt;/h2&gt;

&lt;p&gt;In the early 1970's when the C Programming language was gaining popularity coding style become a matter of discussion and two notable names around this were Brian Kernighan and Dennis Ritchie.&lt;/p&gt;

&lt;p&gt;Interestingly, Ada language enforced strict coding guidelines and standards in the rearly 1980's. Mid 1990's Python and Java came in with strong coding conventions from the initial phase and Python Enhancement Proposal (PEP 8) was well adopted which today is still a valid coding convention.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PEP 8 Guidelines for Code Styling:
I have used this guideline everynow and then and I have seen other programmers adopt this. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;VS Code's flake8 code linting extension is a great choice I use. &lt;br&gt;
The guidelines are:&lt;br&gt;
Indentation: Use 4 spaces per indentation level.&lt;br&gt;
Line Length: Limit all lines to a maximum of 79 characters.&lt;br&gt;
Naming Conventions: Use snake_case for function and variable names and CamelCase for class names.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`# defining variables using snake_case

total_price = 100
customer_email = "customer@example.com"

# defining functions using snake_case
def calculate_total_price():
    pass

class OrderManager:
    pass`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Meaningful comments and documentations:&lt;/strong&gt; Docstrings helps developers understand a function/methods purpose, paramters and return values. It is generally good practice to introduce them and writing comments to clarify a complex logic or decision&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def calculate_total_price(price: float, quantity: int) -&amp;gt; float:
    """
    Calculate the total price of items.

    Args:
        price (float): The price of a single item.
        quantity (int): The number of items purchased.

    Returns:
        float: The total price.
    """
    return price * quantity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**3. Code Repititions: **As a simply rule of thumb avoid repeating a code that's already implemented, instead follow the DRY principle by making reusable functions. The caveat to this is, when a new developer implements a change, the developer might not know there is a duplicated code that has been left out and can mess with a whole lot owing to this error.&lt;/p&gt;

&lt;p&gt;For example, you have a function that calculates total price for an ecommerce store by multiplying the price and quantity.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;total_price_laptops = 1000 * 2
total_price_phones = 500 * 3
This, is easily done with reusable functions like this:
def calculate_total(price, quantity):
    return price * quantity

total_price_laptops = calculate_total(1000, 2)
total_price_phones = calculate_total(500, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Type Hints:&lt;/strong&gt; Python is dynamically typed language, type hinting your code improve readability and catch errors early. It is easier to understand what data types are expected.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_discounted_price(price: float, discount: float) -&amp;gt; float:
    return price - (price * discount)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**5. Writing Tests: **Tests validate the functionality of your code. Popular frameworks like unittest or pytest make writing and running tests simple. Testing asserts that your code behaves as expected, reducing the risk of introducing bugs when making changes or refactoring.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def test_calculate_total_price():
    assert calculate_total_price(100, 2) == 200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Exceptions:&lt;/strong&gt; It can be embarassing when production codes print out lines of meaninglesss code to client users or even worse, output environmental variables. Avoiding this is easier with exception try/except/finally handling to manage predictable errors and ensure your application doesn't crash unexpectedly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try:
    value = int(input("Enter a number: "))
except ValueError:
    print("That's not a valid number!")
finally:
    print('Thank you!')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. Code Modularization:&lt;/strong&gt; There is no rule that says to keep your codes in one file. Break down large blocks of code into smaller, reusable modules and import them across the application where neede. It is easier to test and update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Version Control:&lt;/strong&gt; Version control systems like Git is a fundamental best practice in modern development. Others are Beanstalk, PerForce, Apache subversion. Version controls can track changes, reduce human errors during collaborations and unintended consequences to a great extent.&lt;/p&gt;

&lt;p&gt;By following best practices like adhering to PEP 8, keeping your code DRY, writing meaningful documentation, and handling exceptions gracefully, you can ensure that your Python projects are professional, scalable, and easy to maintain. &lt;br&gt;
Start implementing these tips today, and you'll see a noticeable improvement in the quality of your code.&lt;/p&gt;

</description>
      <category>coding</category>
      <category>python</category>
      <category>git</category>
    </item>
    <item>
      <title>Understanding Database Normalization: A Practical Guide with E-Commerce Examples</title>
      <dc:creator>Paul AJadi</dc:creator>
      <pubDate>Sat, 14 Sep 2024 17:59:53 +0000</pubDate>
      <link>https://dev.to/ajadi473/understanding-database-normalization-a-practical-guide-with-e-commerce-examples-3jln</link>
      <guid>https://dev.to/ajadi473/understanding-database-normalization-a-practical-guide-with-e-commerce-examples-3jln</guid>
      <description>&lt;p&gt;In database design, one of the most fundamental and basic concepts is normalization. It gets confusing at some point, and it has taken some time for me to grasp how to normalize a database through recurrent practice.&lt;/p&gt;

&lt;p&gt;The primary goal of normalization, I believe, is to eliminate redundancy and ensure data integrity. In the end, normalization leads to efficient data retrieval and simplifies database maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Database Normalization?
&lt;/h2&gt;

&lt;p&gt;Database normalization involves structuring a relational database to reduce repetitive data/redundancy and improve data integrity. It's typically done by splitting large tables into smaller tables, related using foreign keys, which ensures that data is stored only once.&lt;/p&gt;

&lt;p&gt;Normalization is carried out through a series of stages/normal forms. Each form introduces more rules and restrictions to further organize the data. &lt;br&gt;
There are five major normal forms (1NF - 5NF), but in practice, the first three normalize a database in a real-world scenario.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is normalization important?
&lt;/h2&gt;

&lt;p&gt;Normalizing databases helps avoid these anomalies, especially with systems that perform CRUD operations:&lt;/p&gt;

&lt;p&gt;Data redundancy: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Repititing the same data in multiple places (1 or more tables).&lt;/li&gt;
&lt;li&gt;Update anomalies: Owing to having multiple entries, it poses difficulty updating redundant data in multiple places and can lead to inconsistency with data.&lt;/li&gt;
&lt;li&gt;Insert/Delete anomalies: You cannot trust adding or removing certain data without having other entries already present.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Normal Forms Overview
&lt;/h2&gt;

&lt;p&gt;Normalization is done in forms/stages, each referred to as a normal form.&lt;br&gt;
First&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Normal Form (1NF): Ensures each column contains indivisible values and that there are no repeating groups. This is called ATOMICITY&lt;/li&gt;
&lt;li&gt;Second Normal Form (2NF): Builds on 1NF, ensuring that all non-key attributes are fully dependent on the primary key.&lt;/li&gt;
&lt;li&gt;Third Normal Form (3NF): Ensures that non-key attributes are not only dependent on the primary key but also independent of each other.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Normalization in an E-commerce Database: An Example
&lt;/h2&gt;

&lt;p&gt;Let us consider a basic e-commerce system where customers place orders for products. I usually will advise having all required fields written out on a pen or a wordpad.&lt;br&gt;
An initial database structure would look like this:&lt;br&gt;
Unnormalized Table or the Flat Structure&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CustomerName (varchar), CustomerEmail (varchar), ProductID (varchar), quantity, price (decimal), orderdate (date), etc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Three Normal Forms&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First Normal Form (1NF): Eliminate Repeating Groups
A table is in 1NF when:
All columns contain &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;atomic (indivisible) values.&lt;/li&gt;
&lt;li&gt;Each record has a unique identifier, known as a primary key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i6zep2t6m2qi3fe21ct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i6zep2t6m2qi3fe21ct.png" alt="Image description" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table 1&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2m0lphb9t7k8lka9obb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2m0lphb9t7k8lka9obb.png" alt="Image description" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table 2&lt;/p&gt;

&lt;p&gt;Consider the two tables above:&lt;br&gt;
Table 1 is in its normalized form, while Table 2 has the Products and Quantity columns, which contain multiple values; this violates 1NF.&lt;/p&gt;

&lt;p&gt;To normalize Table 2, we split the table into two separate tables: orders and order_items.&lt;br&gt;
After 1NF we have tables 1 and 2 as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81wv4d4id24b4kdlkmi8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81wv4d4id24b4kdlkmi8.png" alt="Image description" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First normal form&lt;br&gt;
In 1NF, each field contains atomic values, and we've eliminated repeating groups of products within a single order.&lt;br&gt;
Table 1 seems straightforward but has some problems:&lt;br&gt;
Data redundancy: CustomerName, CustomerEmail is repeated for every order. Ideally, since there will be multiple entries, they can go into a table, say the customer information table, but we will be back to this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Second Normal Form (2NF): Eliminate Partial Dependency
A table is in 2NF when:
It meets all the requirements of 1NF/Passed 1NF.
All non-primary-key attributes are fully dependent on the primary key.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order_items table, from the 1NF, the OrderID and ProductName together can form a composite key (data modeling technique that combines multiple columns to create a unique identifier for each record), but Quantity is already dependent on the combination of the two attributes. (OrderID and ProductName).&lt;br&gt;
This means, however, that if we have data in the table that doesn't relate to the order, we would need to move it to a different table.&lt;br&gt;
ProductPrice is dependent on ProductName, and not OrderID. Just like the price of products in a store does not determine orders made.&lt;br&gt;
Therefore, to achieve 2NF, we need to separate product-related data from order-related data. We create a new products table.&lt;br&gt;
By moving ProductPrice and ProductName to a new Products table, we ensure that every non-key attribute in the OrderItems table depends on the the composite key (OrderID, ProductID). This satisfies the goal of the second normal form.&lt;br&gt;
After 2NF:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4etx0x61w7g51qcpcyin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4etx0x61w7g51qcpcyin.png" alt="Image description" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table 2 in 2NF&lt;br&gt;
Now, ProductPrice is stored in the products table, which keeps product data separate from order data, ensuring that each piece of data is only stored once.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Third Normal Form (3NF): Eliminate Transitive Dependency
A table is in 3NF when:
It is already in 2NF/Passed 2NF.
All non-key attributes are dependent only on the primary key, and there are no transitive dependencies (i.e., non-primary-key attributes should not depend on other non-primary-key attributes).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m4bheryb5i2bvi8l6ed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m4bheryb5i2bvi8l6ed.png" alt="Image description" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table 2 in 3NF&lt;br&gt;
Suppose we have a CustomerAddress, for orders, it will be in the Customers Table and is dependent on CustomerName, not OrderID. This is the concept of transitive dependency, meaning the address is indirectly dependent on the order.&lt;br&gt;
To achieve 3NF, we move the customer data into CustomersTable, we have an OrdersTable, ProductsTable, and OrderDetailsTable at this point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Normalization
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;- Data Integrity: Normalization ensures that the data is stored consistently. So, if a customer changes their name, you only need to update it in the Customers table and it reflects on all related tables.&lt;/li&gt;
&lt;li&gt;- Reduction in Redundancy: By eliminating duplicate data, storage requirements are reduced. This is crucial in asystem where thousands of products, customers, and orders are stored.&lt;/li&gt;
&lt;li&gt;- Efficient Queries: Normalization helps in writing efficient queries and data is stored logically in separate tables, reducing the chances of anomalies.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Disadvantages of Normalization
&lt;/h2&gt;

&lt;p&gt;While normalization offers many advantages, it can also introduce some challenges:&lt;br&gt;
Complex Querie&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;s: As data is spread across multiple tables, queries may become more complex and require more JOIN operations, potentially impacting performance.&lt;/li&gt;
&lt;li&gt;Overhead: Managing relationships between normalized tables requires proper indexing and careful query optimization.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Denormalization: A Trade-Off for Performance
&lt;/h2&gt;

&lt;p&gt;In certain or specific scenarios, especially in high-performance systems, some denormalization (reintroducing redundancy) may be necessary to optimize read performance. For example, storing the product name directly in the OrderDetails table can eliminate the need for a JOIN in read-heavy applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Normalization is an essential process in designing a relational database that is efficient, scalable, and maintainable. Normalization ensures that data such as customer details, products, and orders are stored in a way that minimizes redundancy, prevents anomalies, and maintains data integrity, create a well-structured database that supports the smooth operation.&lt;/p&gt;

&lt;p&gt;However, while it is great to normalize, keep in mind the trade-off between normalization and performance, especially where necessary to optimize read-heavy applications.&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>normalization</category>
    </item>
  </channel>
</rss>
