3 ms·
Totally, "isolating private implementation details" sounds super clear and useful. /s The general answer seems to be that OOP starts really helping when many p
by phpthrowaway99 4y ago
Totally, "isolating private implementation details" sounds super clear and useful. /s
The general answer seems to be that OOP starts really helping when many people/teams are involved.
Being self taught and a one person team I suppose hasn't forced me into it yet.
- Kim_Bruning 4y agoAt some point in time "the other people" are going to end up being different versions of Future You :-P . So if you take the "other people" argument, then it's definitely worth learning just for that reason. OO is pretty handy for the You Of Today too though. The basic low down is that this is all about encapsulation (tidying things away in boxes). You hopefully already know a bit about that. Say you just have all globals through all your files, you probably know you end up with a big mess of spaghetti. To help with that, you've probably already learned about tidying away code and variables into functions. And you might know that if you have a local variable, it is only visible inside the scope of your function. If you're doing it right, then nothing outside your function will ever directly touch the local variables inside the function, or vice versa. This makes it really really easy to reason about what happens inside the function, since -with the exception of your parameters and your return- what happens in vegas... uhh... your function, stays in your function. And the inputs and outputs to the function are well defined. This saves hours and hours of hunting out every last spot that some global was used, just in case it is interfering with something 1000 lines away. If you've done a lot of programming, you've probably been there a hundred times, cursing the laziness of Past You as you went. Maybe you also separate your program out into different files. Model.php, View.php, Controller.php DoImportantThing.php ... maybe something like that? So you probably already realize that tidying things away in boxes is a good idea. Everything you can tidy or organize away inside some box is something you don't need to worry about elsewhere at least. Out of sight is out of mind. Wouldn't it be great if there were more ways to help you tidy things away into boxes? Well, we're in luck. PHP has classes and objects. These are a further way to organize functions and data together into a next-size box. The idea is to tightly control what gets in and what gets out of these boxes again. And if you do, that's an additional level of things you don't need to worry about constantly. This frees up that part of your mind for other things. Of course if you're a PHP programmer you don't NEED to use classes and objects. You don't NEED to use anything. But they're a very powerful tool to help you keep your code squared away. And since they're built right in to the language already, right at hand, why wouldn't you use them? Classes and objects make most non-trivial projects easier and quicker to write (especially if you can reuse old classes, which you often can!) , and they make medium to large projects rather more manageable and tractable. If you poke me, I could find some time to show you how you can make classes and objects work for you? [disclaimer: Classes and Objects are definitely not the first/last/only/best/worst tools to organize code. Different languages have different tools sometimes. PHP just happens to provide Classes and Objects, so that's what you've got to work with there.]
- phpthrowaway99 4y agoI really appreciate your offer regarding PM, and the time you took to write that post. As you figured, I have learned those easier parts of not making things global, writing functions, separating files, all that. The way you explained it does make it clear that OOP could be the next step in helping me keep my sanity and my code clean. Since writing this post I looked up a few explainer videos regarding OOP and somewhat get it now. I somehow get the feeling that if you do a good job with functions, it's almost working the same way. But I'm sure there's still parts I'm missing. I appreciate your offer but I don't think it's fair to use your time when I haven't personally tried to use the countless resources explaining OOP that are available online. I'll look into them so I know what the possibilities are when I work on my next project. Thank you for the response.
- bluefirebrand 4y ago> I somehow get the feeling that if you do a good job with functions, it's almost working the same way. Yes. A lot of what people profess about the "clean" ways to write OOP is essentially just the same as using Functions with Namespaces
- civilized 4y agoWhich you can do with modules in Python, so the particular utility of classes gets increasingly hard to tease out. One neat use of classes is model objects in scikit-learn. But R manages to do the same thing with a much more basic system called S3. In this system, you have "generic" functions which dispatch to different methods based on the data type of the first argument. This is used to endow the programming environment with predict and plot functions that behave differently depending on what kind of model you feed into them. This is essentially the same as how two different OOP classes can have two different methods with the same name. But S3 has none of the other trappings of object-oriented programming whatsoever: no encapsulation, no inheritance, no polymorphism. I found it a fascinating revelation. It's hard to point to anything that R's object system makes unnecessarily difficult, any killer app that proves how pure OOP is better. I would guess that traditional OOP excels in GUI systems, but outside that context, people seem to increasingly find it wanting as a paradigm. We are seeing an evolution away from unipolar OOP ideology and towards a more multipolar exploration of different paradigms and their comparative advantages.
- Dalewyn 4y agoObject oriented programming is quite simple in theory. Say you want to draw a window, it's one of many windows in your program. You could either write out the code to draw a window each and every time you want to draw a window, but that's probably inefficient at best and a nightmare to maintain at worst. So you instead write a generalized class to draw a window. You then invoke that class as a new object when you want to draw a window, and pass to it any information unique to this particular window (eg: dimensions, contents). This means the code to draw a window only exists once in your program. If you need to change how windows are drawn, you only need to change the one class instead of thousands of instances of drawing windows. In essence, any action that you are likely to repeat many times in a program benefits from object oriented programming because it centralizes the code in one place and is abstracted away when the time comes to use it. By contrast, if a given action is something a program would only do once or at most very seldomly, object oriented programming starts to lose importance because there's little value in centralizing and abstracting away a piece of code that will only ever execute once anyway.
- civilized 4y ago> In essence, any action that you are likely to repeat many times in a program benefits from object oriented programming because it centralizes the code in one place and is abstracted away when the time comes to use it. This isn't really the unique benefit of OOP. Plain functions also centralize code in one place and allow you to avoid writing repetitive code.
- bluefirebrand 4y ago> So you instead write a generalized class to draw a window Using Classes when you should be using functions is one of my biggest peeves with OOP and the programmers who write it. Especially those who wind up writing a "Window" Class with one function "draw" and nothing else
- kaba0 4y agoTo make it more OOPy you would probably give a few instance variables to the window like width, height, title, etc. That way, a simple drawWindow function would not be a better replacement anymore.
- civilized 4y agoThe idea that OOP is the natural next step in making a system more "scalable" and "better engineered" is... controversial. http://harmful.cat-v.org/software/OO_programming/ http://harmful.cat-v.org/software/OO_programming/ "Sometimes, the elegant implementation is just a function. Not a method. Not a class. Not a framework. Just a function." – John Carmack Unfortunately, many college-educated programmers have been taught OOP design ideology and profess it uncritically.
- kaba0 4y agoMany content on that website is straight up ridiculous claiming shit like gcc, any format, everything more complex than a rock basically is harmful, so I’m not sure it helps your case. But I can agree with the Carmack quote, but that in itself doesn’t contradict OOP’s supposed benefits.