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?
showInforequires a real String, a real float and two real ints. How is providing a realStringobject better than providing a realStudentobject? – Bart van Ingen Schenau May 12 '16 at 07:08intparameters. From the call site, there's no verification that you're actually passing them in the right order. What if you swapidandage, orfirstNameandlastName? 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 07:30showForm(bool, bool, bool, bool, int)method - I love those... – Boris the Spider May 12 '16 at 08:02Windowthat knows how to display aStudent, theStudentshould know how to display itself (after all, it's the only object that should know what it's attributes are) and you should pass it aWindow(or better yet something that implements aDisplayinterface) to display itself in. Having to extract all of an object's attributes so some external method can do something with them is a design smell. – TMN May 13 '16 at 15:42showForm(bool, bool, bool, bool, int, bool)and have it call the first one. Repeat ad nausem. – corsiKa May 13 '16 at 16:18Studentknow *anything* about presentation/UI? – RubberDuck May 14 '16 at 17:33