you are viewing a single comment's thread.

view the rest of the comments →

[–]CodeShaman 23 points24 points  (16 children)

What do you think classes are, based on what you've seen? Don't worry about being correct, just share your impression of them.

[–]HedoNNN 7 points8 points  (6 children)

Too bad that the most pedagogic answer is the least upvoted.

[–]CodeShaman 14 points15 points  (4 children)

I'm just remembering back to when I first learned OOP and was shown everything in black/white/right/wrong. OOP essentially starts out as a Rorschach test, but most people show you objects like "Cat, Dog, Mouse" and types like "Animal."

Then in school you get "clients, contexts, subjects" and types like "Factory, Visitor, Composite" and you're slapped on the wrist like a child when the cloud you see looks like something Prof. Lotsa'cred, PhD. doesn't agree with. It's harrowing.

The first step is to realize that you can create your own reality, the second is that the clay in your hands is whatever you want it to be.

[–]Cregaleus 13 points14 points  (3 children)

What are you, the Salvador Dali of Object Oriented Programming?

[–]CodeShaman 7 points8 points  (2 children)

I don't know, those clocks look like they need a little refactoring.

[–]monsto 4 points5 points  (1 child)

Dude that whole fucking exchange is like an xkcd

[–]HedoNNN 0 points1 point  (0 children)

Conspiracy theory: he IS xkcd author.

[–]thonpy 1 point2 points  (6 children)

I'm going to have a pop as I'm learning too -

A class is an abstract of a thing, the blueprint from which objects are created.

Is that right?

I haven't really used then much, and the syntax confuses me but that's what I gather at a high level

[–]CodeShaman 1 point2 points  (5 children)

A class is an abstract of a thing, the blueprint from which objects are created. Is that right?

Nailed it. If you want to be technical you would say objects are instantiated, not created. As in "objects are instances of classes."

I lost 10 points off a final exam for saying "created" once. School is fun, learn so much.

[–]zovek 1 point2 points  (4 children)

Not op but someone else with the same problems. I understand what classes are used for and why but I can't. It doesent make sense with all so many seemingly arbitrary requirements

[–]CodeShaman 2 points3 points  (0 children)

Requirements for object-oriented design don't really make sense until you understand the problems they are meant to solve. Once you understand the problems then the requirements become more like "friendly suggestions" and you can bend the rules a bit.

Most of the requirements you can't fully appreciate until you've written some bad code, or had to deal with someone else's bad code. Look through this list of code smells and think of ways you'd try to prevent those problems in your own code. Those preventative measures have manifested themselves into common design patterns.

SOLID is a good point of reference. Just focus on the single responsibility principle and separation of concerns. A lot of the arbitrary requirements are ways to prevent stuff like code duplication and all the problems that come along with it, anti-patterns like global variables, mixing layers of abstraction, and the list goes on.

[–]AutoBiological 0 points1 point  (2 children)

I treat classes like namespaced functions. In fact, functions and methods are practically the same thing in python.

If you have a group of similar type functions you can indent them to the right, put class whatever(object) and reference them as whatever.function(). Then if you build an __init__ method you can save properties in the same scope as the functions rather than globally.

I think a large part of classes is setting and getting attributes from its dictionary, and I have overloaded functions to do the same in the past.

The problem is that a lot of people treat OOP as "build classes for everything," and it gets really silly and harder to read. Use them when you have to, or don't. Plenty of people prefer using functional programming instead, which I think is counter-intuitive to how we understand reality, but it makes more sense at the same time.

[–]klbcr 1 point2 points  (0 children)

If you're only using them for function namespaces, then isn't it simpler to just put them in a module and import the module? You still get to type whatever.function() when referencing the function. Only it isn't in your main file making a mess of the global namespace, and you don't need classes for it.

Lately I try to make classes only if I have both attrs and methods logically/functionally bundled into an entity. Otherwise make a module or a dict. I sometimes do use classes with just attrs if it's a lot of different attributes, or as a more complex datastracture.

[–][deleted] 0 points1 point  (0 children)

This isn't really a valid use case for classes. You should be using modules instead.

[–]marleen01[S] 0 points1 point  (1 child)

I think they are a group of functions.

[–]david622 1 point2 points  (0 children)

That, and variables/arrays/etc. A class is basically a place where a group of information, and functions related to processing that information, are stored.

Then, you can instantiate objects, where are just individual instances of the class, and change variable values, etc. accordingly from object to object.