Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does reflection stop being worth it?

I just refactored a script with about twelve, nearly identical, one-liners to one that uses reflection to dynamically bind static methods to a class.

The refactored version can be found here. And before refactoring here.

My question is: Does this seem over-engineered? Am I chasing some academic elegance that is in practice, worse than the obvious way? The refactored form is way shorter (about 70 lines) and more "beautiful" (for some defined notion of beauty), but a novice programmer might not understand it at all.

like image 890
yarian Avatar asked Oct 06 '22 04:10

yarian


1 Answers

One problem with the "naive" approach is maintainability - you have 12 times more methods to maintain, debug and test. Imagine you need to add an extra parameter to all of them... with time, the methods will become very similar but not exactly the same. So the "complicated" approach may pay off with time.

By the way, there's a bug in one of the 28 "naive" methods which is not present in the rest 27 of them :)

like image 156
Sergey Avatar answered Oct 10 '22 03:10

Sergey