Live data from Hacker News

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

quuxplusone.github.io

71–80 of 406 posts

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

#71
post #49

Earlier quoted context omitted.

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.

No, it's done inverted in functional languages!

In a FPL your graph will be an ADT. The advantage is clients can pick it apart, but there is no easy way to extend it. If you wanted to "use the same object for computing distances to multiple vertices", you're screwed: the ADT is laid bare, and there is no data hiding.

In an OO language, your graph will be an opaque object, and you can write things like CachingDijkstra or ParallelDijkstra and make these work transparently for your clients. The price is that you are bound to your APIs: a client cannot pick apart your data structure, because it's opaque.

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

#72

Is there any real harm though? Free functions pollute the global namespace, which is something I tend to avoid (at least in Ruby where everything shares the same namespace).

What's the difference between polluting the global namespace with a class name, or with a function name?

I get it, I also experience some reticience when adding free/bare functions in Swift. But maybe it's because of 15 years of OOP programming.

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

#73

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.

"everything in C# must be in a class"

As an aside, C# also has structure types which have value semantics - one example being DateTime:

https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

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

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

And if you have many methods, but only one 'public' method, use a closure.

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

#75

On the topic of eliminating simplistic classes: one of things an object is for is to enclose multiple pieces of data and provide multiple methods that access that data. If one can compose ones problem into a lot of classes of object that only have one method then it is quite acceptable to just use closures instead: HI = ‘Hello’ def greeter(title, name): def f(): print(HI, title, name) return f I wrote a whole bunch o…

You can manipulate f.__name__ to improve the debugging experience, since presumably you actually care for what is in f(), not greeter.

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

#76
post #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.

I agree. I think of this as a functor pattern: object that supports some params being bound at constructor time, and other params set when the function is called later.

In languages that let you operator overload function call syntax, you end up with an object that, once constructed, supports being called like with same syntax as a function call. This works easily in python (define __init__ and __call__ ), and you don't have to fight the typesystem to structure code that will accept both a callable object or a function.

Another perspective of the whole thing is that you have a function with many arguments, then you curry to bind some arguments, then pass the resulting function with remaining free arguments to be called.

I prefer structuring code as functor objects as it lets you access the bound parameters as attributes (if you want to expose them) which can also sometimes be useful in code or in test

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

#77

Earlier quoted context omitted.

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

Python has private members through double underscore methods

That’s not their purpose and they’re not really private. Leading double underscore methods cause name mangling (the class name gets added to the method name) and they’re used for solving specific problems with inheritance. Don’t use them just to make something private. You’re screwing up your inheritance if you don’t realize what you’re doing.

Leading single underscore is private by convention. Still should generally be avoided, but it’s the proper use.

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

#78
Author makes some valid points, but jumps to conclusions too quickly. Most importantly, code in examples that actually solves problem is omitted (`[...recursive solution omitted...]`) - and that code can be potentially very long. If you have very long function, you should break it into smaller functions. But, those functions need to operate on shared state. In some languages (Python) you can have nested functions that can operate on variables defined in parent function, but in some other languages you can't, so you can pass shared state in other ways, like function parameters or, wait for it, what can you use when you have bunch of functions operating on some shared state? Use classes! You can even wrap it simple function to have cleaner interface.

Other counter examples might be Template Method pattern etc.

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

#80

Why make it a class even, it doesn't hold any state. Also I'd do it like: DominoTilingCounter tc; std::cout << tc.count(4, 7) << '\n';

Then, count could be a static method, and you wouldn’t need to construct the DominoTilingCounter, yielding

  std::cout 
That’s close to a free function, using the class only for grouping (say of private helper functions, or for memoization data structures)
Post reply on HN