3
$\begingroup$

I have been asked many times that why you are not using classes, objects, and object oriented concepts while coding?

What I do?

Write python scripts to fetch data from different sources, apply pre-processing code, create necessary variables, and take these refined variables to modeling process.

How I do?

Write necessary functions to carry-out specific task, all the functions will be called one-by-one as top down, procedural approach. In case of different modules, I keep them into different python files, and then simply import them.

In the current process, I never found a motivation to use OOPs concepts.

Can you suggest how can I improve my coding practice, and what are the flaws I am putting into my current architecture which can be fulfilled by OOPs?

$\endgroup$
3
  • 2
    $\begingroup$ In the current process, I never found a motivation to use OOPs concepts. So don't be persuaded by others, probably less knowledgable in your domain, to use alien concepts. Personally I make zero use of OOP in most of my data processing. $\endgroup$ Jul 1 '20 at 14:41
  • 2
    $\begingroup$ There are exceptions, but in general an OOP style is more widely used for production code written by software engineers. A Functional style, etc is used more by Non-Software Engineers, such as Data Scientists, working on a POC or in a notebook. OOP will help to avoid re-inventing the wheel or writing the same code many times. Functions accomplish this to some degree, but Classes are much more flexible, through class inheritance, polymorphism, etc. $\endgroup$
    – Donald S
    Jul 2 '20 at 12:15
  • $\begingroup$ Are you doing ML regularly using sklearn? $\endgroup$ Jul 3 '20 at 8:28
2
$\begingroup$

There are exceptions, but in general an OOP style is more widely used for reliable production code written by software engineers. A Functional style, etc is used more by Non-Software Engineers, such as Data Scientists, working on a POC or in a notebook. OOP will help to avoid re-inventing the wheel or writing the same or similar code many times. Functions accomplish this to some degree, but Classes are much more flexible, through class inheritance, polymorphism, etc. Before you change anything, you should consider the pros and cons of each style and your current situation.

Some of the reasons to use OOP: You work on a large project with many people and your code needs to be reliable and portable

Some of the reasons to stick with Functions, etc: You work on small projects with one or two other, the code is POC and will not be used in production, and has a short lifecycle.

$\endgroup$
2
$\begingroup$

This is a really interesting question about the motivation behind Object orientated programming.

The main reason that makes OOP particularly attractive is the reusability of functions and objects which you can reuse in different analysis. For example, for one NLP model, you create a function which cleans text (returns lowercase text, free from stopwords). A few days later you want to generate another NLP model which preprocesses text in the same way as your previous NLP model. Here you can put the clean text method instead a Preprocessing class and reuse the clean text method to clean the text in the same way as before.

Another reason behind OOP is its legibility. I find it is noticeably easier to track where bugs arise in OOP code than functional programming code. This, in turn, makes it easier to test and debug separate components within a class.

This is my take on it, and I am sure everyone has their own way of programming that suits you and the teams you work in. I look forward to reading other people's views/answers to this query.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.