1

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?

  • one may argue that conceptually, this has been addressed in How would you know if you've written readable and easily maintainable code? If your peers keep complaining about your way of doing things, be it one way or another, you better change to make them feel better – gnat Sep 25 '14 at 13:34
  • 2
    I think it's very rare in a well-designed language/framework that following good OOP principles is in opposition to KISS. I can't speak to this particular example as I don't know much about PHP or Yii. – Ben Aaronson Sep 25 '14 at 13:40
  • @gnat As I said, this is question to feed my curriosity. No one is actually pushing me to anything. :] – trejder Sep 25 '14 at 13:43
  • CJSON::encode() is a static method, so the class is little more than a namespace. Being attached to a class doesn't somehow make it better than using a free function. – Doval Sep 25 '14 at 16:07
2

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

4

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
    +1 for a good answer and a great fun, though an obsessive-compulsive code reviewer with a gun, that knows where you live is worth at least +5! :> – trejder Sep 25 '14 at 21:49
3

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.

  • While I agree with you in general (+1), in this particular example (logic already known, to plans for future re-use), class seems to be a little bit overkill IMHO. – trejder Sep 25 '14 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.