0

Consider the following code:

public class Car {

 public string name;
 public string owner;
 public string id;

 public Car(string name, string owner) {
  this.name = name;
  this.owner = owner;
  id = GenerateId(name);

 }

 private static string GenerateId(string name) {
  //do some fancy computation to derive the ID from the name, such as hash it or anything else.
  return result;
 }
}

The important bit is that GenerateId is a static function here that explicitly takes an argument.

Now, let us consider the following, where GenerateId is made into an instance function that takes no arguments explicitly, instead just references the instance field it needed.

public class Car {

 public string name;
 public string owner;
 public string id;

 public Car(string name, string owner) {
  this.name = name;
  this.owner = owner;
  GenerateId(name);

 }

 private void GenerateId() {
  //do some fancy computation to derive the ID from the name, such as hash it or anything else.
  string computation_result = fancy_computation(this.name);
  this.id = computation_result;
 }
}

Another approach would be an instance method that references the id field directly, but needs the "name" string as an argument anyway.

Which is the "more proper" approach that an experienced programmer would follow? Why?

My current thoughts are:

  • If we think GenerateId is always going to be called with name as the argument (and in the future it most likely won't change), it's safe to just reference the name field instead of passing it as the string argument. However, if we expect at some point we will want to compute the ID using the owner string as the base, we can in advance design the function to take a string argument.
  • I've heard multiple times that 'a function should take as few arguments as possible'.

The more I think about it, the more it seems to be a question of "how generic should we make this function?" that isn't decided by the OOP rules, but simply on a case-by-case basis: ask yourself whether you're gonna need a more generic version, or if the function both now and in the future is most likely to do only this one thing, so you're free to 'hardcode' it in, instead of writing more code to make it more generic.

3
  • 3
    This is completely opinionated because there are multiple "correct" answers and so it's off-topic for StackOverflow. The first thing you should keep in mind is "does a Car generate their own Id?" What if you then use the same strategy for another entity? What if you change that so it's generated by, say, a database? Commented Jun 9, 2019 at 9:05
  • 1
    Well the second approach sets the ID of the instance. The first approach simply returns a string and it has nothing to do with the ID. Also, since the fields are public, all of that becomes meaningless because the person using your class can just set the fields. Are you writing a library which you will sell and people will use it? Yes, then you need to figure out how people will use it and make sure you cover most of the cases. No, then only concentrate on the current problem you're trying to solve. Commented Jun 9, 2019 at 11:42
  • 1
    Do you need the logic inside GenerateId if you do not have an instance of Car? Yes, then make it static. No, make it non static. Commented Jun 9, 2019 at 11:45

1 Answer 1

1

If you want to use the generateId method to generate IDs for any given string (or any input) then you can consider the first case. That way generateId will be fully independent of other class attributes.

Extending the above logic, if you want to make the input dependent but the assignment you make with the result independent of other class attributes then the 2nd option is preferred.

In case you want to tightly couple the input and the assignment from the result with the class attributes then go for the 3rd approach. In this case you cannot use this method to generate IDs for other inputs or assign it to other variables.

Sign up to request clarification or add additional context in comments.

2 Comments

In case it is fully independent from instance variables, should it be automatically made static?
Yes, it can be made static and I would prefer that in this case. But also consider this... In this static method if you refer to static variables then the results could be quite different when those variables are modified by other objects.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.