Programming styles
Programming styles
radically altering the previously debugged program is one of the key benefits that distinguishes it from
Structured Programming. To understand this better, let’s compare the two paradigms in terms of
modification flexibility and how they handle changes in the system.
Adding data and functions in procedural language is relatively difficult when we compare with object
oriented languages. In procedural programming, adding new data and related functions often requires
modifying multiple sections of code and possibly duplicating or rearranging existing logic. In contrast,
object-oriented programming (OOP) encapsulates data and related functions within objects, making it
easier to extend and maintain.
Python needs to keep track of certain pieces of data that change over time as a program runs (like which
variables exist and what their values are, what has been printed to the screen, etc). We call this
information the program state.
so in oop we start at individual objects and we combine the individual objects to solve a problem but in
structure we start at main and from there we call subprocedures to solve a problem I got it?
Exactly! In OOP (Object-Oriented Programming), you start by designing objects that interact with each
other. Each object represents a real-world entity with its own attributes (data) and behaviors (methods).
You then combine these objects to build the overall system.
In structured programming, on the other hand, you start with a main function and break the problem
into smaller procedures (functions) that execute in a logical sequence.
So, OOP is bottom-up (building from objects), while structured programming is top-down (starting with
main and breaking down tasks). You nailed it!
In C, we use structures to define data, but we can’t directly associate functions with data. Functions are
implemented separately and take the structure as a parameter to operate on it.
#include <stdio.h>
typedef struct {
char owner[50];
double balance;
} BankAccount;
account->balance -= amount;
} else {
printf("Insufficient funds\n");
int main() {
deposit(&account1, 200.0);
withdraw(&account1, 100.0);
deposit(&account2, 300.0);
withdraw(&account2, 150.0);
Explanation:
Functions: Functions like deposit, withdraw, and calculate_interest are separate, and we pass a pointer
to BankAccount as a parameter.
Modularity: To add another account, we must create another BankAccount structure and call each
function with a pointer to the specific account.
In Python, we define a BankAccount class that encapsulates both the data (attributes) and behaviors
(methods). Each account can be created as an instance of the class.
class BankAccount:
self.owner = owner
self.balance = balance
self.balance += amount
self.balance -= amount
else:
print("Insufficient funds")
account.deposit(200)
account.withdraw(100)
print(f"Balance: {account.balance}")
Explanation:
Class: BankAccount is a class, meaning both data (e.g., owner and balance) and functions (e.g., deposit,
withdraw, and calculate_interest) are encapsulated within the class.
Instance Methods: Methods like deposit and withdraw act directly on the instance (self), allowing us to
manipulate data associated with the instance without needing global variables or external pointers.
Creating Accounts: New accounts are created by instantiating BankAccount, making it simple to handle
multiple accounts and additional functionalities if required.
In C: Functions are separate, requiring manual management of data pointers, making code more verbose
and less modular.
In Python: The object-oriented approach encapsulates data and behavior together, so adding
functionality or new instances of BankAccount is much easier and more scalable.
Let's say we have a BankAccount class with objects representing individual accounts. Each
account can receive messages (method calls) like deposit, withdraw, and transfer. When an
account transfers money to another, it sends a message to the recipient object.
python
Copy code
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
# Process: Increases balance
self.balance += amount
print(f"{self.owner} deposited ${amount}. New balance: $
{self.balance}")
Explanation
Paradigims:
This defines a data structure for recording dates. There are similar structures in many
procedural languages such as C, Ada and Pascal. So, what is wrong with a structure such as this?
Nothing, apart from the issue of visibility? That is, what can see this structure and what can
update the contents of the structure? Any code can directly access and modify its contents. Is this
problem? It could be, for example, some code could set the day to -1, the month to 13 and the
year to 9999. As far as the structure is concerned the information it now holds is fine (that is day
= 01, month = 13, year = 9999). This is because the structure only knows it is supposed to hold
integers; it knows nothing about dates per se. This is not surprising; it is only data.
The problem is that the data is naked and has no defense against what these procedures do to it.
Indeed, it has no defense against what any procedures that can access it, may do to it.