I'm reading Robert Martin's book "Clean Code" and most of what I've read makes sense and I'm trying to apply as much as I possibly can. One of the simplest most basic things he talks about is that methods should be small, he goes all the way as to say that they should not contain more than three lines.
Considering that I broke down a recursive method into two smaller ones but in a code review one of my peers suggested that it looked weird and that recursion was kind of hidden.
So, what is your opinion on that? I'm including both approaches below.
This is the one method approach.
private object GetObject(string propertyName, object dataObject)
{
object returnObject;
if (dataObject == null) return null;
if (propertyName.Contains("."))
{
Type dataObjectType = dataObject.GetType();
PropertyInfo dataObjectProperty = dataObjectType.GetProperty(propertyName.Substring(0, propertyName.IndexOf(".")));
ThrowExceptionIfObjectOrPropertyInexistant(dataObjectProperty, dataObjectType.FullName, propertyName);
object nestedObject = dataObjectProperty.GetValue(dataObject, null);
string nestedObjectPropertyName = propertyName.Substring(propertyName.IndexOf(".") + 1);
returnObject = GetObject(nestedObjectPropertyName, nestedObject);
}
else
{
returnObject = dataObject;
}
return returnObject;
}
This is the two methods one.
private object GetObject(string propertyName, object dataObject)
{
object returnObject;
if (dataObject == null) return null;
if (propertyName.Contains("."))
{
returnObject = GetNestedObject(propertyName, dataObject);
}
else
{
returnObject = dataObject;
}
return returnObject;
}
private object GetNestedObject(string propertyName, object dataObject)
{
object returnObject;
Type dataObjectType = dataObject.GetType();
PropertyInfo dataObjectProperty = dataObjectType.GetProperty(propertyName.Substring(0, propertyName.IndexOf(".")));
ThrowExceptionIfObjectOrPropertyInexistant(dataObjectProperty, dataObjectType.FullName, propertyName);
object nestedObject = dataObjectProperty.GetValue(dataObject, null);
string nestedObjectPropertyName = propertyName.Substring(propertyName.IndexOf(".") + 1);
returnObject = GetObject(nestedObjectPropertyName, nestedObject);
return returnObject;
}