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…
The use of `class` for things that should be simple free functions
41–50 of 406 posts
Re: The use of `class` for things that should be simple free functions
#42Also 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 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
#43Re: The use of `class` for things that should be simple free functions
#44Second, 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
#45Earlier 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.
Re: The use of `class` for things that should be simple free functions
#46I'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
#47Counter-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).
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
#48Counter-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…
Re: The use of `class` for things that should be simple free functions
#49Counter-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
#50A 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); ...…