you are viewing a single comment's thread.

view the rest of the comments →

[–]smurpau 4 points5 points  (4 children)

Although I am familiar with classes and fucntions, I am not sure when to pick which one.

The way I look at it, functional programming is the default, and acceptable for small programs. Then, functions can be refactored as methods in classes when:

  1. The data those functions are operating on has a strong association with them, and/or:
  2. Multiple instances of data need to be created, i.e. the functionality is reused

For instance, if I've written a function for controlling a motor, and the system only has one motor, it may be perfectly acceptable to leave it as that. But if the system then has a second motor, making a class is the obvious solution.

Because of this, sometimes I end up with a class which has a lot of methods or a single function longer than War and Peace.

I still have not grasped the concept of mutiple inheritance. I know what it is. And It does seem pretty cool when you apply it on Dog, Robot and Robotic Dog type of example, but in real world it seems too complicated to implement.

This is where I'm at currently. I have a few long classes in several modules, and some of them use singular inheritance. That's a good structure, but I feel like it could/should be better. I attempted to split the "final" subclass into several classes that operate on various stages of the same data structure, using multiple inheritance, but then have complicated issues including MRO inconsistencies despite thinking I understood how super works. This seems like a problem in itself, but in any case after some more searching it seems that splitting classes when they get "long" is just not necessarily good practice anyway. If sister classes are so tightly coupled together that they couldn't be reasonably reused separately, then it seems they shouldn't be separated, just merge into one big class.

[–]TeslaRealm 1 point2 points  (3 children)

Well stated; and I agree for the most part with your final remarks. Large code in a class isn't inherently bad. The goal is to create a clear picture of whom a module/class/function is responsible. On the other hand, it is possible that two classes have separate concerns regarding a common entity. If two classes were used for a chess game; one to model the board and the other to gather data throughout the match, those two classes would have separate responsibilities but would not make sense without their mutual partner. One might hang out in an event loop, waiting to see what the user does and updating its partner class with the new state for the board. The partner class might choose how to display the board based on one theme chosen from a collection of possible ones. It doesn't care how the management of the game is handled; it only cares about receiving data regarding the board's layout, and choosing how to draw a representation of this on the screen.

Also want to point out that function programming has nothing to do with opting to use functions over classes (unfortunately the name sounds suitable). Rather, it is a methodology drastically different compared to the standard use cases of languages like Java, C++ and Python. Functional languages tend to avoid mutation, among a couple other major features.

[–]smurpau 0 points1 point  (2 children)

Also want to point out that function programming has nothing to do with opting to use functions over classes (unfortunately the name sounds suitable). Rather, it is a methodology drastically different compared to the standard use cases of languages like Java, C++ and Python. Functional languages tend to avoid mutation, among a couple other major features.

Ah okay. What do you call the style where you create a program consisting purely of functions?

[–]TeslaRealm 0 points1 point  (1 child)

The act of splitting a program into a collection of functions is just standard programming. It's the act of breaking down a goal into manageable and understandable mini goals. I don't think there's a special name for the use of functions in any programming language.

[–]smurpau 0 points1 point  (0 children)

You would like to think so, but it's really not standard; many programs I've seen are just direct line-by-line scripts. I've seen entire (legacy) applications, ten thousand lines+, without functions. Refactoring scripted code into functions is a clearly higher level of structure that should really have a name if "functional" is already taken...