Chaining getters and code completion abuse
23 Jul 2006 9:32 - (0) comments
A problem with code completion is that it can be abused to chain getters to find the desired object. You have an object A that has an object B with an object C that has a property d. So you chain it together:
A a = getAFromSomewhere();
String d = a.getB().getC().getD();
And do something with it...
if(d.equals("red")){
a.doSomething();
a.doSomethingElse();
}
This however causes an undesired tight coupling of classes. Here the current class is dependend on classes A, B and C. If the getD method changes it's algorithm, the current class and the class C have to be checked for bugs. And other classes might depend on the getD method as well. The more you chain different classes together the harder it becomes to change things.
The problem isn't always as visible and can be obfuscated with evil temp vars like:
private DatabaseUtil getDatabaseUtil() {
FContext fContext = FContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)
fContext.getExternalContext()
.getContext();
return (DatabaseUtil) servletContext
.getAttribute("DATABASE_UTIL");
}
Which is the same as:
private DatabaseUtil getDatabaseUtil() {
return FContext
.getCurrentInstance()
.getExternalContext()
.((ServletContext) getContext())
.((DatabaseUtil) getAttribute("DATABASE_UTIL"));
}
Also see: http://martinfowler.com/bliki/GetterEradicator.html
Comments
No comments allowed.