1

I wanted to move my program into a class or classes because most of the form post I read say it makes the program easier to read, understand the flow of the code, and improve maintainability.

Realizing at some point I would be adding more and more to my code until its over 1000 lines I decided to move my GUI into a class.

(keep in mind I have a fully functioning program using just functions)

I got as far as getting the basic frame layout to work but I must be doing things all wrong. I cant get everything to work well together. I have been able to get classes to work just find for small sections of my code Like creating buttons or labels. But when I try to add in all the GUI elements it keeps breaking.

I have spent many hours reading up on classes and watch tutorials about classes specifically for python. For some reason I am having a hard time apply what I know about classes to anything more complicated than buttons and labels.

For me so far everything makes sense using functions. So I imaging it should make sense using methods in a class. However it has not been this straight forward.

This all leads to my questions for this post. Keep in mind I am using python. I am not embedding any other language to my knowledge and I know python is a little more flexible thane say Java would be about using classes.

  1. Should I continue to use functions for the main body of my code and use classes for small repetitive task like making buttons while studding classes until I reach that Aha moment with classes? Or Should I stop working on my program and really dig into classes?

  2. Will it have an impact in the long run if I wait to really understand classes or get it done before I move on with anything else or a mix of both?

  3. Would you write all of your program using functions/variables or try to do everything in a class?

2

3 Answers 3

4

Object oriented programming is not just about transforming plain functions into methods within classes. While this approach would give you an additional level to group common functionality, it is really unnecessary in Python, and not essential in most languages.

Object oriented programming is a paradigm, with its own principles, benefits and drawbacks. If you use it as “functions grouped by classes,” you're not using its benefits, you're not following its principles, and, more importantly, you're not using the paradigm itself. You're trying to use a hammer as if it was a saw—I'm not surprised it doesn't feel very practical.

The convenience of object oriented programming is in the ways objects related to each other, would it be through composition and inheritance, and how do they communicate.

Should I continue to use functions for the main body of my code and use classes for small repetitive task like making buttons while studding classes until I reach that Aha moment with classes? Or Should I stop working on my program and really dig into classes?

You should definitively learn about object oriented programming. Forget about classes. Classes are just one aspect. Find a good book about the paradigm itself, a book which is language agnostic.

Should you rework your application? I would say no, specifically in a context of Python. With Python and unlike in languages such as Java, you have a freedom to use classes where you need them. With enough knowledge of OOP, you'll probably find that a good part of your app is good as it is. Remember, Python encourages simplicity.

Will it have an impact in the long run if I wait to really understand classes or get it done before I move on with anything else or a mix of both?

You don't need to wait. You have a program which works. That's great. Make sure you add new features to it while learning OOP.

Would you write all of your program using functions/variables or try to do everything in a class?

Neither.

In Python, both approaches can be mixed. This means that you use OOP exactly when needed, and keep it simple when you don't need OOP. This applies to any project, independently of its scale.

1
  • Thank you for you response. This actually helped me understand why the way I am using classes is not giving me the response I desire. I will continue as I have been and learn OOP as I go. At least I know now there should not be any major problems down the road when I do finally bring some OOP into the mix. Commented May 26, 2017 at 17:49
2

If classes don't make sense yet, there might not be a point to forcing yourself to use them. Re-architecting your code to be more object-oriented might make it easier to read and maintain, but it might not (and might not even be necessary). It's possible to write very clean, well-written, easy-to-read-and-maintain code that is not object-oriented, and it's also possible to write horrible spaghetti code that is object-oriented.

My advice: write code that you feel comfortable with that makes sense to you. Keep it well documented and tidy. Object-oriented methodologies don't make sense for every possible computing task, and without knowing more about your project, I don't want to say you must do it. At the same time, learning new ways of doing things is never a bad thing, so maybe keep up with trying to understand object-oriented design/programming until you get your "Eureka!" moment.

2
  • That makes sense. Its good to know I don't need to force the OOP in my code. One of the main reasons I was concerned about classes is from an answer I got on Code Review. I was worried I was digging a hold for myself by not writing stuff into classes whenever possible. Commented May 26, 2017 at 16:26
  • @SierraMountainTech: You did ask on CodeReview which is more likely to get a strict critique (but that's not necessarily a bad thing) - you can still learn lessons from their answer, even if you don't feel you need to implement everything they suggested in your current project. Commented May 26, 2017 at 17:19
1

Classes are only helpful if they represent something in your problem domain. First think data items and behavior, then model that something into a class. That way you create building blocks and you can assemble them to build your application.

If you already have an application that was not designed in an object oriented way, there is no point in reorganizing it just to make it use classes. You would end up with classes that are just collections of functions and variables, the classes would not represent anything.

Read up on OO some more and start your next application by recognizing classes in your problem and then shape those into code. Do it OO from the ground up and it will start to make sense and be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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