This is more of a general design concept, but if I had a class which I intend to have a list of those objects (around 5000) and I need to perform some function on each of these, should I be designing around a function that is part of the class or a separate function that takes a collection of these objects? I know that there's always situations where one may be better than another, but when first starting to think about the design, which way would be recommend For example:
class Person {
float HungerLevel;
public void AdjustHunger(float amount) { HungerLevel += amount; }
}
public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }
or
class Person {
float HungerLevel;
public void AdjustHunger(float amount) { HungerLevel += amount; }
}
public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += amount; } }
I would imagine that the second option is more efficient if I need to change all objects in the collection by the same value, but not sure. I guess it could even depend on what language I'm using and how it would try to optimize the code? Does a call to a function have a higher cost than a call to an object's variable? Expanding on this example, what if the hunger change was based on another of the objects variables.
class Person{
float HungerLevel;
float Metabolism;
public void AdjustHunger(float amount) { HungerLevel += (amount * Metabolism); }
public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }
vs.
class Person{
float HungerLevel;
public void AdjustHunger(float amount) { HungerLevel += amount; }
}
public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += (amount * Person.Metabolism); } }
I don't have a project right now that deals with this, but the concept popped into my head and I was thinking about how I would start designing this. I could probably setup a test case and try it out with a sample, but is that what most programmers do at the designing stage, creating multiple test implementations? Is there some sort of 'design theory' that I would/should be using? I'm self taught, so I don't know if this would be something that is covered in a more structured training environment (so please excuse me if this is a dumb quest