Sunday, April 18, 2010

Refactoring challenge: finding a better name

A common code smell is that a name containing the word "And". For example, consider class below:
	private final static class ObjectAndGetSetter
{
private final IGetAndSet getAndSetter;
private final Object value;
...
//getters and setters
}
public static interface IGetAndSet
{
public Method getGetter();
public Method getSetter();
}

Both the IGetAndSet and ObjectAndGetSetter names contain the word "And". Why is it a bad thing? Because there are multiple concepts in the name (e.g., the concepts of getter and setter in IGetAndSet), indicating that the single concept has not been spelled out yet. For this case, for IGetAndSet, I think the single concept is "property". For ObjectAndGetSetter, the single concept is "property of a given instance". So, I'd change the code as:
	private final static class InstanceProperty
{
private final IProperty property;
private final Object instance;
...
//getters and setters
}
public static interface IProperty
{
public Method getGetter();
public Method getSetter();
}

Now the challenge: in Spring MVC there is a ModelAndView class:
public class ModelAndView {
/** View instance or view name String */
private Object view;
/** Model Map */
private ModelMap model;
...
}

How do you rename it? Or is it good enough? Post your suggestions as comments.