Live data from Hacker News

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

quuxplusone.github.io

31–40 of 406 posts

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

#31
post #6

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.

A static class in C# isn't really a class, it's just a namespace to hold functions.

Classes in OOP are type dependent namespaces. That's the entire reason why classes even exist. You can now reuse function and member names inside classes instead of having to globally qualify them to prevent name collisions. In C you would do something like class_name_function_name(var, par1, par2). The OOP counterpart would look like this in Java: ClassName var = new ClassName(); var.functionName(par1, par2).

Now you might be asking if this is really such a big deal. I mean it is pretty obvious that this can be useful even if you limit yourself to static dispatch and completely avoid inheritance and polymorphism. The reality is that functional programming languages like Haskell don't support type dependent namespaces. When you name a field in a Haskell record then no other record is allowed to have a field with the same name.

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

#32

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"?

likely "game" would be something that would be about the whole game, including graphics, sounds, etc.

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

#33
post #6

Earlier quoted context omitted.

A static class in C# isn't really a class, it's just a namespace to hold functions.

Classes in OOP are type dependent namespaces. That's the entire reason why classes even exist. You can now reuse function and member names inside classes instead of having to globally qualify them to prevent name collisions. In C you would do something like class_name_function_name(var, par1, par2). The OOP counterpart would look like this in Java: ClassName var = new ClassName(); var.functionName(par1, par2). Now yo…

A static class in C# can't have any instance data, instance methods, and can't be instantiated. It's literally a namespace in everything but name.

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

#36
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.

Yes, this is the point of `dataclasses` and `attrs`.

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

#39
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 of Python like this, yesterday. It felt liberating! So terse!

Downside: one very common reason to have more than one method in a class is to define __str__. There will be no pretty debug printable version of my greeter() “class” for me, alas.

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

#40
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…

Mutable in C++11 land holds a weird space because the threading model says that const member methods are thread safe, which mutable member variables are not.
Post reply on HN