samedi 27 juin 2015

Change-detector tests, data encapsulation and data actually necessary for a function to operate

The question was inspired by this article: http://ift.tt/1dq3JkT

In short, it discourages us to write tests that break when we change the interface in our functions, forcing us to waste time rewriting each call to match new signature.

If we take above article and guidelines into consideration, it becomes logical to rewrite functions from

void someFunc(int param1, int param2)

to

void someFunc(Object o) 

in fact, originally the function may have been called as:

somefunc(o.param1, o.param2);

and now it becomes

somefunc(o);

So, passing an object instead of separate params, gives us some protection for cases where we might need o.param3 for use in function later down the line. But..... does it not contradict the guideline that functions should not receive more information than they need to operate on? I certainly remember reading something like that a couple years back.

If an object has param4 and param5 that this function will never need, then passing the whole object seems incorrect in the light of this guideline...

What is the correct way to think about it? I am only starting to really get into unit testing and stuff like this is kinda confusing to me. Also, I kinda get the feeling that I completely misunderstood the point of that article...

Aucun commentaire:

Enregistrer un commentaire