Programming Paradigms (Copy)
Introduction to Programming Paradigms
- A programming paradigm is a set of programming concepts and methodologies that define how a programming language structures and executes commands.
- Different programming paradigms provide various approaches to problem-solving in programming.
- Some programming languages strictly adhere to one paradigm, while others support multiple paradigms.
- Example: JavaScript is a single-paradigm language, while Python supports multiple paradigms.
- Four major programming paradigms covered:
- Low-level programming
- Imperative programming
- Object-oriented programming (OOP)
- Declarative programming
20.1.1 Low-Level Programming
- Definition: Uses instructions from the computer’s basic instruction set.
- Types:
- Machine code: Direct binary instructions executed by a CPU.
- Assembly language: Uses mnemonics instead of binary but still closely tied to the hardware.
- Characteristics:
- Direct interaction with computer memory and processor.
- Requires detailed knowledge of hardware architecture.
- Difficult to read, write, and debug but highly efficient.
- Use Cases:
- Developing device drivers.
- Writing firmware.
- Optimizing critical system software.
- Example: Writing a printer driver that interacts with hardware registers directly.
20.1.2 Imperative Programming
- Definition: Defines a sequence of commands executed step-by-step in a specific order.
- Also Known As: Procedural programming.
- Characteristics:
- Instructions execute in a predefined sequence.
- Uses variables, loops, and conditionals to control execution.
- Utilizes procedures and functions to break down code into reusable parts.
- Structured Programming:
- An evolved form of imperative programming with better code organization.
- Uses local and global variables for data management.
- Makes debugging and maintenance easier.
- Advantages:
- Simple and easy to learn, making it suitable for beginners.
- Efficient for small programs.
- Less memory-intensive than other paradigms.
- Disadvantages:
- Becomes difficult to manage in large-scale programs.
- Poor scalability.
- Example: A basic program in Python that calculates the sum of numbers:
total = 0 for i in range(1, 11): total += i print("Sum:", total)
20.1.3 Object-Oriented Programming (OOP)
- Definition: Uses self-contained objects that contain both data (attributes) and behaviors (methods).
- Core Concepts:
- Class: A blueprint defining the properties and methods of objects.
- Object: An instance of a class containing specific data.
- Encapsulation: Data and methods are wrapped together to restrict unauthorized access.
- Inheritance: A class (subclass) can inherit attributes and methods from another class (superclass).
- Polymorphism: Methods can be overridden or redefined in derived classes.
- Data Hiding: Protects object integrity by restricting access to internal data.
- Advantages:
- Promotes modularity and reusability.
- Simplifies maintenance and debugging.
- Supports large-scale applications.
- Disadvantages:
- Higher memory usage compared to imperative programming.
- More complex to learn and implement.
- Example: Python implementation of an Employee class:
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def display_info(self): print(f"Employee: {self.name}, Salary: {self.salary}") emp1 = Employee("John", 50000) emp1.display_info()
20.1.4 Declarative Programming
- Definition: Focuses on describing what should be done rather than how to do it.
- Key Characteristics:
- Uses statements of facts and rules.
- The execution mechanism derives solutions by evaluating queries against facts and rules.
- Commonly used for database queries and logic-based programming.
- Types:
- Functional Programming: Uses pure functions without mutable states.
- Logic Programming: Uses logic rules and facts to infer solutions.
- Advantages:
- Code is often shorter and more readable.
- Reduces side effects in program execution.
- Well-suited for parallel processing.
- Disadvantages:
- Less intuitive for beginners.
- Debugging can be complex.
- Examples:
- SQL Query (Database):
SELECT Name FROM Employees WHERE Salary > 50000; - Prolog Fact and Rule (Logic Programming):
language(python, oop). language(prolog, declarative).
- SQL Query (Database):
Comparison of Programming Paradigms
| Feature | Low-Level Programming | Imperative Programming | Object-Oriented Programming | Declarative Programming |
|---|---|---|---|---|
| Complexity | High | Medium | High | Medium |
| Performance | Very High | High | Moderate | Low to Moderate |
| Ease of Use | Difficult | Easy | Moderate | Moderate to Difficult |
| Scalability | Low | Moderate | High | High |
| Modularity | Very Low | Moderate | Very High | High |
| Memory Usage | Low | Low | High | Low |
| Best Used For | System-level tasks | Small-scale apps | Large applications | Database queries, AI |
Conclusion
- Programming paradigms define how programs are structured and executed.
- Different paradigms serve different use cases and have their own advantages and disadvantages.
- Low-level programming is hardware-specific and efficient but complex.
- Imperative programming is straightforward and widely used for simple applications.
- Object-oriented programming is best for large, modular, and scalable applications.
- Declarative programming is effective for logic-based tasks and database management.
- Multi-paradigm languages like Python allow developers to use the best paradigm for a given task.
