1

I am writing a program using a procedural style.

At most I have some modules where the logic is present (one to retrieve data from, one to display the info, one with the saving logic, etc.), but my data is in plain object format (they are formally classes, but conceptually they are structs, or named tuples).

All is fine, but I have to use this library, which is heavily object oriented (it offers me custom objects I have to use, and I have to subclass and implement interfaces to get my stuff done).

Would it be a sensible approach to "wrap" this library in a procedural way? Or do I have to change the style of my program completely to "OOP" if I use an OO library in my program?

  • 4
    There's definitely no generic answer we can give that would apply to all OO libraries in procedural code (not to mention most people can't agree on where "procedural" ends and "OO" begins). Could you describe the "OO API" you're given and the kind of code you'd prefer to write in more detail so we can tell where exactly the problem lies? – Ixrec Mar 11 '16 at 10:32
  • You'll have to deal with the object oriented part in order to "wrap" it anyway. – Tulains Córdova Mar 11 '16 at 11:16
  • This is highly language specific. I would recommend to ask the question again on stackoverflow.com . Put in some code on how far you got already. Programming in a procedural way while still using objects is totally possible. You would use the object more as structures then as objects this way though. – Pieter B Mar 11 '16 at 11:34
  • 1
    Consider to give an example case (by naming the language & library you have in mind).@PieterB: what you suggest is ok, but I think this topic can be discussed an a conceptual level as well, with an answer which might be valid for many languages and libs. – Doc Brown Mar 11 '16 at 14:43
  • "procedural object" is a paradox. Did you mean globally defined struct? – SparK Mar 11 '16 at 15:00
4

You can easily write a procedural wrapper around method calls like object.doStuff(parameter) by creating global procedures like:

 void doStuffWith(object, parameter) {
        object.doStuff(parameter);
 }

But when you have an API which forces you to implement interfaces and extend superclasses to make use of it, you won't get around writing some own classes. When you want to write as few object-oriented code as possible, you could have the method implementations of these classes do little more than call a global function to which they pass the method parameters and the relevant state of the object.

 class MyStuffHandler implements StuffHandler {
      public Stuff stuff;
      public void doStuff(parameters) {
          doStuffWith(this.stuff, parameters);
      }
 }

But the question is "Is it worth the effort?". And usually the answer is no. There is not much wrong with mixing procedural and object-oriented code. Most arguments against this usually boil down to arguments against procedural or object-oriented programming in general.

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.