Suppose I have a custom object, Student:
public class Student{
public int _id;
public String name;
public int age;
public float score;
}
And a class, Window, that is used to show information of a Student:
public class Window{
public void showInfo(Student student);
}
It looks quite normal, but I found Window is not quite easy to test individually, because it needs a real Student object to call the function. So I try to modify showInfo so that it does not accept a Student object directly:
public void showInfo(int _id, String name, int age, float score);
so that it is easier to test Window individually:
showInfo(123, "abc", 45, 6.7);
But I found the modified version has another problems:
Modify Student (e.g.:add new properties) requires modifying the method-signature of showInfo
If Student had many properties, the method-signature of Student would be very long.
So, using custom objects as parameter or accept each property in objects as parameter, which one is more maintainable?
showInfo
requires a real String, a real float and two real ints. How is providing a realString
object better than providing a realStudent
object? – Bart van Ingen Schenau May 12 '16 at 7:08int
parameters. From the call site, there's no verification that you're actually passing them in the right order. What if you swapid
andage
, orfirstName
andlastName
? You're introducing a potential point of failure that can be very hard to detect until it blows up in your face, and you're adding it at every call site. – Chris Hayes May 12 '16 at 7:30showForm(bool, bool, bool, bool, int)
method - I love those... – Boris the Spider May 12 '16 at 8:02