Live data from Hacker News

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

quuxplusone.github.io

41–50 of 406 posts

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

#41

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…

[deleted]

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

#42
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

I have a few little classes that are clients for network services. They have a constructor which sets up an HTTP client or socket or something, and maybe prepares some metadata, and then a method to make a call to the service.

I could write these clients as lambdas or nested functions which close over the stuff which init creates. But why? An object makes it much clearer that there is state.

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

#43
post #27

Earlier quoted context omitted.

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).

[deleted]

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

#44
Well, there is one subtle, possible advantage and that is literally just the logical grouping of code, not really encapsulation in the technical sense.

Second, if you have a handful of functions like this, even remotely related ... they make a nice 'Library' and frankly there is nothing wrong with that.

As I see it, I don't think it's an anti-pattern depending on how it's used.

In some other instances, first-class functions or lambdas may be just as appropriate.

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

#45

Earlier quoted context omitted.

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.

Python doesn't have private members, so that doesn't help you as much.

Python has private members through double underscore methods

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

#46
This is why I like languages where you just can't have a free function!

I'm arguing the opposite, free functions are an annoying and confusing anti-pattern when dealing with OO languages that allow them.

C++ is often just a maze of mostly write only code.

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

#47

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).

Right. I think the article is talking about one very specific case, which is apparently common amongst novice C++ programmers, where there is a single computation, whose only inputs come from the constructor, and which gets done in a method.

There are plenty of cases where it makes sense to wrap a function in an object, but the point of the article is that this isn't one of them!

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

#48

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…

None of these situations require an object to do. You can always have a function that takes in an extra ‘state’ argument. You can then do everything you said by passing in the corresponding state values.

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

#49

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…

None of these situations require an object to do. You can always have a function that takes in an extra ‘state’ argument. You can then do everything you said by passing in the corresponding state values.

Precisely. And that's the way it's done in functional languages.

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

#50

A lot of time where I just write simple functions, I end up having to wrap them into objects because it is much more convenient when I want to do static polymorphism - sure, that function I'm writing now doesn't need state but then two days after I have to refactor because the next strategy I use does indeed require state. e.g. : template struct MyAlgorithm { F1 f1; F2 f2; void operator()(...) { ... f1(whatever); ...…

This is a limitation of C++, not functions.
Post reply on HN