[NOTE] Abstraction
01 January 2024 | 1 min read
Notes on abstraction in programming
Abstraction is about what we expose to the outside world, and what details we hide.
Key ideas
- We present a nice easy to use interface to access information.
- We hide messy or unimportant implementation details.
- TV Remote: We see the buttons, but not the electronics inside.
Example
class NpcMage:
def __init__(self, health_points):
self._hp = health_points
self._buff_multiplier = 1.0
def get_total_health(self):
""" Get total hp with buffs applied """
return self._health * self._buff_hp
In this case we don’t care about the multiplier, only about the total health, which means we can abstract the calculation of the total HP away and hide it nicely behind a getter/method.