I'm a PHP developer coding in the Yii 1.x framework. I was looking for a way to encode unescaped JSON in Yii 1.x, and found the CJSON framework class for this purpose (so OOP).Since it does not support unescaped JSON though, I had to revert back to the pure PHP, procedural, non-OOP approach of using json_encode($results, JSON_UNESCAPED_SLASHES);. However, I asked how to achieve the same with the Yii framework.As an answer I received information that, while it does what I want, is not possible with the base framework and requires that I extend a base class. This proposed solution requires 12 lines of code and involves creation of a separate file, while my solution requires just 1 new line of code.Just to feed my curiosity - what is more important in situations like this? Should I follow KISS and make my code as simple as possible, even reverting to procedural code, or should I stick to OOP solutions and always extend classes if I can't do what I need with existing framework code?
Is the KISS principle more important than utilizing OOP to solve a problem?
object oriented
Comparing procedural code and OOP is like comparing apples and oranges. Sometimes, one leads to a better design, sometimes the other and sometimes neither.In languages that support a mixture of OO and procedural code (which is the large majority of OO languages), it can make sense to sub-class an existing class ifthe base class is open for extension (not sealed, final, whatever it is called in your language of choice), andyour extension must be used by another class, that takes (a reference to) the base class as dependency, oryour extension is applicable only in some situations, but it must also seamlessly handle the situations that the base class caters for, oryour extension needs access to parts of the base class, orthe code using the extension will mostly use it in conjunction with the base class.If none of that holds, then you should go for whatever leads to the simplest code, be it a class, extension or procedure (or just a procedure call in this case).