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?
4
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 if
- the base class is open for extension (not sealed, final, whatever it is called in your language of choice), and
- your extension must be used by another class, that takes (a reference to) the base class as dependency, or
- your extension is applicable only in some situations, but it must also seamlessly handle the situations that the base class caters for, or
- your extension needs access to parts of the base class, or
- the 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).
Just to feed my curiosity, I’d like to ask, what is more important in situations like that? Should I follow KISS and make my code as simple as possible, even reverting sometimes to procedural code, or should I bound myself to OOP and always use extended classes, if I can’t get, what I want, out of base code?
As Tim Peters said “Practicality beats purity”.
Unless you have strict requirements one way or another (e.g. an obsessive-compulsive code reviewer with a gun, that knows where you live) you should optimize your code for maintenance. In other words, choose the one that is easier to figure/maintain/extend/reuse/fix.
That said, this is not a KISS vs. OOP issue as much as it is a “practicality vs. purity” issue (the best OOP code is simple, not in opposition to KISS).
1
Would a random (adequately skilled) programmer, tasked with making changes to your code readily identify what you were doing one way or the other? Would potential future changes be easier or harder one way or the other?
Using your example above, I would prefer to find an extended class. This would indicate to me that the logic related to the unescaped JSON was there and not somewhere else. It would also allow for easier re-use of the same bit of logic somewhere else.
1