Oleksii Chekulaiev
1 min readFeb 25, 2022

--

People have to rediscover "magic" setters/getters every now and then, and write articles praising them, until one day they have to debug the code where some silly programmer who read a lot of articles like that abused those magic methods so your head would explode on why your house gets automatically sold online when you set wrong amount of wood in the fireplace. Then they realize how evil this unobvious logic is.

If you need to use validation on setting a value, you better just use a function, so that later it would be obvious there is something else happening when you set a value.

car.set_fuel_amount(...) is much better at reading it and using it, than car.fuel_amount=... that throws an exception whenever it feels like it.

Worse you have to *know* that you need to process exceptions for setting one value, when another might be just a regular object field.

tl;dr: avoid using magic functions in favor of regular functions, except when it is a one-off thing that does not affect much and fails gracefully.

--

--