-1

In the code sample below. Would UserId, Email, and UserType be considered "dependencies" with respect to the ChangeEmail method? would they be considered "collaborators"? Is it a condition for a member to be of a complex type for it to be considered either?

public class User
{
    public int UserId { get; private set; }
    public string Email { get; private set; }
    public UserType Type { get; private set; }
    public void ChangeEmail(int userId, string newEmail)
    {
        object[] data = Database.GetUserById(userId);
        UserId = userId;
        Email = (string)data[1];
        Type = (UserType)data[2];
        
        if (Email == newEmail)
        return;

        object[] companyData = Database.GetCompany();
        string companyDomainName = (string)companyData[0];
        int numberOfEmployees = (int)companyData[1];
        
        string emailDomain = newEmail.Split('@')[1];
        bool isEmailCorporate = emailDomain == companyDomainName;
        UserType newType = isEmailCorporate ? UserType.Employee : UserType.Customer;
        if (Type != newType)
        {
            int delta = newType == UserType.Employee ? 1 : -1;
            int newNumber = numberOfEmployees + delta;
            Database.SaveCompany(newNumber);
        }
        Email = newEmail;
        Type = newType;
        Database.SaveUser(this);
        MessageBus.SendEmailChangedMessage(UserId, newEmail);
    }
}
public enum UserType
{
    Customer = 1,
    Employee = 2
}
3
  • 2
    Curiously, the snippet doesn't show any of the two cases. ChangeEmail is setting up attributes, but it's not operating through or with them. The method could be rewritten so it doesn't make any reference to the members and it would still compile and work. Basically, the method doesn't depend on any of the members to actually do its job. The only it needs is Database
    – Laiv
    Aug 16, 2021 at 16:58
  • This code was copied from text. I assume that the properties would be used by clients of the class at some point, thus the public modifier.
    – 3m3sd1
    Aug 16, 2021 at 20:16
  • Well, the question is in relation to ChangeEmail, not consumers. Anyways, it doesn't change the observation. The method will work with or without these members because it doesn't use them. Not sure if we can tag instance attributes as "dependencies". I guess it depends on the kind of object. If it's a service, then yes, dependencies are likely collaborators. If it's a POJO ... I don't see it so clear.
    – Laiv
    Aug 16, 2021 at 20:25

1 Answer 1

0

This heart of this question is the definition of these terms. So lets dig up some definitions.

Dependency (computer science) or coupling,
a state in which one object uses a function of another object

wikipedia.org - Dependency

Collaborators
(other classes) with which this class interacts to fulfill its responsibilities

wikipedia.org - Class Responsibility Collaboration Card

Given that the usual way classes interact is through methods (which is a fancy name for a particular kind of function) it's easy to see these terms as meaning the same thing. However, they are not typically mixed together in the same literature. When I hear one mentioned vs the other it mostly tells me which books the speaker has been reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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