I've been working in a rather large codebase filled to the brim with small classes such as
class Person
{
public string name;
public int age;
public int height;
}
As a mainly front-end experience developer I'm finding a hard way to determine the best practice when returning multiple values from methods. For example in Javascript all one would need to do is return a js object. I know one could use keywords such as "out" or "ref" and receive a similar result, but I find using them hinders readability.
So lets say we're finding a person's height and age from a database based on their name. And because the codebase is old and disorganized, you're not allowed to rely on Entity or other frameworks for model classes.
Would you rather create a method like this:
public Person getPerson(string name)
{
Person p = new Person("Samuel");
p.height = db.getHeight("Samuel");
p.age = db.getAge("Samuel");
return p;
}
or just give the class a constructor like so:
class Person
{
public string name;
public int age;
public int height;
public Person(string name)
{
this.name = name;
this.age = db.getAge(name);
this.height = db.getHeight(name);
}
}
Personally, I find that the class with the constructor is better design since you're keeping all the object's logic encapsulated within itself. However, a lot of the codebase uses the method-based approach. I don't really need to worry about going against the grain, my supervisor is pretty open to changes. I would just like to know what the best approach would be.
Thanks!