procedural programming - ibrahimrifats/Back-End-development GitHub Wiki

Procedural Programming in Python - Enhance Note

Introduction

Developers have the freedom to structure their code in various ways. Python supports multiple programming models or paradigms, such as object-oriented, procedural, and functional programming. In this note, we will focus on procedural programming, which involves writing step-by-step instructions for the program to execute. It serves as a crucial foundation for understanding object-oriented programming, making it essential for new developers to learn.

Structuring Code with Procedural Programming

The primary purpose of a programming model is to structure code effectively. This structure enables easier code updates and the creation of new functionality. Procedural programming structures code into procedures, also known as subroutines or functional sections. Each procedure contains logical steps to accomplish a specific task, such as adding two numbers to return their sum.

Code Reusability and DRY Principle

An important concept in procedural programming is code reusability. Creating functions that can be reused throughout the application is crucial to adhere to the "Don't Repeat Yourself" (DRY) principle, which aims to reduce code duplication. Instead of writing separate code for each set of numbers to add, we can create a function that accepts arguments and returns the sum. This approach results in less code and promotes code reusability.

Example: Calculating Bill Total and Tax

Let's examine an example where we calculate the total of a bill and add tax to it. The code is presented in four sections, each serving a specific purpose:

  1. Bill Total Function: Accepts a bill as a parameter and calculates the total bill by looping through its items.
  2. Calculate Tax Function: Accepts the percentage and bill total as parameters, returning the amount of tax to be added to the bill (rounded off to two decimal places).
  3. Food Bill: Represents a customer's bill, which is static but could be changed to accept dynamic user input to create a bill.
  4. Main Sections: We call the two functions to calculate the bill and tax, then print each amount and the overall total.

Reducing Code Footprint with Procedure Programming

By using procedural programming, we can significantly reduce the code footprint. Each subroutine or functional section can be reused in other parts of the code, resulting in a more efficient and maintainable program. In the example, we observe how different sections reuse one another, promoting code reusability and maintainability.

Advantages of Procedural Programming

  • Ease of Learning: Procedural programming is easy for beginners to grasp and start coding with.
  • Code Reusability: Procedures can be reused in different parts of the code, leading to more efficient development.
  • Readability: Breaking down code into specific tasks makes it easy to understand and maintain.

Disadvantages of Procedural Programming

  • Maintenance Challenges: As the program grows, maintaining and extending the code can become challenging.
  • Limited Real-World Object Representation: In some cases, procedural programming may not relate well to real-world objects or entities.
  • Data Exposure: Data may be exposed throughout the entire program, potentially leading to security and privacy issues.

Conclusion

Procedural programming in Python provides a straightforward and practical approach to programming, making it an excellent starting point for new developers. Understanding the advantages and disadvantages of this paradigm will help developers make informed decisions when choosing the most suitable approach for specific coding tasks. As you progress in your programming journey, you'll gain the expertise to select the best paradigms for different scenarios and develop efficient and maintainable code.