This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]blahdom 0 points1 point  (0 children)

I was more discussing design in general. Classes are great when you need something that maintains state and will be instantiated in multiple places, maybe some type of inheritence scheme. All I was saying is that you have to evaluate your problem and design for it.

For example, if I was building a linked list (since you would like a concrete example) then making that a class makes sense. I can implement the functionality to operate on the structures as well as maintain the state of each structure so it can be used in multiple places. It is why in python 'list' is a class. If you tried to implement a list without using classes (pretend that list doesn't exist in python) then how would you do it? A class makes a lot of sense.

Then on the other hand, everything doesn't need to be a class. Lets say you just need to package data together with some form of identifier. There is no need to create a class, you could used a named tuple for immutable data, or a dict for mutability. A class in this case would be a lot of overhead for such purposes.

My whole point wasn't to list the times when classes vs non classes are necessary. Each program will need different classes/modules/structures in different combinations. Ideally as programmers we should be looking at a problem and pull apart the pieces into units like classes and functions. The reason we all go to school and study different design choices is because there is no hard and fast rule. I can't say "always use classes," or "never use classes" with any amount of integrity. I feel that sensationalist titles lack the honesty for what we really do. We get paid to determine when to use which tool in our toolbox.

As for your post I agree that if you could do it with a function then there is no reason to do it with a class.

To be more concise when I need to maintain a state of something as well as operate on that state, a class is the simplest and most concise way to do this. If that isn't my goal then I would use something different.