Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If a “method” returns a value, is it really a method? Does it mutate “this”? What does it mean to be an object, if you’re not being changed at all?


I wrote that other comment just before dozing off for the night because I was surprised no one else had replied yet. But to expand on it:

In Eiffel (which is an object oriented language), methods on objects are separated into two kinds: commands and queries. This is also known as the "Command-Query Separation" principle. The idea is that commands are the ones causing changes to the systems and queries only inform you about the system. `a_set.add(item)` changes the set, while `a_set.has(item)` tells you about the set but makes no changes to it. It's very useful, and very common. I mean, I'd hope that `some_collection.Size()` doesn't actually change its size, that's not what it exists to do.

https://en.wikipedia.org/wiki/Command–query_separation


Thanks, I wasn’t familiar with the name of this principle!

Though I feel these definitions of OOP are closing on the Actor model, so exact definitions are not really possible to give here.


Sometimes, depending on the nature of the implementation, Size() might do the work to compute the size, cache the result, and clear a dirty flag on the cache.

Not sure if that matters.


It does not need to mutate this but it can:

1. Read the fields of 'this' 2. Write the fields of 'this' 3. Call other methods of 'this' (including "private" methods in many OOP languages)

That is very characteristic of OOP languages. Does anybody know if FP languages have something like the 'this' and if not what do they use instead?


Yes, it is a method. It would be a “query” if it causes no changes but returns a value.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: