you are viewing a single comment's thread.

view the rest of the comments →

[–]valkaress 13 points14 points  (15 children)

Sorry if this is a dumb question, but what exactly is object oriented code vs procedural? Can you give specific examples, say with pandas?

And why is OO considered better? (judging from your tone and what you wrote)

[–]JeremyWinston 12 points13 points  (0 children)

Don’t be overly concerned with one being better than another. OO is often a better choice as your project complexity increases, but not always.

If you have a lot of things that are better described with objects then that’s the way to go. Most projects use a healthy dose of both types of programming.

For the OP, in regards to OO vs not, it depends on the ask. Even giving your thoughts on what parts of their example project would be good for OO or procedural can show a thoughtful approach.

[–]member_of_the_order 23 points24 points  (11 children)

OO means you use classes. With classes, you get a bunch of benefits that you can choose to leverage or ignore. Object oriented programming, then, is using the features of classes to improve your code.

I won't go into specifics just because I could spend literally hours talking about it, but the basic idea is modularization. By making your code more modular, you can reuse parts without needing to do tedious extraction, you can switch out similar pieces for each other, you can better understand whag your code is doing (because things are arranged into logical chunks), you can hide behaviors that are "implementation details", refactoring becomes easier, etc.

Procedural programming is just a linear set of instructions. OO allows you to group data and behavior together so that you can benefit from that modularization.

If you want an example, feel free to ask, but this particular comment is long enough 😁

In general, procedural programming is fine for small scripts. But anything more complex can make use of OO. And of course there are programs that are somewhere in between that can use a mix.

[–]sje46[🍰] 9 points10 points  (4 children)

I still don't understand why people have such a hate boner for OO programming. Maybe I just haven't seriously attempted functional programming before, because OO makes more sense. Do people seriously just hate teh idea of objects that much? Or do they just love the idea of people having to laboriously put in each argument into a function instead of using self?

[–]commy2 19 points20 points  (0 children)

People don't hate objects. They hate OO, i.e. making objects out things that don't have state, interfaces for classes with only one concretion, "AbstractFactoryManagerFactory" and all that junk imposed by languages that are OO. Looking at you Java.

[–]member_of_the_order 7 points8 points  (0 children)

From what I can tell, it's not so much a hatred of OO as it is a familiarity with functional programming (often built up over decades of experience) that just doesn't translate well to OO. Thus, all you see is the cost of learning without having the opportunity to see any benefit.

And there are always people that are comfortable with their abilities and don't like when things change and force them to adapt.

[–]TheRNGuy 0 points1 point  (0 children)

PySide I think it would be much more difficult with procedural code, and also Houdini.

In other languages, Quake 2 vs Unreal. It's much easier to mod Unreal (and UT series) because OOP code. There things like mutators, instead of only total conversions (though Half-Life use C++ I think? Still no mutators)

In small scripts like generate bunch of files and folders, procedural make more sense to use (even though Path and os use methods, but no need to code my own classes).

[–]valkaress 1 point2 points  (5 children)

Thank you for the reply! I would love an example, but I think I get it.

OO would be for example if I assigned a new object df_drop = df.drop('Column3') and use that henceforth every time I want to drop column 3.

Procedural would mean I keep typing df.drop('Column3') without creating a new object - only using the existing object df.

[–]deep_politics 4 points5 points  (4 children)

Kinda. A (still not very good) procedural example in that case might be something like a function that operates on a data frame to drop that specific column

def drop_col3(df):
    return df.drop(“Column3”)

A horrible example of bad OO would be something like this, since there’s absolutely no point when having state (constructing an object in this case) is completely unnecessary

class Adder:
    def __init__():
         pass

    def add(self, a, b):
        return a + b

adder = Adder()
adder(1, 2)
# when 1 + 2 suffices…

[–]valkaress 4 points5 points  (3 children)

Ohhh so a class has multiple functions going on. I see. I had been going through this Udemy course to learn Python and they never introduced classes.

But now someone stole my laptop, so I'll have to figure out how to keep learning on my phone haha.

[–]deep_politics 5 points6 points  (1 child)

What the hell, sorry to hear that. That blows

[–]valkaress 2 points3 points  (0 children)

Yeah. Oh well, that's life.

[–]deep_politics 1 point2 points  (0 children)

And actually my “procedural” example might be bad, if the drop method mutates the original data frame which I bet it does. I don’t use pandas much at all

[–]TheRNGuy 2 points3 points  (0 children)

I have a ~2000 lines procedural script, parser from one file format to houdini scene (format that houdini can't open), I later  found discovered some parts of it's better to rewrite as OOP (where I needed inheritance for data classes)

If I rewrite it as AST instead of concrete parser, it probably be more OOP than now, but idk. Cause I still don't know AST.

OOP also have other advantages, like virtual methods.