Member-only story
Functional vs Object-Oriented Programming
In scientific computing, you often start writing scripts quickly to get results – maybe a quick Python loop to process CSV files or a NumPy function to simulate an experiment. Over time, your code grows. You start asking:
Should I keep writing my scripts as functions, or should I organize them into classes?
These two styles – functional programming and object-oriented programming (OOP) – are not mutually exclusive. But understanding their differences will help you write clearer, more maintainable code.
1. The Scenario: Simulating a Population
Let’s imagine we are scientists modeling a simple population of organisms.
Each organism:
• Has a name
• Has an energy level
• Can eat (gain energy)
• Can move (lose energy)
We will implement this in both functional and OOP styles.
2. Functional Programming Approach
In functional programming:
• Data is stored in simple structures (like dictionaries).
• Functions operate on that data.
