Does OOP make sense for small scripts?

2019-03-22 15:28发布

I mostly write small scripts in python, about 50 - 250 lines of code. I usually don't use any objects, just straightforward procedural programming.

I know OOP basics and I have used object in other programming languages before, but for small scripts I don't see how objects would improve them. But maybe that is just my limited experience with OOP.

Am I missing something by not trying harder to use objects, or does OOP just not make a lot of sense for small scripts?

17条回答
我想做一个坏孩纸
2楼-- · 2019-03-22 15:37

OOP is about what you get if you add polymorphism on top of modular programming.
The latter of both promotes low coupling, encapsulation, separation of responsibility and some other concepts, that usually produce code, that is short, expressive, maintainable, flexible, extensible, reusable and robust.

This is not so much a question about size, but about length of the software's life cycle. If you write any code, as short as it may be, as long as it is complex enough that you don't want to rewrite it, when your requirements change, it is important that it meets the aforementioned criteria.

OOP makes modular programming easier in that it has established solutions for implementing the concepts promoted by modular programming, and that polymorphism allows really low coupling through dependency injection.

I personally find it simpler to use OOP to achieve modularity (and reusability in particular), but I guess, that is a matter of habit.

To put it in one sentence. OOP will not help you in solving a given problem better, than procedural programming, but instead yields a solution, that is easier to apply to other problems.

查看更多
相关推荐>>
3楼-- · 2019-03-22 15:40

Am I missing something by not trying harder to use objects, or does OOP just not make a lot of sense for small scripts?

Objects buy you encapsulation and reuse (through inheritance). Neither is likely to be terribly useful when writing small scripts. When you have written a collection of similar scripts or you find yourself repeatedly changing your scripts, then maybe you should consider where objects might help.

查看更多
4楼-- · 2019-03-22 15:40

Using OOP for few hundred lines of code rarely makes sense. But if your script is useful, it will probably grow rather quickly because new features will be added. If this is the case, it is better to start coding OOP way it will pay in the long run.

查看更多
不美不萌又怎样
5楼-- · 2019-03-22 15:41

OOP is just another paradigm. A lot of problems can be solved using both procedural or OOP.

I use OOP when I see clear need of inheritance in the code i am writing, its easier to manage common behaviour and common attributes.

It sometimes makes it easy to understand, and manage. Even if the code is small.

查看更多
戒情不戒烟
6楼-- · 2019-03-22 15:42

Another benefit of OOP is to communicate intent (whether to other developers, managers, or yourself some point in the future). If the script is small enough where it can be fully communicated in a couple of sentences then OOP is probably not necessary, in my opinion.

查看更多
Emotional °昔
7楼-- · 2019-03-22 15:44

My experience is that any purely procedural script longer than a few dozen lines becomes difficult to maintain. For one thing, if I'm setting or modifying a variable in one place and using it in another place, and those two places can't fit on a single screen, trouble will follow.

The answer, of course, is to tighten the scope and make the different parts of your application more encapsulated. OOP is one way to do that, and can be a useful way to model your environment. I like OOP, as I find I can mentally jump from thinking about how the inside of a particular object will work, to thinking about how the objects will work together, and I stay saner.

However, OOP is certainly not the only way to make your code better encapsulated; another approach would be a set of small, well-named functions with carefully defined inputs and outputs, and a master script that calls those functions as appropriate.

查看更多
登录 后发表回答