Is the KISS Principle More Important Than Utilizing OOP?

object-oriented

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?

Best Answer

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).

Related Topic