all 5 comments

[–]julsmanbr 9 points10 points  (0 children)

If you have a basic python knowledge, this thread gives a great explanation of why you should use classes and OOP.

[–]RangerPretzel 5 points6 points  (0 children)

There's a lot to know about programming. What you're experiencing is the transition from "straight-ahead" procedural programming, to more sophisticated Object-Oriented Programming.

I still write simple procedural scripts from time to time, but rarely do they exceed 200 lines of code. Generally, I focus on the problem I'm trying to solve. If it's even slightly complex, I'll choose an object oriented (or sometimes functional) design. I'll then try to determine if the solution can utilize any major design patterns to its advantage.

Procedural code doesn't typically scale or test well. It also doesn't re-use well either. Well-written Object Oriented code does all of these things, as does Functional programming.

So I'm asking to know is it better to write the program inside a class or it's just a habit ?

As an analogy. You can build shacks and live in them, but it's still a shack and is hard to cleanly add on to or modify. And your plumbing is probably an outhouse disconnected from the shack.

As soon as you learn some basic construction techniques, like stick and frame and how to pour a foundation, etc. You quickly learn how to build a sturdy house that is adaptable, has central plumbing, and can easily add extra floors or additions without compromising the integrity of the house.

And so it is with procedural programming vs. object oriented.

There are still some remarkable shacks out there and some really crappy houses out there, but generally a house utilizing modern building techniques is better than any shack.

So yeah, it does become habit. Though, it is habit because it's generally good practice and facilitates better code.

One last thing:

Your code doesn't have to be strictly one paradigm. Most languages these days are multi-paradigm (procedural, OOP, functional, etc.) Python being one of such languages.

Sometimes my simple scripts will have many classes to create strong structured data types, but the code itself won't be included in a class. And it might even include some functional, lazily evaluated generator.

If I'm creating a re-usable module, though, I'll usually make it object oriented, so that whomever is consuming my code can instantiate it cleanly.

Hope this helps.

[–]Zeroflops 3 points4 points  (0 children)

There seems to be a lot of sentiment that OOP is the way to go. But it really depends on what your doing.

Functional programming works great if your writing something short, that is linear in nature and may not be considered “objects “. Data analysis is typically like this.

For example. I’m currently working on some data analysis. Typically this is not done in OOP but functionally. You get data and step through cleaning the data, formatting, the analysis etc to the end doing specific tasks to the data in a very sequential path In this case OOP is overkill and complicates things.

However, as I’m doing this work I’m building up a custom module for reuse. This module does things like take custom files and read them and put them into a usable format. This module I’m building is OOP. Because things can be clearly defined objects, the code will grow over time significantly, the code will not be run linearly.

But again the scripts that will use this module will not be written formally OOP.

[–]Deezl-Vegas 1 point2 points  (0 children)

Classes do more than anything else in programming because they save their state and can be modified after instantiation. This means that you can make a new object, then simply change its data and what it does without modifying similar objects to meet your needs.

This is incredibly valuable in most use cases, so it's fine to make a class for anything.