When writing object-oriented, the receiver is not a subject, but an object? Which is correct, rails tutorial?

Asked 2 years ago, Updated 2 years ago, 77 views

I have a question about object orientation.

Here's a quote from the article:

About naming methods for the Rails model

A good example is that the receiver and instance method are transitive and object relationships.
And as a bad example, the receiver and instance method have a subject-verb relationship.

Good example

cow.grow#<=I grow up the cow. As a result, the cow.age is incremented, etc.
file.delete#<=Idelete the file. As a result, the file disappears on an external file system (for a program).
job.perform#<=I perform the job.

Bad example

manager.evaluate(member)#<=Amanager values this member.

However, the rails tutorial implements the following methods:
This is user follow other user, so this is a bad case.

#app/models/user.rb

  # follow a user
  def follow(other_user)
    active_relationships.create(followed_id:other_user.id)
  end

  # unfollow a user
  default(other_user)
    active_relationships.find_by (followed_id:other_user.id).destroy
  end

Which is true, the argument in this article is wrong, or whether the tutorial deviates from object-oriented principles?

ruby-on-rails ruby

2022-09-30 11:37

2 Answers

There are some rules for naming conventions for each language, but I think there is no absolute rule, such as unifying them for each project team or deciding them for each project in some cases.

By the way, in the language that has spread object-oriented language called Smalltalk,
·Send a message to the object.How you interpret the message depends on the object you receive.
 There is a way of thinking thatIf you want to adopt that idea, I think the "bad example" of the method is more object-oriented.
 The idea of "good example" is that "me" is the subject, so it is the caller who decides the behavior of the object, and the object is given the result.On the other hand, "bad example" gives the object permission to decide what to do.

Also, "good example" seems to be out of balance while trying to use the subject as "I" and the object as an object.
The subject of cow.grow is cow.
file.delete is an object/predicate as claimed, but the English word order is strange.
Simply,
fileManager.delete(file)
It is more natural to use objects as objects, as shown in .

However, as I wrote in the beginning, naming conventions can be determined by each project team or project, so it is important to decide which one to choose.


2022-09-30 11:37

The article about the naming of the original method is too much of an English grammar (word order), and I think that's why it's become a strange story.

cow.grow
file.delete

I don't think it's bad in itself, but rather than depending on the English word order, it's an anemia of the so-called cow and file.Maybe it's to avoid this kind of thing.

Therefore, if the logic is to follow or unfollow users, it seems natural to implement it as a user method.

Therefore, both the original article and the Rails tutorial sample seem to be correct about the good or bad code.However, isn't it wrong to mention the correctness criteria based on the English word order?


2022-09-30 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.