web2students.com
web2students.com

Reputation: 307

Which is a more maintainable way to construct objects?

I am trying to learn design patterns, best practices, etc. There is a particular problem that always confuses me. Abstraction is more important or KISS (keep it simple and stupid). Suppose I need to construct an object which several properties. There are 2 ways:

1) Instantiate the object and pass properties to the constructor.

$user = new user($user_detail_array); // one line code is enough

Benefits of this method:

Negatives:

2) Instantiate the object and set each property individually.

$user = new user();
$user->setName($name);
$user->setEmail($email);
$user->setGender($gender);
so on....

Benefits:

Negatives:

In general what is the better way? If it depends on situation, when is the first approach better than the second? Why? I found that both approaches caused problems. The first method makes the code too abstract. The second method makes code too long and unmanageable.

Upvotes: 1

Views: 160

Answers (4)

MattDavey
MattDavey

Reputation: 9017

The reality is that I don't think there's much difference between your two examples.

Take the first example $user = new user($user_detail_array);. Yes this seems shorter, but in reality, what will we see usually before this line?

$user_detail_array = {
    "name" => "Bill",
    "email" => "[email protected]",
    "gender" => "male"
};
$user = new user($user_detail_array);

Wow, now it doesn't look so different from the second method...

$user = new user();
$user->name = "Bill";
$user->email = "[email protected]";
$user->gender = "male";

My point is, every property of the user entity will appear next to a => (assignment) at some point in the program. Whether it's in array keys, properties of a builder object, or directly on the entity itself - they have to be assigned some time.

I think the best thing to do in this case is keep the assignments (=>) as close to the entity as possible, so it's most clear what is being assigned and where it is being assigned to.

Upvotes: 0

luke1985
luke1985

Reputation: 2354

Another thing about abstraction is that You don't need to set name, email and gender separately.

You can simply abstract them into the array and create a new setter:

$user->setUserDetail($userDetailArray);

Upvotes: 0

Max Tkachenko
Max Tkachenko

Reputation: 504

I think you just misunderstood some concepts. First of all, KISS doesn't mean that you should write bad code without using abstraction. In fact, abstraction is one of the most powerful things that helps programmers write simple code.

The way you are going to design your program always depends on situation. If you are writing script that should run only once, do not even think about abstraction. It would be an overkill. However, if the lifetime of your program is pretty long, try to use abstraction and provide clients of your user class with intuitive and simple interface. Imagine, that code using user wants to change just it's name. In your first way they should provide user with the whole new array. In the second way you have just to call method setName. That's it.

To summarize: when you are designing a class, do not think about simplicity of writing this code. You should rather think about simplicity of using the class you are currently writing.

Upvotes: 2

aaaaaa123456789
aaaaaa123456789

Reputation: 5842

$userBuilder = new UserBuilder();
$userBuilder.name = $name;
// and so on
$user = $userBuilder -> build();

It's abstract, doesn't repeat anything, and it adds another pattern (Builder).

Upvotes: 1

Related Questions