-1

I am making a Mathematics web program which allows the user to compute and prove various quantities or statements, e.g. determinant of a matrix, intersection of sets, determine whether a given map is a homomorphism. I decided to write the code using the OOP paradigm (in PHP, to handle some of the super heavy computations that a user's browser might not appreciate), since I could easily declare sets as Set objects, matrices as Matrix objects, etc. and keep some of the messy details of determining things such as cardinality, determinants, etc. in the background. However, after getting knee-deep in code, I'm wondering if deciding on OOP was a mistake. Here's why.

I'll use my Matrix class as a simple example. Matrix has the following attributes:

  • name (type String) (stores name of this matrix)
  • size (type array) (stores # rows and # columns of this matrix)
  • entries (type array) (stores this matrix's entries)
  • is_invertible (type Boolean) (stores whether this matrix can be inverted)
  • determinant (type Int) (stores the determinant of this matrix)
  • transpose (type array) (stores the transpose of this matrix)

Creating a new matrix called A would be done like so:

$A = new Matrix("A");

Now, in a general math problem concerning matrices, it could be that we know the matrix's name, size, entries, whether it's invertible, its determinant, or its transpose, or any combination of the above. This means that all of these properties need to be accessible, and certainly any of these properties can be changed by the user, depending on what's given in the problem. (I can give examples of problems for any of these cases, if needed.)

The issue I'm having, then, is that this would break the encapsulation "rule" of OOP ("rule" in quotes since, from what I understand, it's not a hard-and-fast rule, just one that should be upheld to the greatest extent possible). I did some searching on when getters and setters should be used, or even IF they should be used (seems odd to me that they wouldn't, in an OOP setting...), but this did not seem to help me much, as I found many contradictory answers and case-specific opinions.

So, my overall questions are: when the user needs access to modify many (if not all) of an object's attributes, but a class-oriented design seems to be ideal for addressing the programming problem,

  1. Is OOP the best way to structure the code, despite essentially ignoring encapsulation altogether?
  2. Is there an alternative to OOP which allows high user access while maintaining the OO "flavor" (i.e. keeping sets, matrices, etc. as objects)
  3. Is it ok to break the encapsulation rule altogether once in a while, if the problem calls for it? Or is that not in the spirit of OOP?
  • Nothing says that you *must* encapsulate. What does that even mean? It means that you can compartmentalise internal implementation details from other code which should have nothing to do with those details. If in your case there simply aren't any "internal details", that does not mean objects are completely worthless. They give structure to your data for starters. You probably also can add some useful methods to those objects that work with the instance data, no? – deceze Jun 08 '17 at 08:00
  • Thanks for the reply. Yes, there are a lot of internal details, and several methods in each class. My main concern is that nothing except for some of the brute calculations is private; there is essentially no data-hiding, which concerns me, as this is one of the features of OOP. – d4rk_1nf1n1ty Jun 08 '17 at 08:07
  • Well, you *can* hide data, you don't *have to.* If the properties are meant to be publicly readable and settable, then there's nothing wrong with having them `public`. Consider a setter to do data validation when setting new values, if that's of any benefit to your usecase… – deceze Jun 08 '17 at 08:09
  • I guess I'm confused about the fact that, in every article and book I've read about OOP, data-hiding was part of the "essentials" in OOP. I have getters and setters for every attribute, and these--and every other method--is publicly accessible. I'm primarily used to procedural programming, so I am somewhat novice in OOP. – d4rk_1nf1n1ty Jun 08 '17 at 08:17
  • You'll have to interact with your object *somehow*, it ought to have *some* `public` interface to get data in and out of it. No object is entirely `private`, that would make it useless. – deceze Jun 08 '17 at 08:22
  • Yes, I understand that; I'm not saying to make EVERYthing private. It just bothers me that my case is nearly the opposite extreme, contrary to exhortations from the various resources I've come across. – d4rk_1nf1n1ty Jun 08 '17 at 08:24
  • Also, any feedback on WHY my question is worth downvoting is appreciated. It's better than simply downvoting. I spent a lot of time making my question clear and direct, so I'm not sure why this response. – d4rk_1nf1n1ty Jun 08 '17 at 08:25

1 Answers1

1
  1. What you are trying to do is not necessarily outside the scope of OOP. The thing is that you have a different model than what would usually be described in programming textbooks (where, for example, the values of the matrix would be always present and all of the functions could be simple methods). (Perhaps this is why the question was unfairly downvoted.) Nothing prevents you from storing values like "is_invertible" internally and implementing setter and getter methods. Doing this might make sense if you are trying to learn OOP. But I think other problems (see coding textbooks) might be easier for learning purposes. I see that a remote goal would be to capture some of mathematics as an OOP framework. But the whole mathematical universe is immensely richer than any fixed architecture (results like Gödel's theorem put a theoretical limit). You can only succeed in developing a framework for a very narrow application, for example solving certain equations. That's what symbolic algebra programs do: you can look at how, for example, SymPy or perhaps parts of Maple and Mathematica are implemented. In my view, the OOP paradigm can be both very useful and too restrictive / unnecessary depending on the task (you can certainly find more about shorcomings of OOP in Wikipedia or elsewhere). Also, your problem can be seen as writing a small programming language - in many of them you have sets, numbers, etc as objects.

  2. You can use only rudimentary OOP or no OOP at all. You can use functional programming.

  3. You should Google/read more about this on this or other sites. Is it OK to sometimes walk across the road when the red traffic light is on?

Valentas
  • 2,014
  • 20
  • 24
  • Thanks for your answer. I have seen how the problem could be viewed as writing a small programming language. I've been toying with the idea of doing so for some time, but after researching a bit, it seemed too daunting a task for me right now, as I don't have much of the theoretical understanding of computer science. (I've done a lot of self-teaching with programming, but not much beyond that.) I've also looked into a few of the CAS's available, but figured I'd get more benefit by trying to play with making one myself eventually. Would you recommend a better route than I'm currently on? – d4rk_1nf1n1ty Jun 08 '17 at 08:52