6

I have been working on a program to solve a Rubik's cube, and I found myself unsure of a couple of things. Firstly, if I have a bunch of functions that have general applications (e.g., clearing the screen), and don't know whether or not I should change them to methods inside of a class. Part of my code is OOP and the other part is procedural. Does this violate PEP8/is it a bad practice? And I guess in broader terms, is it a bad idea to mix two styles of programming (OOP, functional, procedural, etc)?

Jerry Wright
  • 77
  • 1
  • 2
  • There won't be any paradigm at all! – Elis Byberi Nov 22 '17 at 17:51
  • Post this to https://cs.stackexchange.com/ – Elis Byberi Nov 22 '17 at 17:53
  • PEP 8 is not really relevant here. I am voting to close this question because it is too opinion-based. Regardless, it is quite common to intermix module-level functions (what you are calling "procedural") with class definitions in Python. If it really ruffles your OOP feathers, just think of a module as a static class, and functions in that module as static functions on that static class. Voilà! You can now rest easy that you are writing fully object-oriented code, and not intermixing with dirty, procedural peasants. – juanpa.arrivillaga Nov 22 '17 at 17:55

1 Answers1

7

I would say it's not bad at all, if the problem can be solved more easily that way and that your language supports it.

Programming paradigms are just different approaches to solving problems:

  • Procedural says "What are the steps that need to be performed to solve this problem?"
  • Functional says "What values should be transformed and how should they be transformed to solve this problem?"
  • OOP says "What objects need to interact with one another and what messages are needed to be sent between them to solve this problem?"

When you divide up your problem into smaller ones, you might find that some parts of the problem can be solved more easily in a functional way, other parts a procedural way. It's totally possible to have such a problem.

Python is mainly procedural and quite OOP, and has functional features (namely functools). It's not as functional as Haskell and not as OOP as C#, but it allows you to use those paradigms to some extent.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Mixing them won't create a new paradigm? – Elis Byberi Nov 22 '17 at 17:59
  • At the risk of starting a flame war, the only reason I can think of that Python is "not as OOP as Java" is that Python doesn't force you to write everything in a class definition. But Java has primitive types, which to me seems to violate OOP more than not writing something in a class definition. In Python, *everything* is an object. But fundamentally, you are correct that programming paradigms are simply different approaches, and languages provide different levels of support for these different approaches. You can write object-oriented code in C! – juanpa.arrivillaga Nov 22 '17 at 18:00
  • @juanpa.arrivillaga Changed to `C#`; And I guess writing OO code in C is possible, but it would be quite hard, wouldn't it? The language isn't designed that way. – Sweeper Nov 22 '17 at 18:06
  • 1
    @Sweeper OOP is not about using `classes`. You can emulate a `class` in a `struct` just fine in C. – Elis Byberi Nov 22 '17 at 18:08