Is there some recommended practice regarding methods as verbs in OOP? Should the object work syntactically as subject or as object/complement?

Should object.doSomething() be normally understood as "the object iself does something" (subject) or "the caller does something with the object"?

I suspect that the first alternative is more right, and it sounds more natural with such a general verb... But consider for example "OutputStream.write(byte[])", which...

writes b.length bytes from the specified byte array to this output stream.

Here it's not the object who is the subject of the action, it's the caller. The Writer (rather confusingly) does not really "write", its the caller is who "writes" bytes to the Writer. Should this be considered incorrect?

  • If you extend the logic, it is the CPU that manipulates the instructions and data, and it is you who code... Can be repeated infinitely many times. These are just abstractions, some of which are chosen for mere convenience. – Deer Hunter Jul 27 '13 at 17:59
  • You are asking the object to perform some action. Think of a method call as a message you are sending the object. Here you are sending the message "write" to the object "stream" passing the parameters you want the object to write. – Martin York Jul 27 '13 at 19:40
  • 1
    @LokiAstari: But, precisely, that's not the semantic of the "write" method. We are not asking the object to write (it is not the subject of the verb), but "we" (the caller) are writing to it. – leonbloy Jul 27 '13 at 19:53
  • @leonbloy: In this instance, I disagree; you are asking the OutputStream instance to "write" the specified bytes. You're not writing to the OutputStream instance, you're having the OutputStream instance write to whatever-it's-set-up-to-write-to. (But in general, I agree that the method-name is not always a verb with the instance as subject.) – ruakh Jul 27 '13 at 21:38
  • @leonbloy: No. I am definitely asking the stream to perform an action (the action being write). – Martin York Jul 28 '13 at 0:33

No, sometimes.

The object can also be the actor, in which the verb may accept a subject as a parameter. These types of objects are typically called 'service' or service class.

Take for instance a money transfer from one bank account to another. Which of the bank accounts is the subject? Perhaps they are both subject? Would one account transfer it's credit to the other account or is it better to have a third object that manages the money transfer?

Objects as subject are nice as long as the verb is operating mostly on it's own data/state.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.