I've been musing on style. I have a question for the more experienced programmers out there.

How do you decide when a class's method doesn't actually need to be a method, but instead could just be a regular function?

I'm not talking about functions that need to be shared between classes. I'm talking about intentionally moving a method outside of a class, even though it will only ever be used by that class.

For example, if I've got a parser-related function that is only needed by the parser, I will normally just put it in the parser class as a method. It keeps things neat, I can make it private and lock down the class. But I've never before stopped to think whether this is the correct thing to do every time.

After all, (my language of choice) has the concept of units. I could just have some functions just as functions, and only have methods for things that directly need to update the class's properties.

It would make things easier for testing, too. I wouldn't need to create an instance of a class just to test a specific function works as intended.

But I don't know if it's the "right" way to do things in OO, or for that matter.

0

If you have a fediverse account, you can quote this note from your own instance. Search https://oldbytes.space/users/thelastpsion/statuses/115912365573995643 on your instance and quote it. (Note that quoting is not supported in Mastodon.)