1

I have an intern and he writes code fast.

However, I have difficulty making him understand the importance of writing classes and follow the OOP paradigm.

We recently had a discussion that went like something this:

"Instead of having this long function that extracts data from two different queries and then combine the data into a new data structure as a standalone function, why not start by putting it in a class?

I understand that it's not much differences for now, but I can foresee that this class will grow to have more functions and the next guy who takes over will naturally refactor the giant function into more functions within the same class."

When he objected, I told him, "Okay, I gave you my criteria (write the function within a class) and my reason (we will likely have it as a class in the future, might as well start now no matter how imperfect the start). If you have a better criteria and a better reason, why don't you suggest it?"

One day later his reply was, "python is an object oriented programming language so when codes are organised inside a file, it is somewhat oop alr"

How do I make him understand the importance or better yet appreciate the importance of software craftsmanship?

In case, I made some bad assumptions myself, I am willing to stand corrected and I understand the dangers of asking this question and having it closed down. So if there was a better place to pose this question, I am willing to try it.

closed as too broad by gnat, David Arno, Robert Harvey, Greg Burghardt, Thomas Owens Jan 4 '17 at 21:30

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 7
    ""Instead of having this long function that extracts data from two different queries and then combine the data into a new data structure as a standalone function, why not start by putting it in a class?...". What benefit does putting it into a class bring? Much better to ask him to refactor that long function into a set of smaller, private, functions, rather than worry about OOP (which is arguably a failed paradigm anyway). – David Arno Jan 4 '17 at 14:19
  • 3
    "but I can foresee that this class will grow to have more functions" -> Stop right there. If its not a requirement in an actual backlog, then it doesn't exist. Don't violate YAGNI without serious consideration. – Graham Jan 4 '17 at 14:33
  • While I think this question is not a good fit for this site, I've had this exact question asked of me a million times from C# developers. This isn't a "Python" question so much as it is a question about the basics of object oriented programming. If it is logic that goes in different layers of an application (for instance data access) then it should be in its own class. YAGNI doesn't apply here. Putting data access in its own class is just good design up front. – Greg Burghardt Jan 4 '17 at 16:38
  • 1
    If you put a procedural function in a class, you barely have any resemblance to OOP. – JeffO Jan 4 '17 at 19:07
12

Python is a multi-paradigm language in which procedural code is quite common, and the decision to write a class or a function is taken on a case by case basis. Python recognizes that a class is not always the best tool for the job. If you hope to convince someone of the merits of OOP, you need to solidify the merits in your own mind first, and not just use a class because it's the default "best practice" or whatever.

People choose classes when they need to carry related state between function invocations. In other words, when you already have something resembling a self that gets passed between a group of functions, that's a good sign that a class would be a great fit there. If you have no state being passed, or a different set of state every time, then OOP is a poor fit.

  • "People choose classes when they need to carry related state between function invocations." +1000 for pointing out the actual beneficial distinction of OOP, which is sometimes preferable, and sometimes not. I wish someone would have explained OOP to me this way earlier on, rather than "its the best way to organize your code". – Graham Jan 4 '17 at 21:27
  • "carry related state between function invocations" => can give one example? In my situation, I was hoping to group utility functions in the same class as the job of these functions basically is to extract data in multiple queries and then join them as data structure before writing them into excel sheets – Kim Stacks Jan 9 '17 at 5:33
1

Classes are not a code organization methodology. Classes encapsulate state and have methods to modify that state. If that is not your use case, procedural functions are perfectly adequate.

The cure for functions that are too long is not a class, but refactoring the long function into several smaller functions.

  • 2
    »Classes are not a code organization methodology« What else? All paradigms are in the end ways to structure/write code. – Thomas Junk Jan 4 '17 at 16:23
  • 3
    Classes are most definitely a code organization methodology. – whatsisname Jan 4 '17 at 16:26
  • @whatisname, classes are not a way to group related functions that modify no state, except in languages like Java, where you have final classes with only static methods and no member variables to make up for the lack of plain functions. The question was about Python, where there are many other mechanisms to organize code than classes.... – Tony BenBrahim Jan 4 '17 at 16:39
0

The problem I see in general is, that different paradigms are sometimes treated like a cargo cult and it is good to remember what we are doing and why. The case with OOP is not, that it is in and from itself better, nor is it necessary to put everything in a class to follow software craftsmanship.

The main difference between OOP and procedural is, where your data lives and where the code which deals with the data lives (without going into detail about the pillars of OOP ktl.). But the point is: someone thought it a good (or maybe a better) idea to put data and its modifying code together. That is nothing which is carved in stone for generations to come. It is just a way of thinking and working. The hope was, that the way OOP structures code:

  • code will be better organized (data alongside modifying code)

  • less error prone

  • less repetitive etc.

It was developed for a reason.

That said, it should be clear, that to achieve the above goals OOP is one but perhaps not the only way.

And the good thing about Python in contrast to Java is, that there are on the one hand classes and methods like Java but also are functions first class citizens. That allows to decide how to structure your code, which suits the problem you are trying to solve best.

Finally: A nice talk about to Stop writing classes

-3

In reality the difference is very subtle: (a) automatic cleanup, (b) encapsulation, (c) simplicity of going from a single instance of a class to to class being a member of another class, lists, maps, etc.

All these things make real difference only on relatively large projects, so interns would not get it, people dealing with less than 1M LOCs would not get it either - they should simply believe ones who have relevant experience.

  • 3
    I'd prefer that software developers think for themselves and understand the issues, rather than just take the word of someone more experienced. – Robert Harvey Jan 4 '17 at 15:12
  • I prefer it too, but the vast majority of software engineers never faced the issues that really required oop. – zzz777 Jan 4 '17 at 17:01

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