Live data from Hacker News

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

quuxplusone.github.io

241–250 of 406 posts

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

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

You aren't arguing anything, you are only stating a very shaky premise.

Not any less meaningful then your comment. You aren't arguing anything, you are only stating a very shaky premise.

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

#242

Earlier quoted context omitted.

What you call “for free”, I call implicit. Passing it as function arguments makes it explicit. You don't need to “hate” “objects” to prefer the explicit over the implicit.

I don't find all that much of a difference between notations like set.map{...} and map(set){...}, so at that point I suppose it really is just aesthetic preference.

Sounds like you would enjoy Uniform Function Call Syntax!

https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax

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

#243
post #119
post #90

“the count() member function sounds like it should be non-modifying, but in fact it needs to update member data and thus cannot be const” That isn’t correct. Just declare count as mutable . See https://en.cppreference.com/w/cpp/language/cv . Memoization is a valid use case for that feature.

Often it's the motivating example when the feature gets explained.

I would argue that mutexs or other concurrency primitives are the quintessential example, but caching is the other big one.

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

#244

I had to look up what memoization is- guess I’m getting rusty? But it turns out it’s just a new word for an old concept (caching). I’ve always done it this way: double calculation(double arg) { thread_local bool prev_arg_valid = false; thread_local double prev_arg; thread_local double prev_result; double result; if(prev_arg_valid && prev_arg == arg) result = prev_result; else { //do the calculation result = ...; prev…

> I had to look up what memoization is- guess I’m getting rusty? But it turns out it’s just a new word for an old concept (caching).

The term memoization was coined in 1968 and quite possibly predates the term cache (with respect to computing).

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

#245
post #229

Earlier quoted context omitted.

Seems like kind of a meaningless gesture to pass in your own “this” just because you don’t like OOP. If you need a class, why not write it the idiomatic way?

I didn't down-vote you, but in my opinion it's not meaningless because it makes it inherently more testable. Try testing an opaque class without any accessibility into its state. It is much more difficult. If it takes in the state, performs an operation, and returns a new version of that state (ideally an immutable copy) then it becomes much easier to test and validate. The reason information hiding is/was advocated…

Isn't how you pass things into a function separate from whether you are hiding information? If your state parameter was an opaque pointer then you wouldn't necessarily be able to do anything with it. Also if your class internals were public you would be able to change anything you wanted.

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

#246

Earlier quoted context omitted.

Sure, you can build your own object and pass the 'this' pointer around manually. You could use C for everything, and tediously do everything again that objects do for free. But why? There's a lot of object-hating going around. Its silly. Use objects to encapsulate functionality, they are good at that and everybody understands what it means.

You don’t agree with an idea so it’s silly? Passing around state via variables is no more tedious than the extra syntax for class definitions, constructors, member variable access, extra semantics related to objects, extra keywords related to visibility, etc. You can encapsulate functionality without objects. There is nothing that you can do with an object that you can’t do with a closure, and the closure version wil…

Objects with a single public method are more tedious than equivalent closures. Objects with multiple public methods are much more friendly than closures with multiple public entry points (they can be done, but they look and feel hacky).

Also, pieces of data that hide their internal structure and just expose an API to modify that data (Objects) are a very useful construct, and used in all languages with first-class mutation. Some languages have special syntax for this case (e.g. C++, Python, Go, OCaml), some don't (e.g. C). For example, no language that I know of exposes a mutable List type with a public count of elements. Neither does any language I know of expose a mutable List type as a closure.

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

#247

Earlier quoted context omitted.

Sure, you can build your own object and pass the 'this' pointer around manually. You could use C for everything, and tediously do everything again that objects do for free. But why? There's a lot of object-hating going around. Its silly. Use objects to encapsulate functionality, they are good at that and everybody understands what it means.

You don’t agree with an idea so it’s silly? Passing around state via variables is no more tedious than the extra syntax for class definitions, constructors, member variable access, extra semantics related to objects, extra keywords related to visibility, etc. You can encapsulate functionality without objects. There is nothing that you can do with an object that you can’t do with a closure, and the closure version wil…

Though i agree with with your general statement, i think the 1/10th the size figure is a bit disingenuous. At least the cases where i've gone from the unnecessary OO style to a simpler function/procedure based solution (in JS, which supports both styles just nice), the code size difference hasn't been anything that great. At most it's been half the size. Which is a lot i think! But not an order of magnitude less.

I'm noting this because i think that number kinda detracts from your main argument, with which i agree completely. I think even if the code would remain roughly the same size, the removal of OO would still be worth it, since, as you mention, is much less jargon and conceptual baggage. And if i can achieve the same result with a much simpler conceptual model and language, then all the better :)

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

#248
post #235

Earlier quoted context omitted.

The class will have to provide you with an option to get your answer back after a computation, so that's what you test. I don't see a reason to test how a class computes a result as long as the result is correct.

Well either you have internal state or you don't. If you don't have internal state (and it just returns the answer), then there is no difference between it and a function. If it does have internal state, then the answer it returns depends on the value of that internal state which means it's hard to verify that the answer returned is correct since you have to know the internal state of the object.

The whole idea of internal state is that it is not accessible (so not relevant) to the outside world.

You test a class as a user by calling its methods in the desired order (per requirements and documentation) and you check that the results you get are correct. If the results are correct, the class fits your use case. If they are not, then you're either calling it wrong or it has a bug. Either way, no need to know the internal state.

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

#249

Earlier quoted context omitted.

Well either you have internal state or you don't. If you don't have internal state (and it just returns the answer), then there is no difference between it and a function. If it does have internal state, then the answer it returns depends on the value of that internal state which means it's hard to verify that the answer returned is correct since you have to know the internal state of the object.

The whole idea of internal state is that it is not accessible (so not relevant) to the outside world. You test a class as a user by calling its methods in the desired order (per requirements and documentation) and you check that the results you get are correct. If the results are correct, the class fits your use case. If they are not, then you're either calling it wrong or it has a bug. Either way, no need to know th…

"Calling its methods in the desired order"

This is my point. The order of calling the methods matters, because it manipulates opaque internal state. Since this ordering matters, the caller is implicitly dependent on this internal state as it must know the order to call the methods in to get the desired result.

So it's very relevant to the outside world in my opinion and nothing is truly hidden. It becomes part of the public API whether you hide it or not.

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

#250

Earlier quoted context omitted.

You don’t agree with an idea so it’s silly? Passing around state via variables is no more tedious than the extra syntax for class definitions, constructors, member variable access, extra semantics related to objects, extra keywords related to visibility, etc. You can encapsulate functionality without objects. There is nothing that you can do with an object that you can’t do with a closure, and the closure version wil…

Though i agree with with your general statement, i think the 1/10th the size figure is a bit disingenuous. At least the cases where i've gone from the unnecessary OO style to a simpler function/procedure based solution (in JS, which supports both styles just nice), the code size difference hasn't been anything that great. At most it's been half the size. Which is a lot i think! But not an order of magnitude less. I'm…

I will definitely admit that those numbers are made up, more of a rhetorical technique. Numbers are important, I do agree.
Post reply on HN