Member-only story
Why Use Classes Instead of Just a Bunch of Functions?
As a self-taught programmer, I used to wonder — why do we need classes in programming? Wouldn’t a bunch of reusable functions be enough?
At first, the idea of object-oriented programming (OOP) seemed unnecessary. I could write functions that performed tasks and call them whenever needed. But as my projects grew, I started running into code duplication, scattered logic, and difficulty managing state. That’s when I realized the true power of classes
Why Not Just Use Functions?
Functions are great! They promote code reusability and help break down a problem into smaller, manageable chunks. However, when functions start depending on shared data, things can quickly get messy.
Consider this simple example without classes:
# Using separate functions with global variables
user_name = "Alice"
user_age = 30
def get_user_info():
return f"User: {user_name}, Age: {user_age}"
def birthday():
global user_age
user_age += 1
While this works, it has problems:
- Functions depend on global variables, making the code harder to debug and modify.
- If we want multiple users, we’ll need separate variables (
user_name2
,user_age2
), leading to duplication.