Live data from Hacker News

The use of `class` for things that should be simple free functions

quuxplusone.github.io

21–30 of 406 posts

Re: The use of `class` for things that should be simple free functions

#21
post #17

Single-function classes make sense if you want the computation to happen lazily on-demand (it may be resource-intensive in CPU, GPU, storage, I/O, etc). The class acts as the place to hold the parameters needed for it and the result if/when it is computed. There's nothing wrong with that. Down the line you may often need the capability to discard the computation to save storage (and maybe re-do it at a later time), a…

> The class acts as the place to hold the parameters needed for it and the result if/when it is computed. There's nothing wrong with that.

Right, and the constness problem can be overcome by making some fields mutable. This is exactly what "mutable" is for.

If the requirement is to have a lazy, memoized computation, then a class is good. If the requirement is to have an eager, non-memoized computation, a function is good. The article keeps shifting the goalposts and beating strawmen that implement different specifications. It's not very good at making the point it thinks it's making.

Re: The use of `class` for things that should be simple free functions

#22
post #3

Also known in python as "if your class has only two methods, one of which is init, it's a function" in the "stop writing classes" https://www.youtube.com/watch?v=o9pEzgHorH0 EDIT: typo, changed link

Or it's a named stateful or immutable data container with validation? You don't always want to use primitive types. But I am not a Python dev.

Or anything that needs validation before you try to run the main part. e.g. the init() method may give you a shopping list of data you're going to need to pass to run() but which couldn't have been deduced until init() validated its parameters.

Sure, you could handle this the functional way by passing a getData() function to run() so it can do so itself but I'm not sure that's any more readable.

Re: The use of `class` for things that should be simple free functions

#23

Earlier quoted context omitted.

> struct MyAlgorithm This is a particularly bad name for your situation. An algorithm is not an object. Why not "my_state"?

> Why not "my_state"? because it's a HN post and not real code ? And it contains the code of the algorithm, in the operator()... function. So my_state would only tell half the story. When I use it, what do you think makes more sense : GameExecutionAlgorithm game_algo; next_positions = game_algo(...); or GameExecutionState game_state; next_positions = game_state(...); > An algorithm is not an object. That is however c…

>When I use it, what do you think makes more sense : > (...)

None of your examples make any sense to me. Why not name it simply "game"?

Re: The use of `class` for things that should be simple free functions

#24
Patterns and antipatterns are important, but naming is even more important. Calling "using a class when a function would suffice" "the OO antipattern" isn't going to help anybody.

I wish we wouldn't let today's tendency towards clickbait titles extend into engineering terminology.

Re: The use of `class` for things that should be simple free functions

#25

Counter-point: while in many situations this isn't the right approach, it's worth recognizing when this is the right approach, because they can look awfully similar. An example of this is graph searching, e.g. BFS or Dijkstra's algorithm. The typical implementation is a function. But if you make Dijkstra a class, with (say) a function to iterate through nodes, it lets you do several things that would be difficult wit…

I believe you describing at least one of two cases: 1. There are multiple methods on the object (not counting constructor) 2. There are arguments passed to the methods.

Either of these cases are different from the provided example: The example will only ever compute a single value per object, always returning that value from a single method with no arguments (other than self).

Re: The use of `class` for things that should be simple free functions

#26

Earlier quoted context omitted.

> Why not "my_state"? because it's a HN post and not real code ? And it contains the code of the algorithm, in the operator()... function. So my_state would only tell half the story. When I use it, what do you think makes more sense : GameExecutionAlgorithm game_algo; next_positions = game_algo(...); or GameExecutionState game_state; next_positions = game_state(...); > An algorithm is not an object. That is however c…

>When I use it, what do you think makes more sense : > (...) None of your examples make any sense to me. Why not name it simply "game"?

What about something like "in_order_traversal", which is an algorithm and you need to keep track of the traversed stack. You could either write it with functions in which case you would need to pass the stack as a parameter and have a constructor/initial state version of the function that passes the stack to the main function, or have a functor/function class that keeps the stack internally.

Re: The use of `class` for things that should be simple free functions

#27
post #3

Also known in python as "if your class has only two methods, one of which is init, it's a function" in the "stop writing classes" https://www.youtube.com/watch?v=o9pEzgHorH0 EDIT: typo, changed link

Or it's a named stateful or immutable data container with validation? You don't always want to use primitive types. But I am not a Python dev.

Then build a function from validated parameters (or partially apply some parameters).

Re: The use of `class` for things that should be simple free functions

#28

Patterns and antipatterns are important, but naming is even more important. Calling "using a class when a function would suffice" "the OO antipattern" isn't going to help anybody. I wish we wouldn't let today's tendency towards clickbait titles extend into engineering terminology.

I agree - it's baity and misleading, and so should be changed (https://news.ycombinator.com/newsguidelines.html). I've replaced it with the article's phrase for what it's really about.

Re: The use of `class` for things that should be simple free functions

#29

I'm fairly new to Unity programming in C# and I was surprised a few months ago that everything in C# must be in a class. As far as I can tell there is no such thing as just a function. Now I have I have a class with some static functions in it.

C# is slowly moving away from "everything is a class". Static classes with static imports is basically a module or namespace. C# also allows stand-alone functions inside methods. But you still have to declare a static class to hold "top-level" functions, which is somewhat ugly.

Given the current development of C# I expect we see stand-alone functions in the future.

Re: The use of `class` for things that should be simple free functions

#30

Counter-point: while in many situations this isn't the right approach, it's worth recognizing when this is the right approach, because they can look awfully similar. An example of this is graph searching, e.g. BFS or Dijkstra's algorithm. The typical implementation is a function. But if you make Dijkstra a class, with (say) a function to iterate through nodes, it lets you do several things that would be difficult wit…

I believe you describing at least one of two cases: 1. There are multiple methods on the object (not counting constructor) 2. There are arguments passed to the methods. Either of these cases are different from the provided example: The example will only ever compute a single value per object, always returning that value from a single method with no arguments (other than self).

Not necessarily. What I said can apply to even a class with with 1 method that takes no arguments... though you might often recognize it as a more sophisticated kind of iterator:

  class BFS(object):
    def __init__(self, origin): self.origin = origin; ...
    def next(self): ...
Post reply on HN